blob: 2fe15351ac4eb14c370fadb2960acd913a4648c1 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Marc Olson8ae4e442017-09-06 17:23:56 -070037unsigned int admin_timeout = 60;
38module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080039MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
Marc Olson8ae4e442017-09-06 17:23:56 -070042unsigned int nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080044MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020047static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080048module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Christoph Hellwig44e44b22017-04-05 19:18:11 +020051static u8 nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070053MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010054
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080055static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080056module_param(default_ps_max_latency_us, ulong, 0644);
57MODULE_PARM_DESC(default_ps_max_latency_us,
58 "max power saving latency for new devices; use PM QOS to change per device");
59
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070060static bool force_apst;
61module_param(force_apst, bool, 0644);
62MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
63
Jens Axboef5d11842017-06-27 12:03:06 -060064static bool streams;
65module_param(streams, bool, 0644);
66MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010071static DEFINE_IDA(nvme_subsystems_ida);
72static LIST_HEAD(nvme_subsystems);
73static DEFINE_MUTEX(nvme_subsystems_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010074
Christoph Hellwig9843f682017-10-18 13:10:01 +020075static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020076static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010077static struct class *nvme_class;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010078static struct class *nvme_subsys_class;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010079
Keith Busch84fef622017-11-07 10:28:32 -070080static void nvme_ns_remove(struct nvme_ns *ns);
81static int nvme_revalidate_disk(struct gendisk *disk);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010082
Arnav Dawnb6dccf72017-07-12 16:10:40 +053083static __le32 nvme_get_log_dw10(u8 lid, size_t size)
84{
85 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
86}
87
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020088int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
89{
90 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
91 return -EBUSY;
92 if (!queue_work(nvme_wq, &ctrl->reset_work))
93 return -EBUSY;
94 return 0;
95}
96EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
97
98static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
99{
100 int ret;
101
102 ret = nvme_reset_ctrl(ctrl);
103 if (!ret)
104 flush_work(&ctrl->reset_work);
105 return ret;
106}
107
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200108static void nvme_delete_ctrl_work(struct work_struct *work)
109{
110 struct nvme_ctrl *ctrl =
111 container_of(work, struct nvme_ctrl, delete_work);
112
Sagi Grimberg40546372017-10-29 14:21:02 +0200113 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200114 nvme_stop_ctrl(ctrl);
115 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200116 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200117 nvme_uninit_ctrl(ctrl);
118 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200119}
120
121int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
122{
123 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
124 return -EBUSY;
125 if (!queue_work(nvme_wq, &ctrl->delete_work))
126 return -EBUSY;
127 return 0;
128}
129EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
130
131int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
132{
133 int ret = 0;
134
135 /*
136 * Keep a reference until the work is flushed since ->delete_ctrl
137 * can free the controller.
138 */
139 nvme_get_ctrl(ctrl);
140 ret = nvme_delete_ctrl(ctrl);
141 if (!ret)
142 flush_work(&ctrl->delete_work);
143 nvme_put_ctrl(ctrl);
144 return ret;
145}
146EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
147
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100148static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
149{
150 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
151}
152
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200153static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200154{
155 switch (nvme_req(req)->status & 0x7ff) {
156 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200157 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200158 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200159 return BLK_STS_NOSPC;
Keith Busche96fef22018-01-09 12:04:14 -0700160 case NVME_SC_LBA_RANGE:
161 return BLK_STS_TARGET;
162 case NVME_SC_BAD_ATTRIBUTES:
Junxiong Guane02ab022017-04-21 12:59:07 +0200163 case NVME_SC_ONCS_NOT_SUPPORTED:
Keith Busche96fef22018-01-09 12:04:14 -0700164 case NVME_SC_INVALID_OPCODE:
165 case NVME_SC_INVALID_FIELD:
166 case NVME_SC_INVALID_NS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200167 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200168 case NVME_SC_WRITE_FAULT:
169 case NVME_SC_READ_ERROR:
170 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200171 case NVME_SC_ACCESS_DENIED:
172 case NVME_SC_READ_ONLY:
Keith Busche96fef22018-01-09 12:04:14 -0700173 case NVME_SC_COMPARE_FAILED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200174 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200175 case NVME_SC_GUARD_CHECK:
176 case NVME_SC_APPTAG_CHECK:
177 case NVME_SC_REFTAG_CHECK:
178 case NVME_SC_INVALID_PI:
179 return BLK_STS_PROTECTION;
180 case NVME_SC_RESERVATION_CONFLICT:
181 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200182 default:
183 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200184 }
185}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200186
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200187static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200188{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200189 if (blk_noretry_request(req))
190 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200191 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200192 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200193 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200194 return false;
195 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200196}
197
198void nvme_complete_rq(struct request *req)
199{
Keith Busch908e4562018-01-09 12:04:15 -0700200 blk_status_t status = nvme_error_status(req);
201
202 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
203 if (nvme_req_needs_failover(req, status)) {
Christoph Hellwig32acab32017-11-02 12:59:30 +0100204 nvme_failover_req(req);
205 return;
206 }
207
208 if (!blk_queue_dying(req->q)) {
209 nvme_req(req)->retries++;
210 blk_mq_requeue_request(req, true);
211 return;
212 }
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200213 }
Keith Busch908e4562018-01-09 12:04:15 -0700214 blk_mq_end_request(req, status);
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200215}
216EXPORT_SYMBOL_GPL(nvme_complete_rq);
217
Ming Linc55a2fd2016-05-18 14:05:02 -0700218void nvme_cancel_request(struct request *req, void *data, bool reserved)
219{
Ming Linc55a2fd2016-05-18 14:05:02 -0700220 if (!blk_mq_request_started(req))
221 return;
222
223 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
224 "Cancelling I/O %d", req->tag);
225
Christoph Hellwige54b0642017-11-02 21:28:51 +0300226 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200227 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200228
Ming Linc55a2fd2016-05-18 14:05:02 -0700229}
230EXPORT_SYMBOL_GPL(nvme_cancel_request);
231
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200232bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
233 enum nvme_ctrl_state new_state)
234{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300235 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200236 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200237 bool changed = false;
238
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200239 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300240
241 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200242 switch (new_state) {
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800243 case NVME_CTRL_ADMIN_ONLY:
244 switch (old_state) {
245 case NVME_CTRL_RESETTING:
246 changed = true;
247 /* FALLTHRU */
248 default:
249 break;
250 }
251 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200252 case NVME_CTRL_LIVE:
253 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200254 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200255 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900256 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200257 changed = true;
258 /* FALLTHRU */
259 default:
260 break;
261 }
262 break;
263 case NVME_CTRL_RESETTING:
264 switch (old_state) {
265 case NVME_CTRL_NEW:
266 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800267 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900268 changed = true;
269 /* FALLTHRU */
270 default:
271 break;
272 }
273 break;
274 case NVME_CTRL_RECONNECTING:
275 switch (old_state) {
276 case NVME_CTRL_LIVE:
James Smart3cec7f92017-10-25 16:43:13 -0700277 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200278 changed = true;
279 /* FALLTHRU */
280 default:
281 break;
282 }
283 break;
284 case NVME_CTRL_DELETING:
285 switch (old_state) {
286 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800287 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200288 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900289 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200290 changed = true;
291 /* FALLTHRU */
292 default:
293 break;
294 }
295 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600296 case NVME_CTRL_DEAD:
297 switch (old_state) {
298 case NVME_CTRL_DELETING:
299 changed = true;
300 /* FALLTHRU */
301 default:
302 break;
303 }
304 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200305 default:
306 break;
307 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200308
309 if (changed)
310 ctrl->state = new_state;
311
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200312 spin_unlock_irqrestore(&ctrl->lock, flags);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100313 if (changed && ctrl->state == NVME_CTRL_LIVE)
314 nvme_kick_requeue_lists(ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200315 return changed;
316}
317EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
318
Christoph Hellwiged754e52017-11-09 13:50:43 +0100319static void nvme_free_ns_head(struct kref *ref)
320{
321 struct nvme_ns_head *head =
322 container_of(ref, struct nvme_ns_head, ref);
323
Christoph Hellwig32acab32017-11-02 12:59:30 +0100324 nvme_mpath_remove_disk(head);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100325 ida_simple_remove(&head->subsys->ns_ida, head->instance);
326 list_del_init(&head->entry);
327 cleanup_srcu_struct(&head->srcu);
328 kfree(head);
329}
330
331static void nvme_put_ns_head(struct nvme_ns_head *head)
332{
333 kref_put(&head->ref, nvme_free_ns_head);
334}
335
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100336static void nvme_free_ns(struct kref *kref)
337{
338 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
339
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200340 if (ns->ndev)
341 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100342
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100343 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100344 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700345 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100346 kfree(ns);
347}
348
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100349static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100350{
351 kref_put(&ns->kref, nvme_free_ns);
352}
353
Christoph Hellwig41609822015-11-20 09:00:02 +0100354struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800355 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100356{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100357 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100358 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100359
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200360 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100361 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200362 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100363 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200364 qid ? qid - 1 : 0);
365 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100366 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100367 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100368
Christoph Hellwig21d34712015-11-26 09:08:36 +0100369 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800370 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100371
Christoph Hellwig41609822015-11-20 09:00:02 +0100372 return req;
373}
Ming Lin576d55d2016-02-10 10:03:32 -0800374EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100375
Jens Axboef5d11842017-06-27 12:03:06 -0600376static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
377{
378 struct nvme_command c;
379
380 memset(&c, 0, sizeof(c));
381
382 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530383 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600384 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
385 c.directive.dtype = NVME_DIR_IDENTIFY;
386 c.directive.tdtype = NVME_DIR_STREAMS;
387 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
388
389 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
390}
391
392static int nvme_disable_streams(struct nvme_ctrl *ctrl)
393{
394 return nvme_toggle_streams(ctrl, false);
395}
396
397static int nvme_enable_streams(struct nvme_ctrl *ctrl)
398{
399 return nvme_toggle_streams(ctrl, true);
400}
401
402static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
403 struct streams_directive_params *s, u32 nsid)
404{
405 struct nvme_command c;
406
407 memset(&c, 0, sizeof(c));
408 memset(s, 0, sizeof(*s));
409
410 c.directive.opcode = nvme_admin_directive_recv;
411 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700412 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600413 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
414 c.directive.dtype = NVME_DIR_STREAMS;
415
416 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
417}
418
419static int nvme_configure_directives(struct nvme_ctrl *ctrl)
420{
421 struct streams_directive_params s;
422 int ret;
423
424 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
425 return 0;
426 if (!streams)
427 return 0;
428
429 ret = nvme_enable_streams(ctrl);
430 if (ret)
431 return ret;
432
Arnav Dawn62346ea2017-07-12 16:11:53 +0530433 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600434 if (ret)
435 return ret;
436
437 ctrl->nssa = le16_to_cpu(s.nssa);
438 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
439 dev_info(ctrl->device, "too few streams (%u) available\n",
440 ctrl->nssa);
441 nvme_disable_streams(ctrl);
442 return 0;
443 }
444
445 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
446 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
447 return 0;
448}
449
450/*
451 * Check if 'req' has a write hint associated with it. If it does, assign
452 * a valid namespace stream to the write.
453 */
454static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
455 struct request *req, u16 *control,
456 u32 *dsmgmt)
457{
458 enum rw_hint streamid = req->write_hint;
459
460 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
461 streamid = 0;
462 else {
463 streamid--;
464 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
465 return;
466
467 *control |= NVME_RW_DTYPE_STREAMS;
468 *dsmgmt |= streamid << 16;
469 }
470
471 if (streamid < ARRAY_SIZE(req->q->write_hints))
472 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
473}
474
Ming Lin8093f7c2016-04-12 13:10:14 -0600475static inline void nvme_setup_flush(struct nvme_ns *ns,
476 struct nvme_command *cmnd)
477{
478 memset(cmnd, 0, sizeof(*cmnd));
479 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100480 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600481}
482
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200483static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600484 struct nvme_command *cmnd)
485{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100486 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600487 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100488 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600489
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100490 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600491 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200492 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600493
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100494 __rq_for_each_bio(bio, req) {
495 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
496 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
497
498 range[n].cattr = cpu_to_le32(0);
499 range[n].nlb = cpu_to_le32(nlb);
500 range[n].slba = cpu_to_le64(slba);
501 n++;
502 }
503
504 if (WARN_ON_ONCE(n != segments)) {
505 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200506 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100507 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600508
509 memset(cmnd, 0, sizeof(*cmnd));
510 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100511 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200512 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600513 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
514
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700515 req->special_vec.bv_page = virt_to_page(range);
516 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100517 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700518 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600519
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200520 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600521}
Ming Lin8093f7c2016-04-12 13:10:14 -0600522
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200523static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
524 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600525{
Jens Axboef5d11842017-06-27 12:03:06 -0600526 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600527 u16 control = 0;
528 u32 dsmgmt = 0;
529
530 if (req->cmd_flags & REQ_FUA)
531 control |= NVME_RW_FUA;
532 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
533 control |= NVME_RW_LR;
534
535 if (req->cmd_flags & REQ_RAHEAD)
536 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
537
538 memset(cmnd, 0, sizeof(*cmnd));
539 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100540 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600541 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
542 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
543
Jens Axboef5d11842017-06-27 12:03:06 -0600544 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
545 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
546
Ming Lin8093f7c2016-04-12 13:10:14 -0600547 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100548 /*
549 * If formated with metadata, the block layer always provides a
550 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
551 * we enable the PRACT bit for protection information or set the
552 * namespace capacity to zero to prevent any I/O.
553 */
554 if (!blk_integrity_rq(req)) {
555 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
556 return BLK_STS_NOTSUPP;
557 control |= NVME_RW_PRINFO_PRACT;
558 }
559
Ming Lin8093f7c2016-04-12 13:10:14 -0600560 switch (ns->pi_type) {
561 case NVME_NS_DPS_PI_TYPE3:
562 control |= NVME_RW_PRINFO_PRCHK_GUARD;
563 break;
564 case NVME_NS_DPS_PI_TYPE1:
565 case NVME_NS_DPS_PI_TYPE2:
566 control |= NVME_RW_PRINFO_PRCHK_GUARD |
567 NVME_RW_PRINFO_PRCHK_REF;
568 cmnd->rw.reftag = cpu_to_le32(
569 nvme_block_nr(ns, blk_rq_pos(req)));
570 break;
571 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600572 }
573
574 cmnd->rw.control = cpu_to_le16(control);
575 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200576 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600577}
578
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200579blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600580 struct nvme_command *cmd)
581{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200582 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600583
Christoph Hellwig987f6992017-04-05 19:18:08 +0200584 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200585 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200586 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200587 req->rq_flags |= RQF_DONTPREP;
588 }
589
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100590 switch (req_op(req)) {
591 case REQ_OP_DRV_IN:
592 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800593 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100594 break;
595 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600596 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100597 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200598 case REQ_OP_WRITE_ZEROES:
599 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100600 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600601 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100602 break;
603 case REQ_OP_READ:
604 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200605 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100606 break;
607 default:
608 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200609 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100610 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600611
James Smart721b3912016-10-21 23:33:34 +0300612 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600613 return ret;
614}
615EXPORT_SYMBOL_GPL(nvme_setup_cmd);
616
Christoph Hellwig41609822015-11-20 09:00:02 +0100617/*
618 * Returns 0 on success. If the result is negative, it's a Linux error code;
619 * if the result is positive, it's an NVM Express status code
620 */
621int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800622 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800623 unsigned timeout, int qid, int at_head,
624 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100625{
626 struct request *req;
627 int ret;
628
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200629 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100630 if (IS_ERR(req))
631 return PTR_ERR(req);
632
633 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
634
Christoph Hellwig21d34712015-11-26 09:08:36 +0100635 if (buffer && bufflen) {
636 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
637 if (ret)
638 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100639 }
640
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200641 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800642 if (result)
643 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200644 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
645 ret = -EINTR;
646 else
647 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100648 out:
649 blk_mq_free_request(req);
650 return ret;
651}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200652EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100653
654int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
655 void *buffer, unsigned bufflen)
656{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200657 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
658 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100659}
Ming Lin576d55d2016-02-10 10:03:32 -0800660EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100661
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400662static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
663 unsigned len, u32 seed, bool write)
664{
665 struct bio_integrity_payload *bip;
666 int ret = -ENOMEM;
667 void *buf;
668
669 buf = kmalloc(len, GFP_KERNEL);
670 if (!buf)
671 goto out;
672
673 ret = -EFAULT;
674 if (write && copy_from_user(buf, ubuf, len))
675 goto out_free_meta;
676
677 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
678 if (IS_ERR(bip)) {
679 ret = PTR_ERR(bip);
680 goto out_free_meta;
681 }
682
683 bip->bip_iter.bi_size = len;
684 bip->bip_iter.bi_sector = seed;
685 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
686 offset_in_page(buf));
687 if (ret == len)
688 return buf;
689 ret = -ENOMEM;
690out_free_meta:
691 kfree(buf);
692out:
693 return ERR_PTR(ret);
694}
695
Keith Busch63263d62017-08-29 17:46:04 -0400696static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400697 struct nvme_command *cmd, void __user *ubuffer,
698 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
699 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100700{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200701 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600702 struct nvme_ns *ns = q->queuedata;
703 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100704 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600705 struct bio *bio = NULL;
706 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100707 int ret;
708
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200709 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100710 if (IS_ERR(req))
711 return PTR_ERR(req);
712
713 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
714
715 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100716 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
717 GFP_KERNEL);
718 if (ret)
719 goto out;
720 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200721 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400722 if (disk && meta_buffer && meta_len) {
723 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
724 meta_seed, write);
725 if (IS_ERR(meta)) {
726 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600727 goto out_unmap;
728 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600729 }
730 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400731
Keith Busch0b7f1f22015-10-23 09:47:28 -0600732 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200733 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
734 ret = -EINTR;
735 else
736 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100737 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800738 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600739 if (meta && !ret && !write) {
740 if (copy_to_user(meta_buffer, meta, meta_len))
741 ret = -EFAULT;
742 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600743 kfree(meta);
744 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200745 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600746 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100747 out:
748 blk_mq_free_request(req);
749 return ret;
750}
751
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200752static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200753{
754 struct nvme_ctrl *ctrl = rq->end_io_data;
755
756 blk_mq_free_request(rq);
757
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200758 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200759 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200760 "failed nvme_keep_alive_end_io error=%d\n",
761 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200762 return;
763 }
764
765 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
766}
767
768static int nvme_keep_alive(struct nvme_ctrl *ctrl)
769{
770 struct nvme_command c;
771 struct request *rq;
772
773 memset(&c, 0, sizeof(c));
774 c.common.opcode = nvme_admin_keep_alive;
775
776 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
777 NVME_QID_ANY);
778 if (IS_ERR(rq))
779 return PTR_ERR(rq);
780
781 rq->timeout = ctrl->kato * HZ;
782 rq->end_io_data = ctrl;
783
784 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
785
786 return 0;
787}
788
789static void nvme_keep_alive_work(struct work_struct *work)
790{
791 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
792 struct nvme_ctrl, ka_work);
793
794 if (nvme_keep_alive(ctrl)) {
795 /* allocation failure, reset the controller */
796 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200797 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200798 return;
799 }
800}
801
802void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
803{
804 if (unlikely(ctrl->kato == 0))
805 return;
806
807 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
808 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
809}
810EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
811
812void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
813{
814 if (unlikely(ctrl->kato == 0))
815 return;
816
817 cancel_delayed_work_sync(&ctrl->ka_work);
818}
819EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
820
Keith Busch3f7f25a92017-06-20 15:09:56 -0400821static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100822{
823 struct nvme_command c = { };
824 int error;
825
826 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
827 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200828 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100829
830 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
831 if (!*id)
832 return -ENOMEM;
833
834 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
835 sizeof(struct nvme_id_ctrl));
836 if (error)
837 kfree(*id);
838 return error;
839}
840
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200841static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100842 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200843{
844 struct nvme_command c = { };
845 int status;
846 void *data;
847 int pos;
848 int len;
849
850 c.identify.opcode = nvme_admin_identify;
851 c.identify.nsid = cpu_to_le32(nsid);
852 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
853
854 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
855 if (!data)
856 return -ENOMEM;
857
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200858 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200859 NVME_IDENTIFY_DATA_SIZE);
860 if (status)
861 goto free_data;
862
863 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
864 struct nvme_ns_id_desc *cur = data + pos;
865
866 if (cur->nidl == 0)
867 break;
868
869 switch (cur->nidt) {
870 case NVME_NIDT_EUI64:
871 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200872 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200873 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
874 cur->nidl);
875 goto free_data;
876 }
877 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100878 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200879 break;
880 case NVME_NIDT_NGUID:
881 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200882 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200883 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
884 cur->nidl);
885 goto free_data;
886 }
887 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100888 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200889 break;
890 case NVME_NIDT_UUID:
891 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200892 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200893 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
894 cur->nidl);
895 goto free_data;
896 }
897 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100898 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200899 break;
900 default:
901 /* Skip unnkown types */
902 len = cur->nidl;
903 break;
904 }
905
906 len += sizeof(*cur);
907 }
908free_data:
909 kfree(data);
910 return status;
911}
912
Keith Busch540c8012015-10-22 15:45:06 -0600913static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
914{
915 struct nvme_command c = { };
916
917 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200918 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600919 c.identify.nsid = cpu_to_le32(nsid);
920 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
921}
922
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200923static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
924 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100925{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200926 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100927 struct nvme_command c = { };
928 int error;
929
930 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200931 c.identify.opcode = nvme_admin_identify;
932 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200933 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100934
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200935 id = kmalloc(sizeof(*id), GFP_KERNEL);
936 if (!id)
937 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100938
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200939 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
940 if (error) {
941 dev_warn(ctrl->device, "Identify namespace failed\n");
942 kfree(id);
943 return NULL;
944 }
945
946 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100947}
948
Keith Busch3f7f25a92017-06-20 15:09:56 -0400949static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700950 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100951{
952 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800953 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100954 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100955
956 memset(&c, 0, sizeof(c));
957 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100958 c.features.fid = cpu_to_le32(fid);
959 c.features.dword11 = cpu_to_le32(dword11);
960
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800961 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700962 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700963 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800964 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100965 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100966}
967
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100968int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
969{
970 u32 q_count = (*count - 1) | ((*count - 1) << 16);
971 u32 result;
972 int status, nr_io_queues;
973
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700974 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100975 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200976 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100977 return status;
978
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200979 /*
980 * Degraded controllers might return an error when setting the queue
981 * count. We still want to be able to bring them online and offer
982 * access to the admin queue, as that might be only way to fix them up.
983 */
984 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200985 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200986 *count = 0;
987 } else {
988 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
989 *count = min(*count, nr_io_queues);
990 }
991
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100992 return 0;
993}
Ming Lin576d55d2016-02-10 10:03:32 -0800994EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100995
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100996static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
997{
998 struct nvme_user_io io;
999 struct nvme_command c;
1000 unsigned length, meta_len;
1001 void __user *metadata;
1002
1003 if (copy_from_user(&io, uio, sizeof(io)))
1004 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001005 if (io.flags)
1006 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001007
1008 switch (io.opcode) {
1009 case nvme_cmd_write:
1010 case nvme_cmd_read:
1011 case nvme_cmd_compare:
1012 break;
1013 default:
1014 return -EINVAL;
1015 }
1016
1017 length = (io.nblocks + 1) << ns->lba_shift;
1018 meta_len = (io.nblocks + 1) * ns->ms;
1019 metadata = (void __user *)(uintptr_t)io.metadata;
1020
1021 if (ns->ext) {
1022 length += meta_len;
1023 meta_len = 0;
1024 } else if (meta_len) {
1025 if ((io.metadata & 3) || !io.metadata)
1026 return -EINVAL;
1027 }
1028
1029 memset(&c, 0, sizeof(c));
1030 c.rw.opcode = io.opcode;
1031 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001032 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001033 c.rw.slba = cpu_to_le64(io.slba);
1034 c.rw.length = cpu_to_le16(io.nblocks);
1035 c.rw.control = cpu_to_le16(io.control);
1036 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1037 c.rw.reftag = cpu_to_le32(io.reftag);
1038 c.rw.apptag = cpu_to_le16(io.apptag);
1039 c.rw.appmask = cpu_to_le16(io.appmask);
1040
Keith Busch63263d62017-08-29 17:46:04 -04001041 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001042 (void __user *)(uintptr_t)io.addr, length,
1043 metadata, meta_len, io.slba, NULL, 0);
1044}
1045
Keith Busch84fef622017-11-07 10:28:32 -07001046static u32 nvme_known_admin_effects(u8 opcode)
1047{
1048 switch (opcode) {
1049 case nvme_admin_format_nvm:
1050 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1051 NVME_CMD_EFFECTS_CSE_MASK;
1052 case nvme_admin_sanitize_nvm:
1053 return NVME_CMD_EFFECTS_CSE_MASK;
1054 default:
1055 break;
1056 }
1057 return 0;
1058}
1059
1060static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1061 u8 opcode)
1062{
1063 u32 effects = 0;
1064
1065 if (ns) {
1066 if (ctrl->effects)
1067 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1068 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1069 dev_warn(ctrl->device,
1070 "IO command:%02x has unhandled effects:%08x\n",
1071 opcode, effects);
1072 return 0;
1073 }
1074
1075 if (ctrl->effects)
1076 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1077 else
1078 effects = nvme_known_admin_effects(opcode);
1079
1080 /*
1081 * For simplicity, IO to all namespaces is quiesced even if the command
1082 * effects say only one namespace is affected.
1083 */
1084 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1085 nvme_start_freeze(ctrl);
1086 nvme_wait_freeze(ctrl);
1087 }
1088 return effects;
1089}
1090
1091static void nvme_update_formats(struct nvme_ctrl *ctrl)
1092{
1093 struct nvme_ns *ns;
1094
1095 mutex_lock(&ctrl->namespaces_mutex);
1096 list_for_each_entry(ns, &ctrl->namespaces, list) {
1097 if (ns->disk && nvme_revalidate_disk(ns->disk))
1098 nvme_ns_remove(ns);
1099 }
1100 mutex_unlock(&ctrl->namespaces_mutex);
1101}
1102
1103static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1104{
1105 /*
1106 * Revalidate LBA changes prior to unfreezing. This is necessary to
1107 * prevent memory corruption if a logical block size was changed by
1108 * this command.
1109 */
1110 if (effects & NVME_CMD_EFFECTS_LBCC)
1111 nvme_update_formats(ctrl);
1112 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1113 nvme_unfreeze(ctrl);
1114 if (effects & NVME_CMD_EFFECTS_CCC)
1115 nvme_init_identify(ctrl);
1116 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1117 nvme_queue_scan(ctrl);
1118}
1119
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001120static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001121 struct nvme_passthru_cmd __user *ucmd)
1122{
1123 struct nvme_passthru_cmd cmd;
1124 struct nvme_command c;
1125 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001126 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001127 int status;
1128
1129 if (!capable(CAP_SYS_ADMIN))
1130 return -EACCES;
1131 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1132 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001133 if (cmd.flags)
1134 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001135
1136 memset(&c, 0, sizeof(c));
1137 c.common.opcode = cmd.opcode;
1138 c.common.flags = cmd.flags;
1139 c.common.nsid = cpu_to_le32(cmd.nsid);
1140 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1141 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1142 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1143 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1144 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1145 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1146 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1147 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1148
1149 if (cmd.timeout_ms)
1150 timeout = msecs_to_jiffies(cmd.timeout_ms);
1151
Keith Busch84fef622017-11-07 10:28:32 -07001152 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001153 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001154 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001155 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1156 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001157 nvme_passthru_end(ctrl, effects);
1158
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001159 if (status >= 0) {
1160 if (put_user(cmd.result, &ucmd->result))
1161 return -EFAULT;
1162 }
1163
1164 return status;
1165}
1166
Christoph Hellwig32acab32017-11-02 12:59:30 +01001167/*
1168 * Issue ioctl requests on the first available path. Note that unlike normal
1169 * block layer requests we will not retry failed request on another controller.
1170 */
1171static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1172 struct nvme_ns_head **head, int *srcu_idx)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001173{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001174#ifdef CONFIG_NVME_MULTIPATH
1175 if (disk->fops == &nvme_ns_head_ops) {
1176 *head = disk->private_data;
1177 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1178 return nvme_find_path(*head);
1179 }
1180#endif
1181 *head = NULL;
1182 *srcu_idx = -1;
1183 return disk->private_data;
1184}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001185
Christoph Hellwig32acab32017-11-02 12:59:30 +01001186static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1187{
1188 if (head)
1189 srcu_read_unlock(&head->srcu, idx);
1190}
1191
1192static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
1193{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001194 switch (cmd) {
1195 case NVME_IOCTL_ID:
1196 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001197 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001198 case NVME_IOCTL_ADMIN_CMD:
1199 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1200 case NVME_IOCTL_IO_CMD:
1201 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1202 case NVME_IOCTL_SUBMIT_IO:
1203 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001204 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001205#ifdef CONFIG_NVM
1206 if (ns->ndev)
1207 return nvme_nvm_ioctl(ns, cmd, arg);
1208#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001209 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001210 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001211 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001212 return -ENOTTY;
1213 }
1214}
1215
Christoph Hellwig32acab32017-11-02 12:59:30 +01001216static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1217 unsigned int cmd, unsigned long arg)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001218{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001219 struct nvme_ns_head *head = NULL;
1220 struct nvme_ns *ns;
1221 int srcu_idx, ret;
1222
1223 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1224 if (unlikely(!ns))
1225 ret = -EWOULDBLOCK;
1226 else
1227 ret = nvme_ns_ioctl(ns, cmd, arg);
1228 nvme_put_ns_from_disk(head, srcu_idx);
1229 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001230}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001231
1232static int nvme_open(struct block_device *bdev, fmode_t mode)
1233{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001234 struct nvme_ns *ns = bdev->bd_disk->private_data;
1235
Christoph Hellwig32acab32017-11-02 12:59:30 +01001236#ifdef CONFIG_NVME_MULTIPATH
1237 /* should never be called due to GENHD_FL_HIDDEN */
1238 if (WARN_ON_ONCE(ns->head->disk))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001239 goto fail;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001240#endif
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001241 if (!kref_get_unless_zero(&ns->kref))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001242 goto fail;
1243 if (!try_module_get(ns->ctrl->ops->module))
1244 goto fail_put_ns;
1245
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001246 return 0;
Nitzan Carmi85088c42018-01-04 17:56:13 +02001247
1248fail_put_ns:
1249 nvme_put_ns(ns);
1250fail:
1251 return -ENXIO;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001252}
1253
1254static void nvme_release(struct gendisk *disk, fmode_t mode)
1255{
Nitzan Carmi85088c42018-01-04 17:56:13 +02001256 struct nvme_ns *ns = disk->private_data;
1257
1258 module_put(ns->ctrl->ops->module);
1259 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001260}
1261
1262static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1263{
1264 /* some standard values */
1265 geo->heads = 1 << 6;
1266 geo->sectors = 1 << 5;
1267 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1268 return 0;
1269}
1270
1271#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001272static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001273{
1274 struct blk_integrity integrity;
1275
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001276 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001277 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001278 case NVME_NS_DPS_PI_TYPE3:
1279 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001280 integrity.tag_size = sizeof(u16) + sizeof(u32);
1281 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001282 break;
1283 case NVME_NS_DPS_PI_TYPE1:
1284 case NVME_NS_DPS_PI_TYPE2:
1285 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001286 integrity.tag_size = sizeof(u16);
1287 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001288 break;
1289 default:
1290 integrity.profile = NULL;
1291 break;
1292 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001293 integrity.tuple_size = ms;
1294 blk_integrity_register(disk, &integrity);
1295 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001296}
1297#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001298static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001299{
1300}
1301#endif /* CONFIG_BLK_DEV_INTEGRITY */
1302
Scott Bauer6b8190d2017-06-15 10:44:30 -06001303static void nvme_set_chunk_size(struct nvme_ns *ns)
1304{
1305 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1306 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1307}
1308
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001309static void nvme_config_discard(struct nvme_ctrl *ctrl,
1310 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001311{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001312 u32 size = queue_logical_block_size(queue);
1313
1314 if (stream_alignment)
1315 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001316
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001317 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1318 NVME_DSM_MAX_RANGES);
1319
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001320 queue->limits.discard_alignment = size;
1321 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001322
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001323 blk_queue_max_discard_sectors(queue, UINT_MAX);
1324 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1325 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001326
1327 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001328 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001329}
1330
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001331static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001332 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001333{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001334 memset(ids, 0, sizeof(*ids));
1335
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001336 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001337 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001338 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001339 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001340 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001341 /* Don't treat error as fatal we potentially
1342 * already have a NGUID or EUI-64
1343 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001344 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001345 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001346 "%s: Identify Descriptors failed\n", __func__);
1347 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001348}
1349
Christoph Hellwiged754e52017-11-09 13:50:43 +01001350static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1351{
1352 return !uuid_is_null(&ids->uuid) ||
1353 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1354 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1355}
1356
Christoph Hellwig002fab02017-11-09 13:50:16 +01001357static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1358{
1359 return uuid_equal(&a->uuid, &b->uuid) &&
1360 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1361 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1362}
1363
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001364static void nvme_update_disk_info(struct gendisk *disk,
1365 struct nvme_ns *ns, struct nvme_id_ns *id)
1366{
1367 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1368 unsigned stream_alignment = 0;
1369
1370 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1371 stream_alignment = ns->sws * ns->sgs;
1372
1373 blk_mq_freeze_queue(disk->queue);
1374 blk_integrity_unregister(disk);
1375
1376 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1377 if (ns->ms && !ns->ext &&
1378 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1379 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001380 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001381 capacity = 0;
1382 set_capacity(disk, capacity);
1383
1384 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1385 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1386 blk_mq_unfreeze_queue(disk->queue);
1387}
1388
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001389static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1390{
1391 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001392
1393 /*
1394 * If identify namespace failed, use default 512 byte block size so
1395 * block layer can use before failing read/write for 0 capacity.
1396 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001397 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001398 if (ns->lba_shift == 0)
1399 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001400 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001401 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1402 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1403 /* the PI implementation requires metadata equal t10 pi tuple size */
1404 if (ns->ms == sizeof(struct t10_pi_tuple))
1405 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1406 else
1407 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001408
Scott Bauer6b8190d2017-06-15 10:44:30 -06001409 if (ns->noiob)
1410 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001411 nvme_update_disk_info(disk, ns, id);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001412#ifdef CONFIG_NVME_MULTIPATH
1413 if (ns->head->disk)
1414 nvme_update_disk_info(ns->head->disk, ns, id);
1415#endif
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001416}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001417
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001418static int nvme_revalidate_disk(struct gendisk *disk)
1419{
1420 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001421 struct nvme_ctrl *ctrl = ns->ctrl;
1422 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001423 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001424 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001425
1426 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1427 set_capacity(disk, 0);
1428 return -ENODEV;
1429 }
1430
Christoph Hellwiged754e52017-11-09 13:50:43 +01001431 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001432 if (!id)
1433 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001434
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001435 if (id->ncap == 0) {
1436 ret = -ENODEV;
1437 goto out;
1438 }
1439
Keith Busch5e0fab52017-10-27 13:51:22 -06001440 __nvme_revalidate_disk(disk, id);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001441 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1442 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001443 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001444 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001445 ret = -ENODEV;
1446 }
1447
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001448out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001449 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001450 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001451}
1452
1453static char nvme_pr_type(enum pr_type type)
1454{
1455 switch (type) {
1456 case PR_WRITE_EXCLUSIVE:
1457 return 1;
1458 case PR_EXCLUSIVE_ACCESS:
1459 return 2;
1460 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1461 return 3;
1462 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1463 return 4;
1464 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1465 return 5;
1466 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1467 return 6;
1468 default:
1469 return 0;
1470 }
1471};
1472
1473static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1474 u64 key, u64 sa_key, u8 op)
1475{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001476 struct nvme_ns_head *head = NULL;
1477 struct nvme_ns *ns;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001478 struct nvme_command c;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001479 int srcu_idx, ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001480 u8 data[16] = { 0, };
1481
Keith Buschb0d61d52017-11-16 13:36:49 -07001482 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1483 if (unlikely(!ns))
1484 return -EWOULDBLOCK;
1485
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001486 put_unaligned_le64(key, &data[0]);
1487 put_unaligned_le64(sa_key, &data[8]);
1488
1489 memset(&c, 0, sizeof(c));
1490 c.common.opcode = op;
Keith Buschb0d61d52017-11-16 13:36:49 -07001491 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001492 c.common.cdw10[0] = cpu_to_le32(cdw10);
1493
Keith Buschb0d61d52017-11-16 13:36:49 -07001494 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001495 nvme_put_ns_from_disk(head, srcu_idx);
1496 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001497}
1498
1499static int nvme_pr_register(struct block_device *bdev, u64 old,
1500 u64 new, unsigned flags)
1501{
1502 u32 cdw10;
1503
1504 if (flags & ~PR_FL_IGNORE_KEY)
1505 return -EOPNOTSUPP;
1506
1507 cdw10 = old ? 2 : 0;
1508 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1509 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1510 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1511}
1512
1513static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1514 enum pr_type type, unsigned flags)
1515{
1516 u32 cdw10;
1517
1518 if (flags & ~PR_FL_IGNORE_KEY)
1519 return -EOPNOTSUPP;
1520
1521 cdw10 = nvme_pr_type(type) << 8;
1522 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1523 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1524}
1525
1526static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1527 enum pr_type type, bool abort)
1528{
1529 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1530 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1531}
1532
1533static int nvme_pr_clear(struct block_device *bdev, u64 key)
1534{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001535 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001536 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1537}
1538
1539static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1540{
1541 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1542 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1543}
1544
1545static const struct pr_ops nvme_pr_ops = {
1546 .pr_register = nvme_pr_register,
1547 .pr_reserve = nvme_pr_reserve,
1548 .pr_release = nvme_pr_release,
1549 .pr_preempt = nvme_pr_preempt,
1550 .pr_clear = nvme_pr_clear,
1551};
1552
Scott Bauera98e58e52017-02-03 12:50:32 -07001553#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001554int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1555 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001556{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001557 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001558 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001559
1560 memset(&cmd, 0, sizeof(cmd));
1561 if (send)
1562 cmd.common.opcode = nvme_admin_security_send;
1563 else
1564 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001565 cmd.common.nsid = 0;
1566 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1567 cmd.common.cdw10[1] = cpu_to_le32(len);
1568
1569 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1570 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1571}
1572EXPORT_SYMBOL_GPL(nvme_sec_submit);
1573#endif /* CONFIG_BLK_SED_OPAL */
1574
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001575static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001576 .owner = THIS_MODULE,
1577 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001578 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001579 .open = nvme_open,
1580 .release = nvme_release,
1581 .getgeo = nvme_getgeo,
1582 .revalidate_disk= nvme_revalidate_disk,
1583 .pr_ops = &nvme_pr_ops,
1584};
1585
Christoph Hellwig32acab32017-11-02 12:59:30 +01001586#ifdef CONFIG_NVME_MULTIPATH
1587static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
1588{
1589 struct nvme_ns_head *head = bdev->bd_disk->private_data;
1590
1591 if (!kref_get_unless_zero(&head->ref))
1592 return -ENXIO;
1593 return 0;
1594}
1595
1596static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
1597{
1598 nvme_put_ns_head(disk->private_data);
1599}
1600
1601const struct block_device_operations nvme_ns_head_ops = {
1602 .owner = THIS_MODULE,
1603 .open = nvme_ns_head_open,
1604 .release = nvme_ns_head_release,
1605 .ioctl = nvme_ioctl,
1606 .compat_ioctl = nvme_ioctl,
1607 .getgeo = nvme_getgeo,
1608 .pr_ops = &nvme_pr_ops,
1609};
1610#endif /* CONFIG_NVME_MULTIPATH */
1611
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001612static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1613{
1614 unsigned long timeout =
1615 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1616 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1617 int ret;
1618
1619 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001620 if (csts == ~0)
1621 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001622 if ((csts & NVME_CSTS_RDY) == bit)
1623 break;
1624
1625 msleep(100);
1626 if (fatal_signal_pending(current))
1627 return -EINTR;
1628 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001629 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001630 "Device not ready; aborting %s\n", enabled ?
1631 "initialisation" : "reset");
1632 return -ENODEV;
1633 }
1634 }
1635
1636 return ret;
1637}
1638
1639/*
1640 * If the device has been passed off to us in an enabled state, just clear
1641 * the enabled bit. The spec says we should set the 'shutdown notification
1642 * bits', but doing so may cause the device to complete commands to the
1643 * admin queue ... and we don't know what memory that might be pointing at!
1644 */
1645int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1646{
1647 int ret;
1648
1649 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1650 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1651
1652 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1653 if (ret)
1654 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001655
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001656 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001657 msleep(NVME_QUIRK_DELAY_AMOUNT);
1658
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001659 return nvme_wait_ready(ctrl, cap, false);
1660}
Ming Lin576d55d2016-02-10 10:03:32 -08001661EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001662
1663int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1664{
1665 /*
1666 * Default to a 4K page size, with the intention to update this
1667 * path in the future to accomodate architectures with differing
1668 * kernel and IO page sizes.
1669 */
1670 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1671 int ret;
1672
1673 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001674 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001675 "Minimum device page size %u too large for host (%u)\n",
1676 1 << dev_page_min, 1 << page_shift);
1677 return -ENODEV;
1678 }
1679
1680 ctrl->page_size = 1 << page_shift;
1681
1682 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1683 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001684 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001685 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1686 ctrl->ctrl_config |= NVME_CC_ENABLE;
1687
1688 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1689 if (ret)
1690 return ret;
1691 return nvme_wait_ready(ctrl, cap, true);
1692}
Ming Lin576d55d2016-02-10 10:03:32 -08001693EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001694
1695int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1696{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001697 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001698 u32 csts;
1699 int ret;
1700
1701 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1702 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1703
1704 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1705 if (ret)
1706 return ret;
1707
1708 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1709 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1710 break;
1711
1712 msleep(100);
1713 if (fatal_signal_pending(current))
1714 return -EINTR;
1715 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001716 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001717 "Device shutdown incomplete; abort shutdown\n");
1718 return -ENODEV;
1719 }
1720 }
1721
1722 return ret;
1723}
Ming Lin576d55d2016-02-10 10:03:32 -08001724EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001725
Christoph Hellwigda358252016-03-02 18:07:11 +01001726static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1727 struct request_queue *q)
1728{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001729 bool vwc = false;
1730
Christoph Hellwigda358252016-03-02 18:07:11 +01001731 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001732 u32 max_segments =
1733 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1734
Christoph Hellwigda358252016-03-02 18:07:11 +01001735 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001736 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001737 }
Keith Busche6282ae2016-12-19 11:37:50 -05001738 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1739 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001740 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001741 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1742 vwc = true;
1743 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001744}
1745
Jon Derrickdbf86b32017-08-16 09:51:29 +02001746static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1747{
1748 __le64 ts;
1749 int ret;
1750
1751 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1752 return 0;
1753
1754 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1755 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1756 NULL);
1757 if (ret)
1758 dev_warn_once(ctrl->device,
1759 "could not set timestamp (%d)\n", ret);
1760 return ret;
1761}
1762
Keith Busch634b8322017-08-10 11:23:31 +02001763static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001764{
1765 /*
1766 * APST (Autonomous Power State Transition) lets us program a
1767 * table of power state transitions that the controller will
1768 * perform automatically. We configure it with a simple
1769 * heuristic: we are willing to spend at most 2% of the time
1770 * transitioning between power states. Therefore, when running
1771 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001772 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001773 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001774 * the requested maximum latency.
1775 *
1776 * We will not autonomously enter any non-operational state for
1777 * which the total latency exceeds ps_max_latency_us. Users
1778 * can set ps_max_latency_us to zero to turn off APST.
1779 */
1780
1781 unsigned apste;
1782 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001783 u64 max_lat_us = 0;
1784 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001785 int ret;
1786
1787 /*
1788 * If APST isn't supported or if we haven't been initialized yet,
1789 * then don't do anything.
1790 */
1791 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001792 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001793
1794 if (ctrl->npss > 31) {
1795 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001796 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001797 }
1798
1799 table = kzalloc(sizeof(*table), GFP_KERNEL);
1800 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001801 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001802
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001803 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001804 /* Turn off APST. */
1805 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001806 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001807 } else {
1808 __le64 target = cpu_to_le64(0);
1809 int state;
1810
1811 /*
1812 * Walk through all states from lowest- to highest-power.
1813 * According to the spec, lower-numbered states use more
1814 * power. NPSS, despite the name, is the index of the
1815 * lowest-power state, not the number of states.
1816 */
1817 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001818 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001819
1820 if (target)
1821 table->entries[state] = target;
1822
1823 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001824 * Don't allow transitions to the deepest state
1825 * if it's quirked off.
1826 */
1827 if (state == ctrl->npss &&
1828 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1829 continue;
1830
1831 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001832 * Is this state a useful non-operational state for
1833 * higher-power states to autonomously transition to?
1834 */
1835 if (!(ctrl->psd[state].flags &
1836 NVME_PS_FLAGS_NON_OP_STATE))
1837 continue;
1838
Kai-Heng Fengda875912017-06-07 15:25:42 +08001839 exit_latency_us =
1840 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1841 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001842 continue;
1843
Kai-Heng Fengda875912017-06-07 15:25:42 +08001844 total_latency_us =
1845 exit_latency_us +
1846 le32_to_cpu(ctrl->psd[state].entry_lat);
1847
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001848 /*
1849 * This state is good. Use it as the APST idle
1850 * target for higher power states.
1851 */
1852 transition_ms = total_latency_us + 19;
1853 do_div(transition_ms, 20);
1854 if (transition_ms > (1 << 24) - 1)
1855 transition_ms = (1 << 24) - 1;
1856
1857 target = cpu_to_le64((state << 3) |
1858 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001859
1860 if (max_ps == -1)
1861 max_ps = state;
1862
1863 if (total_latency_us > max_lat_us)
1864 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001865 }
1866
1867 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001868
1869 if (max_ps == -1) {
1870 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1871 } else {
1872 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1873 max_ps, max_lat_us, (int)sizeof(*table), table);
1874 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001875 }
1876
1877 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1878 table, sizeof(*table), NULL);
1879 if (ret)
1880 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1881
1882 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001883 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001884}
1885
1886static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1887{
1888 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1889 u64 latency;
1890
1891 switch (val) {
1892 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1893 case PM_QOS_LATENCY_ANY:
1894 latency = U64_MAX;
1895 break;
1896
1897 default:
1898 latency = val;
1899 }
1900
1901 if (ctrl->ps_max_latency_us != latency) {
1902 ctrl->ps_max_latency_us = latency;
1903 nvme_configure_apst(ctrl);
1904 }
1905}
1906
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001907struct nvme_core_quirk_entry {
1908 /*
1909 * NVMe model and firmware strings are padded with spaces. For
1910 * simplicity, strings in the quirk table are padded with NULLs
1911 * instead.
1912 */
1913 u16 vid;
1914 const char *mn;
1915 const char *fr;
1916 unsigned long quirks;
1917};
1918
1919static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001920 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001921 /*
1922 * This Toshiba device seems to die using any APST states. See:
1923 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1924 */
1925 .vid = 0x1179,
1926 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001927 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001928 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001929};
1930
1931/* match is null-terminated but idstr is space-padded. */
1932static bool string_matches(const char *idstr, const char *match, size_t len)
1933{
1934 size_t matchlen;
1935
1936 if (!match)
1937 return true;
1938
1939 matchlen = strlen(match);
1940 WARN_ON_ONCE(matchlen > len);
1941
1942 if (memcmp(idstr, match, matchlen))
1943 return false;
1944
1945 for (; matchlen < len; matchlen++)
1946 if (idstr[matchlen] != ' ')
1947 return false;
1948
1949 return true;
1950}
1951
1952static bool quirk_matches(const struct nvme_id_ctrl *id,
1953 const struct nvme_core_quirk_entry *q)
1954{
1955 return q->vid == le16_to_cpu(id->vid) &&
1956 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1957 string_matches(id->fr, q->fr, sizeof(id->fr));
1958}
1959
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001960static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
1961 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02001962{
1963 size_t nqnlen;
1964 int off;
1965
1966 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1967 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001968 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02001969 return;
1970 }
1971
1972 if (ctrl->vs >= NVME_VS(1, 2, 1))
1973 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1974
1975 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001976 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02001977 "nqn.2014.08.org.nvmexpress:%4x%4x",
1978 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001979 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001980 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001981 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001982 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001983 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
1984}
1985
1986static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
1987{
1988 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
1989 kfree(subsys);
1990}
1991
1992static void nvme_release_subsystem(struct device *dev)
1993{
1994 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
1995}
1996
1997static void nvme_destroy_subsystem(struct kref *ref)
1998{
1999 struct nvme_subsystem *subsys =
2000 container_of(ref, struct nvme_subsystem, ref);
2001
2002 mutex_lock(&nvme_subsystems_lock);
2003 list_del(&subsys->entry);
2004 mutex_unlock(&nvme_subsystems_lock);
2005
Christoph Hellwiged754e52017-11-09 13:50:43 +01002006 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002007 device_del(&subsys->dev);
2008 put_device(&subsys->dev);
2009}
2010
2011static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2012{
2013 kref_put(&subsys->ref, nvme_destroy_subsystem);
2014}
2015
2016static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2017{
2018 struct nvme_subsystem *subsys;
2019
2020 lockdep_assert_held(&nvme_subsystems_lock);
2021
2022 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2023 if (strcmp(subsys->subnqn, subsysnqn))
2024 continue;
2025 if (!kref_get_unless_zero(&subsys->ref))
2026 continue;
2027 return subsys;
2028 }
2029
2030 return NULL;
2031}
2032
Hannes Reinecke1e496932017-11-10 10:58:23 +01002033#define SUBSYS_ATTR_RO(_name, _mode, _show) \
2034 struct device_attribute subsys_attr_##_name = \
2035 __ATTR(_name, _mode, _show, NULL)
2036
2037static ssize_t nvme_subsys_show_nqn(struct device *dev,
2038 struct device_attribute *attr,
2039 char *buf)
2040{
2041 struct nvme_subsystem *subsys =
2042 container_of(dev, struct nvme_subsystem, dev);
2043
2044 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2045}
2046static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2047
2048#define nvme_subsys_show_str_function(field) \
2049static ssize_t subsys_##field##_show(struct device *dev, \
2050 struct device_attribute *attr, char *buf) \
2051{ \
2052 struct nvme_subsystem *subsys = \
2053 container_of(dev, struct nvme_subsystem, dev); \
2054 return sprintf(buf, "%.*s\n", \
2055 (int)sizeof(subsys->field), subsys->field); \
2056} \
2057static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2058
2059nvme_subsys_show_str_function(model);
2060nvme_subsys_show_str_function(serial);
2061nvme_subsys_show_str_function(firmware_rev);
2062
2063static struct attribute *nvme_subsys_attrs[] = {
2064 &subsys_attr_model.attr,
2065 &subsys_attr_serial.attr,
2066 &subsys_attr_firmware_rev.attr,
2067 &subsys_attr_subsysnqn.attr,
2068 NULL,
2069};
2070
2071static struct attribute_group nvme_subsys_attrs_group = {
2072 .attrs = nvme_subsys_attrs,
2073};
2074
2075static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2076 &nvme_subsys_attrs_group,
2077 NULL,
2078};
2079
Israel Rukshinb837b282018-01-04 17:56:14 +02002080static int nvme_active_ctrls(struct nvme_subsystem *subsys)
2081{
2082 int count = 0;
2083 struct nvme_ctrl *ctrl;
2084
2085 mutex_lock(&subsys->lock);
2086 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2087 if (ctrl->state != NVME_CTRL_DELETING &&
2088 ctrl->state != NVME_CTRL_DEAD)
2089 count++;
2090 }
2091 mutex_unlock(&subsys->lock);
2092
2093 return count;
2094}
2095
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002096static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2097{
2098 struct nvme_subsystem *subsys, *found;
2099 int ret;
2100
2101 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2102 if (!subsys)
2103 return -ENOMEM;
2104 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
2105 if (ret < 0) {
2106 kfree(subsys);
2107 return ret;
2108 }
2109 subsys->instance = ret;
2110 mutex_init(&subsys->lock);
2111 kref_init(&subsys->ref);
2112 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002113 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002114 nvme_init_subnqn(subsys, ctrl, id);
2115 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2116 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2117 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2118 subsys->vendor_id = le16_to_cpu(id->vid);
2119 subsys->cmic = id->cmic;
2120
2121 subsys->dev.class = nvme_subsys_class;
2122 subsys->dev.release = nvme_release_subsystem;
Hannes Reinecke1e496932017-11-10 10:58:23 +01002123 subsys->dev.groups = nvme_subsys_attrs_groups;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002124 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
2125 device_initialize(&subsys->dev);
2126
2127 mutex_lock(&nvme_subsystems_lock);
2128 found = __nvme_find_get_subsystem(subsys->subnqn);
2129 if (found) {
2130 /*
2131 * Verify that the subsystem actually supports multiple
2132 * controllers, else bail out.
2133 */
Israel Rukshinb837b282018-01-04 17:56:14 +02002134 if (nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002135 dev_err(ctrl->device,
2136 "ignoring ctrl due to duplicate subnqn (%s).\n",
2137 found->subnqn);
2138 nvme_put_subsystem(found);
2139 ret = -EINVAL;
2140 goto out_unlock;
2141 }
2142
2143 __nvme_release_subsystem(subsys);
2144 subsys = found;
2145 } else {
2146 ret = device_add(&subsys->dev);
2147 if (ret) {
2148 dev_err(ctrl->device,
2149 "failed to register subsystem device.\n");
2150 goto out_unlock;
2151 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002152 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002153 list_add_tail(&subsys->entry, &nvme_subsystems);
2154 }
2155
2156 ctrl->subsys = subsys;
2157 mutex_unlock(&nvme_subsystems_lock);
2158
2159 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2160 dev_name(ctrl->device))) {
2161 dev_err(ctrl->device,
2162 "failed to create sysfs link from subsystem.\n");
2163 /* the transport driver will eventually put the subsystem */
2164 return -EINVAL;
2165 }
2166
2167 mutex_lock(&subsys->lock);
2168 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2169 mutex_unlock(&subsys->lock);
2170
2171 return 0;
2172
2173out_unlock:
2174 mutex_unlock(&nvme_subsystems_lock);
2175 put_device(&subsys->dev);
2176 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02002177}
2178
Keith Buschc627c482017-11-07 10:28:31 -07002179static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
2180 size_t size)
2181{
2182 struct nvme_command c = { };
2183
2184 c.common.opcode = nvme_admin_get_log_page;
2185 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
2186 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
2187
2188 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2189}
2190
Keith Busch84fef622017-11-07 10:28:32 -07002191static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2192{
2193 int ret;
2194
2195 if (!ctrl->effects)
2196 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2197
2198 if (!ctrl->effects)
2199 return 0;
2200
2201 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2202 sizeof(*ctrl->effects));
2203 if (ret) {
2204 kfree(ctrl->effects);
2205 ctrl->effects = NULL;
2206 }
2207 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002208}
2209
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002210/*
2211 * Initialize the cached copies of the Identify data and various controller
2212 * register in our nvme_ctrl structure. This should be called as soon as
2213 * the admin queue is fully up and running.
2214 */
2215int nvme_init_identify(struct nvme_ctrl *ctrl)
2216{
2217 struct nvme_id_ctrl *id;
2218 u64 cap;
2219 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002220 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002221 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002222
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002223 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2224 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002225 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002226 return ret;
2227 }
2228
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002229 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2230 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002231 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002232 return ret;
2233 }
2234 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2235
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002236 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002237 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2238
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002239 ret = nvme_identify_ctrl(ctrl, &id);
2240 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002241 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002242 return -EIO;
2243 }
2244
Keith Busch84fef622017-11-07 10:28:32 -07002245 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2246 ret = nvme_get_effects_log(ctrl);
2247 if (ret < 0)
2248 return ret;
2249 }
Christoph Hellwig180de0072017-06-26 12:39:02 +02002250
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002251 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002252 int i;
2253
2254 ret = nvme_init_subsystem(ctrl, id);
2255 if (ret)
2256 goto out_free;
2257
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002258 /*
2259 * Check for quirks. Quirk can depend on firmware version,
2260 * so, in principle, the set of quirks present can change
2261 * across a reset. As a possible future enhancement, we
2262 * could re-scan for quirks every time we reinitialize
2263 * the device, but we'd have to make sure that the driver
2264 * behaves intelligently if the quirks change.
2265 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002266 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2267 if (quirk_matches(id, &core_quirks[i]))
2268 ctrl->quirks |= core_quirks[i].quirks;
2269 }
2270 }
2271
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002272 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002273 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 -07002274 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2275 }
2276
Scott Bauer8a9ae522017-02-17 13:59:40 +01002277 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002278 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002279 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002280 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002281 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002282 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002283 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002284 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002285 max_hw_sectors = UINT_MAX;
2286 ctrl->max_hw_sectors =
2287 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002288
Christoph Hellwigda358252016-03-02 18:07:11 +01002289 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002290 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002291 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002292
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002293 if (id->rtd3e) {
2294 /* us -> s */
2295 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2296
2297 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2298 shutdown_timeout, 60);
2299
2300 if (ctrl->shutdown_timeout != shutdown_timeout)
Max Gurtovoy1a3838d2017-12-31 15:33:27 +02002301 dev_info(ctrl->device,
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002302 "Shutdown timeout set to %u seconds\n",
2303 ctrl->shutdown_timeout);
2304 } else
2305 ctrl->shutdown_timeout = shutdown_timeout;
2306
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002307 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002308 ctrl->apsta = id->apsta;
2309 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002310 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2311 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002312 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 -04002313 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002314 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002315 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002316 }
2317 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002318 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002319 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002320 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2321
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002322 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002323 ctrl->icdoff = le16_to_cpu(id->icdoff);
2324 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2325 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2326 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2327
2328 /*
2329 * In fabrics we need to verify the cntlid matches the
2330 * admin connect
2331 */
Keith Busch634b8322017-08-10 11:23:31 +02002332 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002333 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002334 goto out_free;
2335 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002336
2337 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002338 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002339 "keep-alive support is mandatory for fabrics\n");
2340 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002341 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002342 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002343 } else {
2344 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002345 ctrl->hmpre = le32_to_cpu(id->hmpre);
2346 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002347 ctrl->hmminds = le32_to_cpu(id->hmminds);
2348 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002349 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002350
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002351 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002352
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002353 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002354 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002355 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002356 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2357
Keith Busch634b8322017-08-10 11:23:31 +02002358 ret = nvme_configure_apst(ctrl);
2359 if (ret < 0)
2360 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002361
2362 ret = nvme_configure_timestamp(ctrl);
2363 if (ret < 0)
2364 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002365
2366 ret = nvme_configure_directives(ctrl);
2367 if (ret < 0)
2368 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002369
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002370 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002371
Keith Busch634b8322017-08-10 11:23:31 +02002372 return 0;
2373
2374out_free:
2375 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002376 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002377}
Ming Lin576d55d2016-02-10 10:03:32 -08002378EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002379
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002380static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig21d34712015-11-26 09:08:36 +01002381{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002382 struct nvme_ctrl *ctrl =
2383 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig21d34712015-11-26 09:08:36 +01002384
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002385 switch (ctrl->state) {
2386 case NVME_CTRL_LIVE:
2387 case NVME_CTRL_ADMIN_ONLY:
2388 break;
2389 default:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002390 return -EWOULDBLOCK;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002391 }
2392
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002393 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002394 return 0;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002395}
2396
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002397static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2398{
2399 struct nvme_ns *ns;
2400 int ret;
2401
2402 mutex_lock(&ctrl->namespaces_mutex);
2403 if (list_empty(&ctrl->namespaces)) {
2404 ret = -ENOTTY;
2405 goto out_unlock;
2406 }
2407
2408 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2409 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002410 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002411 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2412 ret = -EINVAL;
2413 goto out_unlock;
2414 }
2415
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002416 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002417 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2418 kref_get(&ns->kref);
2419 mutex_unlock(&ctrl->namespaces_mutex);
2420
2421 ret = nvme_user_cmd(ctrl, ns, argp);
2422 nvme_put_ns(ns);
2423 return ret;
2424
2425out_unlock:
2426 mutex_unlock(&ctrl->namespaces_mutex);
2427 return ret;
2428}
2429
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002430static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2431 unsigned long arg)
2432{
2433 struct nvme_ctrl *ctrl = file->private_data;
2434 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002435
2436 switch (cmd) {
2437 case NVME_IOCTL_ADMIN_CMD:
2438 return nvme_user_cmd(ctrl, NULL, argp);
2439 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002440 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002441 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002442 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002443 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002444 case NVME_IOCTL_SUBSYS_RESET:
2445 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002446 case NVME_IOCTL_RESCAN:
2447 nvme_queue_scan(ctrl);
2448 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002449 default:
2450 return -ENOTTY;
2451 }
2452}
2453
2454static const struct file_operations nvme_dev_fops = {
2455 .owner = THIS_MODULE,
2456 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002457 .unlocked_ioctl = nvme_dev_ioctl,
2458 .compat_ioctl = nvme_dev_ioctl,
2459};
2460
2461static ssize_t nvme_sysfs_reset(struct device *dev,
2462 struct device_attribute *attr, const char *buf,
2463 size_t count)
2464{
2465 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2466 int ret;
2467
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002468 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002469 if (ret < 0)
2470 return ret;
2471 return count;
2472}
2473static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2474
Keith Busch9ec3bb22016-04-29 15:45:18 -06002475static ssize_t nvme_sysfs_rescan(struct device *dev,
2476 struct device_attribute *attr, const char *buf,
2477 size_t count)
2478{
2479 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2480
2481 nvme_queue_scan(ctrl);
2482 return count;
2483}
2484static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2485
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002486static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
Keith Busch118472a2016-02-18 09:57:48 -07002487{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002488 struct gendisk *disk = dev_to_disk(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002489
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002490 if (disk->fops == &nvme_fops)
2491 return nvme_get_ns_from_dev(dev)->head;
2492 else
2493 return disk->private_data;
2494}
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002495
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002496static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2497 char *buf)
2498{
2499 struct nvme_ns_head *head = dev_to_ns_head(dev);
2500 struct nvme_ns_ids *ids = &head->ids;
2501 struct nvme_subsystem *subsys = head->subsys;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002502 int serial_len = sizeof(subsys->serial);
2503 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002504
Christoph Hellwig002fab02017-11-09 13:50:16 +01002505 if (!uuid_is_null(&ids->uuid))
2506 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Keith Busch118472a2016-02-18 09:57:48 -07002507
Christoph Hellwig002fab02017-11-09 13:50:16 +01002508 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2509 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002510
Christoph Hellwig002fab02017-11-09 13:50:16 +01002511 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2512 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002513
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002514 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2515 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002516 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002517 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2518 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002519 model_len--;
2520
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002521 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2522 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002523 head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002524}
2525static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2526
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002527static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002528 char *buf)
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002529{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002530 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002531}
2532static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2533
Keith Busch2b9b6e82015-12-22 10:10:45 -07002534static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002535 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002536{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002537 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002538
2539 /* For backward compatibility expose the NGUID to userspace if
2540 * we have no UUID set
2541 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002542 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002543 printk_ratelimited(KERN_WARNING
2544 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002545 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002546 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002547 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002548}
2549static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2550
2551static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002552 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002553{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002554 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002555}
2556static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2557
2558static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002559 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002560{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002561 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002562}
2563static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2564
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002565static struct attribute *nvme_ns_id_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002566 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002567 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002568 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002569 &dev_attr_eui.attr,
2570 &dev_attr_nsid.attr,
2571 NULL,
2572};
2573
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002574static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002575 struct attribute *a, int n)
2576{
2577 struct device *dev = container_of(kobj, struct device, kobj);
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002578 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002579
2580 if (a == &dev_attr_uuid.attr) {
Martin Wilcka04b5de2017-09-28 21:33:23 +02002581 if (uuid_is_null(&ids->uuid) &&
Christoph Hellwig002fab02017-11-09 13:50:16 +01002582 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002583 return 0;
2584 }
2585 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002586 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002587 return 0;
2588 }
2589 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002590 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002591 return 0;
2592 }
2593 return a->mode;
2594}
2595
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002596const struct attribute_group nvme_ns_id_attr_group = {
2597 .attrs = nvme_ns_id_attrs,
2598 .is_visible = nvme_ns_id_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002599};
2600
Ming Lin931e1c22016-02-26 13:24:19 -08002601#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002602static ssize_t field##_show(struct device *dev, \
2603 struct device_attribute *attr, char *buf) \
2604{ \
2605 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002606 return sprintf(buf, "%.*s\n", \
2607 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002608} \
2609static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2610
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002611nvme_show_str_function(model);
2612nvme_show_str_function(serial);
2613nvme_show_str_function(firmware_rev);
2614
Ming Lin931e1c22016-02-26 13:24:19 -08002615#define nvme_show_int_function(field) \
2616static ssize_t field##_show(struct device *dev, \
2617 struct device_attribute *attr, char *buf) \
2618{ \
2619 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2620 return sprintf(buf, "%d\n", ctrl->field); \
2621} \
2622static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2623
Ming Lin931e1c22016-02-26 13:24:19 -08002624nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002625
Ming Lin1a353d82016-06-13 16:45:24 +02002626static ssize_t nvme_sysfs_delete(struct device *dev,
2627 struct device_attribute *attr, const char *buf,
2628 size_t count)
2629{
2630 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2631
2632 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002633 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002634 return count;
2635}
2636static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2637
2638static ssize_t nvme_sysfs_show_transport(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2641{
2642 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2643
2644 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2645}
2646static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2647
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002648static ssize_t nvme_sysfs_show_state(struct device *dev,
2649 struct device_attribute *attr,
2650 char *buf)
2651{
2652 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2653 static const char *const state_name[] = {
2654 [NVME_CTRL_NEW] = "new",
2655 [NVME_CTRL_LIVE] = "live",
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002656 [NVME_CTRL_ADMIN_ONLY] = "only-admin",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002657 [NVME_CTRL_RESETTING] = "resetting",
2658 [NVME_CTRL_RECONNECTING]= "reconnecting",
2659 [NVME_CTRL_DELETING] = "deleting",
2660 [NVME_CTRL_DEAD] = "dead",
2661 };
2662
2663 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2664 state_name[ctrl->state])
2665 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2666
2667 return sprintf(buf, "unknown state\n");
2668}
2669
2670static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2671
Ming Lin1a353d82016-06-13 16:45:24 +02002672static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2673 struct device_attribute *attr,
2674 char *buf)
2675{
2676 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2677
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002678 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002679}
2680static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2681
2682static ssize_t nvme_sysfs_show_address(struct device *dev,
2683 struct device_attribute *attr,
2684 char *buf)
2685{
2686 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2687
2688 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2689}
2690static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2691
Keith Busch779ff7562016-01-12 15:09:31 -07002692static struct attribute *nvme_dev_attrs[] = {
2693 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002694 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002695 &dev_attr_model.attr,
2696 &dev_attr_serial.attr,
2697 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002698 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002699 &dev_attr_delete_controller.attr,
2700 &dev_attr_transport.attr,
2701 &dev_attr_subsysnqn.attr,
2702 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002703 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002704 NULL
2705};
2706
Ming Lin1a353d82016-06-13 16:45:24 +02002707static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2708 struct attribute *a, int n)
2709{
2710 struct device *dev = container_of(kobj, struct device, kobj);
2711 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2712
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002713 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2714 return 0;
2715 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2716 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002717
2718 return a->mode;
2719}
2720
Keith Busch779ff7562016-01-12 15:09:31 -07002721static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002722 .attrs = nvme_dev_attrs,
2723 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002724};
2725
2726static const struct attribute_group *nvme_dev_attr_groups[] = {
2727 &nvme_dev_attrs_group,
2728 NULL,
2729};
2730
Christoph Hellwiged754e52017-11-09 13:50:43 +01002731static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2732 unsigned nsid)
2733{
2734 struct nvme_ns_head *h;
2735
2736 lockdep_assert_held(&subsys->lock);
2737
2738 list_for_each_entry(h, &subsys->nsheads, entry) {
2739 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2740 return h;
2741 }
2742
2743 return NULL;
2744}
2745
2746static int __nvme_check_ids(struct nvme_subsystem *subsys,
2747 struct nvme_ns_head *new)
2748{
2749 struct nvme_ns_head *h;
2750
2751 lockdep_assert_held(&subsys->lock);
2752
2753 list_for_each_entry(h, &subsys->nsheads, entry) {
2754 if (nvme_ns_ids_valid(&new->ids) &&
2755 nvme_ns_ids_equal(&new->ids, &h->ids))
2756 return -EINVAL;
2757 }
2758
2759 return 0;
2760}
2761
2762static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2763 unsigned nsid, struct nvme_id_ns *id)
2764{
2765 struct nvme_ns_head *head;
2766 int ret = -ENOMEM;
2767
2768 head = kzalloc(sizeof(*head), GFP_KERNEL);
2769 if (!head)
2770 goto out;
2771 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2772 if (ret < 0)
2773 goto out_free_head;
2774 head->instance = ret;
2775 INIT_LIST_HEAD(&head->list);
2776 init_srcu_struct(&head->srcu);
2777 head->subsys = ctrl->subsys;
2778 head->ns_id = nsid;
2779 kref_init(&head->ref);
2780
2781 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2782
2783 ret = __nvme_check_ids(ctrl->subsys, head);
2784 if (ret) {
2785 dev_err(ctrl->device,
2786 "duplicate IDs for nsid %d\n", nsid);
2787 goto out_cleanup_srcu;
2788 }
2789
Christoph Hellwig32acab32017-11-02 12:59:30 +01002790 ret = nvme_mpath_alloc_disk(ctrl, head);
2791 if (ret)
2792 goto out_cleanup_srcu;
2793
Christoph Hellwiged754e52017-11-09 13:50:43 +01002794 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
2795 return head;
2796out_cleanup_srcu:
2797 cleanup_srcu_struct(&head->srcu);
2798 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2799out_free_head:
2800 kfree(head);
2801out:
2802 return ERR_PTR(ret);
2803}
2804
2805static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
2806 struct nvme_id_ns *id, bool *new)
2807{
2808 struct nvme_ctrl *ctrl = ns->ctrl;
2809 bool is_shared = id->nmic & (1 << 0);
2810 struct nvme_ns_head *head = NULL;
2811 int ret = 0;
2812
2813 mutex_lock(&ctrl->subsys->lock);
2814 if (is_shared)
2815 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2816 if (!head) {
2817 head = nvme_alloc_ns_head(ctrl, nsid, id);
2818 if (IS_ERR(head)) {
2819 ret = PTR_ERR(head);
2820 goto out_unlock;
2821 }
2822
2823 *new = true;
2824 } else {
2825 struct nvme_ns_ids ids;
2826
2827 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2828 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2829 dev_err(ctrl->device,
2830 "IDs don't match for shared namespace %d\n",
2831 nsid);
2832 ret = -EINVAL;
2833 goto out_unlock;
2834 }
2835
2836 *new = false;
2837 }
2838
2839 list_add_tail(&ns->siblings, &head->list);
2840 ns->head = head;
2841
2842out_unlock:
2843 mutex_unlock(&ctrl->subsys->lock);
2844 return ret;
2845}
2846
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002847static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2848{
2849 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2850 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2851
Christoph Hellwiged754e52017-11-09 13:50:43 +01002852 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002853}
2854
Keith Busch32f0c4a2016-07-13 11:45:02 -06002855static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002856{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002857 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002858
Keith Busch32f0c4a2016-07-13 11:45:02 -06002859 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002860 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002861 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002862 if (!kref_get_unless_zero(&ns->kref))
2863 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002864 ret = ns;
2865 break;
2866 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002867 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002868 break;
2869 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002870 mutex_unlock(&ctrl->namespaces_mutex);
2871 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002872}
2873
Jens Axboef5d11842017-06-27 12:03:06 -06002874static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2875{
2876 struct streams_directive_params s;
2877 int ret;
2878
2879 if (!ctrl->nr_streams)
2880 return 0;
2881
Christoph Hellwiged754e52017-11-09 13:50:43 +01002882 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002883 if (ret)
2884 return ret;
2885
2886 ns->sws = le32_to_cpu(s.sws);
2887 ns->sgs = le16_to_cpu(s.sgs);
2888
2889 if (ns->sws) {
2890 unsigned int bs = 1 << ns->lba_shift;
2891
2892 blk_queue_io_min(ns->queue, bs * ns->sws);
2893 if (ns->sgs)
2894 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2895 }
2896
2897 return 0;
2898}
2899
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002900static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2901{
2902 struct nvme_ns *ns;
2903 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002904 struct nvme_id_ns *id;
2905 char disk_name[DISK_NAME_LEN];
Christoph Hellwig32acab32017-11-02 12:59:30 +01002906 int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
Christoph Hellwiged754e52017-11-09 13:50:43 +01002907 bool new = true;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002908
2909 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2910 if (!ns)
2911 return;
2912
2913 ns->queue = blk_mq_init_queue(ctrl->tagset);
2914 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002915 goto out_free_ns;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002916 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2917 ns->queue->queuedata = ns;
2918 ns->ctrl = ctrl;
2919
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002920 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002921 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002922
2923 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002924 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002925 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002926
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002927 id = nvme_identify_ns(ctrl, nsid);
2928 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002929 goto out_free_queue;
2930
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002931 if (id->ncap == 0)
2932 goto out_free_id;
2933
Christoph Hellwiged754e52017-11-09 13:50:43 +01002934 if (nvme_init_ns_head(ns, nsid, id, &new))
2935 goto out_free_id;
2936
Christoph Hellwig32acab32017-11-02 12:59:30 +01002937#ifdef CONFIG_NVME_MULTIPATH
2938 /*
2939 * If multipathing is enabled we need to always use the subsystem
2940 * instance number for numbering our devices to avoid conflicts
2941 * between subsystems that have multiple controllers and thus use
2942 * the multipath-aware subsystem node and those that have a single
2943 * controller and use the controller node directly.
2944 */
2945 if (ns->head->disk) {
2946 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
2947 ctrl->cntlid, ns->head->instance);
2948 flags = GENHD_FL_HIDDEN;
2949 } else {
2950 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
2951 ns->head->instance);
2952 }
2953#else
2954 /*
2955 * But without the multipath code enabled, multiple controller per
2956 * subsystems are visible as devices and thus we cannot use the
2957 * subsystem instance.
2958 */
Christoph Hellwiged754e52017-11-09 13:50:43 +01002959 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
Christoph Hellwig32acab32017-11-02 12:59:30 +01002960#endif
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002961
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002962 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2963 if (nvme_nvm_register(ns, disk_name, node)) {
2964 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01002965 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002966 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002967 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002968
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002969 disk = alloc_disk_node(0, node);
2970 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002971 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002972
2973 disk->fops = &nvme_fops;
2974 disk->private_data = ns;
2975 disk->queue = ns->queue;
Christoph Hellwig32acab32017-11-02 12:59:30 +01002976 disk->flags = flags;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002977 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2978 ns->disk = disk;
2979
2980 __nvme_revalidate_disk(disk, id);
2981
Keith Busch32f0c4a2016-07-13 11:45:02 -06002982 mutex_lock(&ctrl->namespaces_mutex);
2983 list_add_tail(&ns->list, &ctrl->namespaces);
2984 mutex_unlock(&ctrl->namespaces_mutex);
2985
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002986 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002987
2988 kfree(id);
2989
Dan Williams0d52c7562016-06-15 19:44:20 -07002990 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002991 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002992 &nvme_ns_id_attr_group))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002993 pr_warn("%s: failed to create sysfs group for identification\n",
2994 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002995 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2996 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2997 ns->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +01002998
2999 if (new)
3000 nvme_mpath_add_disk(ns->head);
Hannes Reineckee9a48032017-11-09 17:57:06 +01003001 nvme_mpath_add_disk_links(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003002 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01003003 out_unlink_ns:
3004 mutex_lock(&ctrl->subsys->lock);
3005 list_del_rcu(&ns->siblings);
3006 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003007 out_free_id:
3008 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003009 out_free_queue:
3010 blk_cleanup_queue(ns->queue);
3011 out_free_ns:
3012 kfree(ns);
3013}
3014
3015static void nvme_ns_remove(struct nvme_ns *ns)
3016{
Keith Busch646017a2016-02-24 09:15:54 -07003017 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3018 return;
3019
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003020 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003021 if (blk_get_integrity(ns->disk))
3022 blk_integrity_unregister(ns->disk);
Hannes Reineckee9a48032017-11-09 17:57:06 +01003023 nvme_mpath_remove_disk_links(ns);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003024 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003025 &nvme_ns_id_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003026 if (ns->ndev)
3027 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003028 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003029 blk_cleanup_queue(ns->queue);
3030 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003031
Christoph Hellwiged754e52017-11-09 13:50:43 +01003032 mutex_lock(&ns->ctrl->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003033 nvme_mpath_clear_current_path(ns);
Keith Busch9941a862017-11-16 13:36:50 -07003034 list_del_rcu(&ns->siblings);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003035 mutex_unlock(&ns->ctrl->subsys->lock);
3036
Keith Busch32f0c4a2016-07-13 11:45:02 -06003037 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003038 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003039 mutex_unlock(&ns->ctrl->namespaces_mutex);
3040
Keith Busch9941a862017-11-16 13:36:50 -07003041 synchronize_srcu(&ns->head->srcu);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003042 nvme_put_ns(ns);
3043}
3044
Keith Busch540c8012015-10-22 15:45:06 -06003045static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3046{
3047 struct nvme_ns *ns;
3048
Keith Busch32f0c4a2016-07-13 11:45:02 -06003049 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06003050 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003051 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06003052 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003053 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06003054 } else
3055 nvme_alloc_ns(ctrl, nsid);
3056}
3057
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303058static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3059 unsigned nsid)
3060{
3061 struct nvme_ns *ns, *next;
3062
3063 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01003064 if (ns->head->ns_id > nsid)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303065 nvme_ns_remove(ns);
3066 }
3067}
3068
Keith Busch540c8012015-10-22 15:45:06 -06003069static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3070{
3071 struct nvme_ns *ns;
3072 __le32 *ns_list;
3073 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
3074 int ret = 0;
3075
3076 ns_list = kzalloc(0x1000, GFP_KERNEL);
3077 if (!ns_list)
3078 return -ENOMEM;
3079
3080 for (i = 0; i < num_lists; i++) {
3081 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3082 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303083 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06003084
3085 for (j = 0; j < min(nn, 1024U); j++) {
3086 nsid = le32_to_cpu(ns_list[j]);
3087 if (!nsid)
3088 goto out;
3089
3090 nvme_validate_ns(ctrl, nsid);
3091
3092 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06003093 ns = nvme_find_get_ns(ctrl, prev);
3094 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06003095 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003096 nvme_put_ns(ns);
3097 }
Keith Busch540c8012015-10-22 15:45:06 -06003098 }
3099 }
3100 nn -= j;
3101 }
3102 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303103 nvme_remove_invalid_namespaces(ctrl, prev);
3104 free:
Keith Busch540c8012015-10-22 15:45:06 -06003105 kfree(ns_list);
3106 return ret;
3107}
3108
Christoph Hellwig5955be22016-04-26 13:51:59 +02003109static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003110{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003111 unsigned i;
3112
Keith Busch540c8012015-10-22 15:45:06 -06003113 for (i = 1; i <= nn; i++)
3114 nvme_validate_ns(ctrl, i);
3115
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303116 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003117}
3118
Christoph Hellwig5955be22016-04-26 13:51:59 +02003119static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003120{
Christoph Hellwig5955be22016-04-26 13:51:59 +02003121 struct nvme_ctrl *ctrl =
3122 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003123 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06003124 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003125
Christoph Hellwig5955be22016-04-26 13:51:59 +02003126 if (ctrl->state != NVME_CTRL_LIVE)
3127 return;
3128
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003129 WARN_ON_ONCE(!ctrl->tagset);
3130
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003131 if (nvme_identify_ctrl(ctrl, &id))
3132 return;
Keith Busch540c8012015-10-22 15:45:06 -06003133
3134 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06003135 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06003136 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3137 if (!nvme_scan_ns_list(ctrl, nn))
3138 goto done;
3139 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02003140 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06003141 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06003142 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06003143 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01003144 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003145 kfree(id);
3146}
Christoph Hellwig5955be22016-04-26 13:51:59 +02003147
3148void nvme_queue_scan(struct nvme_ctrl *ctrl)
3149{
3150 /*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003151 * Only new queue scan work when admin and IO queues are both alive
Christoph Hellwig5955be22016-04-26 13:51:59 +02003152 */
3153 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003154 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003155}
3156EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003157
Keith Busch32f0c4a2016-07-13 11:45:02 -06003158/*
3159 * This function iterates the namespace list unlocked to allow recovery from
3160 * controller failure. It is up to the caller to ensure the namespace list is
3161 * not modified by scan work while this function is executing.
3162 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003163void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3164{
3165 struct nvme_ns *ns, *next;
3166
Keith Busch0ff9d4e2016-05-12 08:37:14 -06003167 /*
3168 * The dead states indicates the controller was not gracefully
3169 * disconnected. In that case, we won't be able to flush any data while
3170 * removing the namespaces' disks; fail all the queues now to avoid
3171 * potentially having to clean up the failed sync later.
3172 */
3173 if (ctrl->state == NVME_CTRL_DEAD)
3174 nvme_kill_queues(ctrl);
3175
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003176 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
3177 nvme_ns_remove(ns);
3178}
Ming Lin576d55d2016-02-10 10:03:32 -08003179EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003180
Keith Busche3d78742017-11-07 15:13:14 -07003181static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3182{
3183 char *envp[2] = { NULL, NULL };
3184 u32 aen_result = ctrl->aen_result;
3185
3186 ctrl->aen_result = 0;
3187 if (!aen_result)
3188 return;
3189
3190 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3191 if (!envp[0])
3192 return;
3193 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3194 kfree(envp[0]);
3195}
3196
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003197static void nvme_async_event_work(struct work_struct *work)
3198{
3199 struct nvme_ctrl *ctrl =
3200 container_of(work, struct nvme_ctrl, async_event_work);
3201
Keith Busche3d78742017-11-07 15:13:14 -07003202 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07003203 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003204}
3205
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303206static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3207{
3208
3209 u32 csts;
3210
3211 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3212 return false;
3213
3214 if (csts == ~0)
3215 return false;
3216
3217 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3218}
3219
3220static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3221{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303222 struct nvme_fw_slot_info_log *log;
3223
3224 log = kmalloc(sizeof(*log), GFP_KERNEL);
3225 if (!log)
3226 return;
3227
Keith Buschc627c482017-11-07 10:28:31 -07003228 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303229 dev_warn(ctrl->device,
3230 "Get FW SLOT INFO log error\n");
3231 kfree(log);
3232}
3233
3234static void nvme_fw_act_work(struct work_struct *work)
3235{
3236 struct nvme_ctrl *ctrl = container_of(work,
3237 struct nvme_ctrl, fw_act_work);
3238 unsigned long fw_act_timeout;
3239
3240 if (ctrl->mtfa)
3241 fw_act_timeout = jiffies +
3242 msecs_to_jiffies(ctrl->mtfa * 100);
3243 else
3244 fw_act_timeout = jiffies +
3245 msecs_to_jiffies(admin_timeout * 1000);
3246
3247 nvme_stop_queues(ctrl);
3248 while (nvme_ctrl_pp_status(ctrl)) {
3249 if (time_after(jiffies, fw_act_timeout)) {
3250 dev_warn(ctrl->device,
3251 "Fw activation timeout, reset controller\n");
3252 nvme_reset_ctrl(ctrl);
3253 break;
3254 }
3255 msleep(100);
3256 }
3257
3258 if (ctrl->state != NVME_CTRL_LIVE)
3259 return;
3260
3261 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003262 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303263 nvme_get_fw_slot_info(ctrl);
3264}
3265
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003266void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3267 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003268{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003269 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003270
Keith Buschad22c352017-11-07 15:13:12 -07003271 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003272 return;
3273
Keith Busche3d78742017-11-07 15:13:14 -07003274 switch (result & 0x7) {
3275 case NVME_AER_ERROR:
3276 case NVME_AER_SMART:
3277 case NVME_AER_CSS:
3278 case NVME_AER_VS:
3279 ctrl->aen_result = result;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003280 break;
3281 default:
3282 break;
3283 }
3284
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003285 switch (result & 0xff07) {
3286 case NVME_AER_NOTICE_NS_CHANGED:
3287 dev_info(ctrl->device, "rescanning\n");
3288 nvme_queue_scan(ctrl);
3289 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303290 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003291 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303292 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003293 default:
3294 dev_warn(ctrl->device, "async event result %08x\n", result);
3295 }
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003296 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003297}
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003298EXPORT_SYMBOL_GPL(nvme_complete_async_event);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003299
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003300void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003301{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003302 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003303 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003304 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303305 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003306}
3307EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003308
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003309void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3310{
3311 if (ctrl->kato)
3312 nvme_start_keep_alive(ctrl);
3313
3314 if (ctrl->queue_count > 1) {
3315 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003316 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003317 nvme_start_queues(ctrl);
3318 }
3319}
3320EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3321
3322void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3323{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003324 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003325}
Ming Lin576d55d2016-02-10 10:03:32 -08003326EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003327
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003328static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003329{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003330 struct nvme_ctrl *ctrl =
3331 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003332 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003333
Christoph Hellwig9843f682017-10-18 13:10:01 +02003334 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003335 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003336
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003337 if (subsys) {
3338 mutex_lock(&subsys->lock);
3339 list_del(&ctrl->subsys_entry);
3340 mutex_unlock(&subsys->lock);
3341 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3342 }
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003343
3344 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003345
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003346 if (subsys)
3347 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003348}
3349
3350/*
3351 * Initialize a NVMe controller structures. This needs to be called during
3352 * earliest initialization so that we have the initialized structured around
3353 * during probing.
3354 */
3355int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3356 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3357{
3358 int ret;
3359
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003360 ctrl->state = NVME_CTRL_NEW;
3361 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003362 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01003363 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003364 ctrl->dev = dev;
3365 ctrl->ops = ops;
3366 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003367 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003368 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303369 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003370 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003371
Christoph Hellwig9843f682017-10-18 13:10:01 +02003372 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3373 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003374 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003375 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003376
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003377 device_initialize(&ctrl->ctrl_device);
3378 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003379 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003380 ctrl->device->class = nvme_class;
3381 ctrl->device->parent = ctrl->dev;
3382 ctrl->device->groups = nvme_dev_attr_groups;
3383 ctrl->device->release = nvme_free_ctrl;
3384 dev_set_drvdata(ctrl->device, ctrl);
3385 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3386 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003387 goto out_release_instance;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003388
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003389 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3390 ctrl->cdev.owner = ops->module;
3391 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003392 if (ret)
3393 goto out_free_name;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003394
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003395 /*
3396 * Initialize latency tolerance controls. The sysfs files won't
3397 * be visible to userspace unless the device actually supports APST.
3398 */
3399 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3400 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3401 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3402
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003403 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003404out_free_name:
3405 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003406out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003407 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003408out:
3409 return ret;
3410}
Ming Lin576d55d2016-02-10 10:03:32 -08003411EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003412
Keith Busch69d9a992016-02-24 09:15:56 -07003413/**
3414 * nvme_kill_queues(): Ends all namespace queues
3415 * @ctrl: the dead controller that needs to end
3416 *
3417 * Call this function when the driver determines it is unable to get the
3418 * controller in a state capable of servicing IO.
3419 */
3420void nvme_kill_queues(struct nvme_ctrl *ctrl)
3421{
3422 struct nvme_ns *ns;
3423
Keith Busch32f0c4a2016-07-13 11:45:02 -06003424 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08003425
Ming Lei443bd902017-06-19 10:21:08 +08003426 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003427 if (ctrl->admin_q)
3428 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003429
Keith Busch32f0c4a2016-07-13 11:45:02 -06003430 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003431 /*
3432 * Revalidating a dead namespace sets capacity to 0. This will
3433 * end buffered writers dirtying pages that can't be synced.
3434 */
Keith Buschf33447b2017-02-10 18:15:51 -05003435 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3436 continue;
3437 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003438 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003439
Ming Lei443bd902017-06-19 10:21:08 +08003440 /* Forcibly unquiesce queues to avoid blocking dispatch */
3441 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003442 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003443 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07003444}
Linus Torvalds237045f2016-03-18 17:13:31 -07003445EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003446
Keith Busch302ad8c2017-03-01 14:22:12 -05003447void nvme_unfreeze(struct nvme_ctrl *ctrl)
3448{
3449 struct nvme_ns *ns;
3450
3451 mutex_lock(&ctrl->namespaces_mutex);
3452 list_for_each_entry(ns, &ctrl->namespaces, list)
3453 blk_mq_unfreeze_queue(ns->queue);
3454 mutex_unlock(&ctrl->namespaces_mutex);
3455}
3456EXPORT_SYMBOL_GPL(nvme_unfreeze);
3457
3458void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3459{
3460 struct nvme_ns *ns;
3461
3462 mutex_lock(&ctrl->namespaces_mutex);
3463 list_for_each_entry(ns, &ctrl->namespaces, list) {
3464 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3465 if (timeout <= 0)
3466 break;
3467 }
3468 mutex_unlock(&ctrl->namespaces_mutex);
3469}
3470EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3471
3472void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3473{
3474 struct nvme_ns *ns;
3475
3476 mutex_lock(&ctrl->namespaces_mutex);
3477 list_for_each_entry(ns, &ctrl->namespaces, list)
3478 blk_mq_freeze_queue_wait(ns->queue);
3479 mutex_unlock(&ctrl->namespaces_mutex);
3480}
3481EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3482
3483void nvme_start_freeze(struct nvme_ctrl *ctrl)
3484{
3485 struct nvme_ns *ns;
3486
3487 mutex_lock(&ctrl->namespaces_mutex);
3488 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003489 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05003490 mutex_unlock(&ctrl->namespaces_mutex);
3491}
3492EXPORT_SYMBOL_GPL(nvme_start_freeze);
3493
Keith Busch25646262016-01-04 09:10:57 -07003494void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003495{
3496 struct nvme_ns *ns;
3497
Keith Busch32f0c4a2016-07-13 11:45:02 -06003498 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003499 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003500 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003501 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003502}
Ming Lin576d55d2016-02-10 10:03:32 -08003503EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003504
Keith Busch25646262016-01-04 09:10:57 -07003505void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003506{
3507 struct nvme_ns *ns;
3508
Keith Busch32f0c4a2016-07-13 11:45:02 -06003509 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003510 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003511 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003512 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003513}
Ming Lin576d55d2016-02-10 10:03:32 -08003514EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003515
Sagi Grimberg31b84462017-10-11 12:53:07 +03003516int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3517{
3518 if (!ctrl->ops->reinit_request)
3519 return 0;
3520
3521 return blk_mq_tagset_iter(set, set->driver_data,
3522 ctrl->ops->reinit_request);
3523}
3524EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3525
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003526int __init nvme_core_init(void)
3527{
3528 int result;
3529
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003530 nvme_wq = alloc_workqueue("nvme-wq",
3531 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3532 if (!nvme_wq)
3533 return -ENOMEM;
3534
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003535 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003536 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003537 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003538
3539 nvme_class = class_create(THIS_MODULE, "nvme");
3540 if (IS_ERR(nvme_class)) {
3541 result = PTR_ERR(nvme_class);
3542 goto unregister_chrdev;
3543 }
3544
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003545 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3546 if (IS_ERR(nvme_subsys_class)) {
3547 result = PTR_ERR(nvme_subsys_class);
3548 goto destroy_class;
3549 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003550 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003551
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003552destroy_class:
3553 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003554unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003555 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003556destroy_wq:
3557 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003558 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003559}
3560
3561void nvme_core_exit(void)
3562{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003563 ida_destroy(&nvme_subsystems_ida);
3564 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003565 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003566 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003567 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003568}
Ming Lin576d55d2016-02-10 10:03:32 -08003569
3570MODULE_LICENSE("GPL");
3571MODULE_VERSION("1.0");
3572module_init(nvme_core_init);
3573module_exit(nvme_core_exit);