blob: 4ff5114f467d7f5663782645e73cd1e2e6901e1b [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 <scsi/sg.h>
31#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010032
33#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020034#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010035
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010036#define NVME_MINORS (1U << MINORBITS)
37
Ming Linba0ba7d2016-02-10 10:03:30 -080038unsigned char admin_timeout = 60;
39module_param(admin_timeout, byte, 0644);
40MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080041EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080042
43unsigned char nvme_io_timeout = 30;
44module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
45MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080046EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080047
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020048static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080049module_param(shutdown_timeout, byte, 0644);
50MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
51
Christoph Hellwig44e44b22017-04-05 19:18:11 +020052static u8 nvme_max_retries = 5;
53module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070054MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010055
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010056static int nvme_char_major;
57module_param(nvme_char_major, int, 0);
58
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080059static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080060module_param(default_ps_max_latency_us, ulong, 0644);
61MODULE_PARM_DESC(default_ps_max_latency_us,
62 "max power saving latency for new devices; use PM QOS to change per device");
63
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070064static bool force_apst;
65module_param(force_apst, bool, 0644);
66MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010071static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080072static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010073
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010074static struct class *nvme_class;
75
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020076int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
77{
78 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
79 return -EBUSY;
80 if (!queue_work(nvme_wq, &ctrl->reset_work))
81 return -EBUSY;
82 return 0;
83}
84EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
85
86static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
87{
88 int ret;
89
90 ret = nvme_reset_ctrl(ctrl);
91 if (!ret)
92 flush_work(&ctrl->reset_work);
93 return ret;
94}
95
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020096static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020097{
98 switch (nvme_req(req)->status & 0x7ff) {
99 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200100 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200101 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200102 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200103 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200104 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200105 case NVME_SC_WRITE_FAULT:
106 case NVME_SC_READ_ERROR:
107 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200108 return BLK_STS_MEDIUM;
109 default:
110 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200111 }
112}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200113
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200114static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200115{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200116 if (blk_noretry_request(req))
117 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200118 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200119 return false;
120 if (jiffies - req->start_time >= req->timeout)
121 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200122 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200123 return false;
124 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200125}
126
127void nvme_complete_rq(struct request *req)
128{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200129 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
130 nvme_req(req)->retries++;
131 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
132 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200133 }
134
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200135 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200136}
137EXPORT_SYMBOL_GPL(nvme_complete_rq);
138
Ming Linc55a2fd2016-05-18 14:05:02 -0700139void nvme_cancel_request(struct request *req, void *data, bool reserved)
140{
141 int status;
142
143 if (!blk_mq_request_started(req))
144 return;
145
146 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
147 "Cancelling I/O %d", req->tag);
148
149 status = NVME_SC_ABORT_REQ;
150 if (blk_queue_dying(req->q))
151 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200152 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200153 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200154
Ming Linc55a2fd2016-05-18 14:05:02 -0700155}
156EXPORT_SYMBOL_GPL(nvme_cancel_request);
157
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200158bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
159 enum nvme_ctrl_state new_state)
160{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300161 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200162 bool changed = false;
163
164 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300165
166 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200167 switch (new_state) {
168 case NVME_CTRL_LIVE:
169 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200170 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200171 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900172 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200173 changed = true;
174 /* FALLTHRU */
175 default:
176 break;
177 }
178 break;
179 case NVME_CTRL_RESETTING:
180 switch (old_state) {
181 case NVME_CTRL_NEW:
182 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900183 changed = true;
184 /* FALLTHRU */
185 default:
186 break;
187 }
188 break;
189 case NVME_CTRL_RECONNECTING:
190 switch (old_state) {
191 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200192 changed = true;
193 /* FALLTHRU */
194 default:
195 break;
196 }
197 break;
198 case NVME_CTRL_DELETING:
199 switch (old_state) {
200 case NVME_CTRL_LIVE:
201 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900202 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200203 changed = true;
204 /* FALLTHRU */
205 default:
206 break;
207 }
208 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600209 case NVME_CTRL_DEAD:
210 switch (old_state) {
211 case NVME_CTRL_DELETING:
212 changed = true;
213 /* FALLTHRU */
214 default:
215 break;
216 }
217 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200218 default:
219 break;
220 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200221
222 if (changed)
223 ctrl->state = new_state;
224
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300225 spin_unlock_irq(&ctrl->lock);
226
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200227 return changed;
228}
229EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
230
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100231static void nvme_free_ns(struct kref *kref)
232{
233 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
234
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200235 if (ns->ndev)
236 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100237
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200238 if (ns->disk) {
239 spin_lock(&dev_list_lock);
240 ns->disk->private_data = NULL;
241 spin_unlock(&dev_list_lock);
242 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100243
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100244 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700245 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
246 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100247 kfree(ns);
248}
249
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100250static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100251{
252 kref_put(&ns->kref, nvme_free_ns);
253}
254
255static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
256{
257 struct nvme_ns *ns;
258
259 spin_lock(&dev_list_lock);
260 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800261 if (ns) {
262 if (!kref_get_unless_zero(&ns->kref))
263 goto fail;
264 if (!try_module_get(ns->ctrl->ops->module))
265 goto fail_put_ns;
266 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100267 spin_unlock(&dev_list_lock);
268
269 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800270
271fail_put_ns:
272 kref_put(&ns->kref, nvme_free_ns);
273fail:
274 spin_unlock(&dev_list_lock);
275 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100276}
277
Christoph Hellwig41609822015-11-20 09:00:02 +0100278struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200279 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100280{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100281 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100282 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100283
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200284 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100285 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200286 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100287 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200288 qid ? qid - 1 : 0);
289 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100290 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100291 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100292
Christoph Hellwig21d34712015-11-26 09:08:36 +0100293 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800294 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100295
Christoph Hellwig41609822015-11-20 09:00:02 +0100296 return req;
297}
Ming Lin576d55d2016-02-10 10:03:32 -0800298EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100299
Ming Lin8093f7c2016-04-12 13:10:14 -0600300static inline void nvme_setup_flush(struct nvme_ns *ns,
301 struct nvme_command *cmnd)
302{
303 memset(cmnd, 0, sizeof(*cmnd));
304 cmnd->common.opcode = nvme_cmd_flush;
305 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
306}
307
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200308static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600309 struct nvme_command *cmnd)
310{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100311 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600312 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100313 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600314
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100315 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600316 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200317 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600318
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100319 __rq_for_each_bio(bio, req) {
320 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
321 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
322
323 range[n].cattr = cpu_to_le32(0);
324 range[n].nlb = cpu_to_le32(nlb);
325 range[n].slba = cpu_to_le64(slba);
326 n++;
327 }
328
329 if (WARN_ON_ONCE(n != segments)) {
330 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200331 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100332 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600333
334 memset(cmnd, 0, sizeof(*cmnd));
335 cmnd->dsm.opcode = nvme_cmd_dsm;
336 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200337 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600338 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
339
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700340 req->special_vec.bv_page = virt_to_page(range);
341 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100342 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700343 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600344
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200345 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600346}
Ming Lin8093f7c2016-04-12 13:10:14 -0600347
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200348static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
349 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600350{
351 u16 control = 0;
352 u32 dsmgmt = 0;
353
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200354 /*
355 * If formated with metadata, require the block layer provide a buffer
356 * unless this namespace is formated such that the metadata can be
357 * stripped/generated by the controller with PRACT=1.
358 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300359 if (ns && ns->ms &&
360 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200361 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
362 return BLK_STS_NOTSUPP;
363
Ming Lin8093f7c2016-04-12 13:10:14 -0600364 if (req->cmd_flags & REQ_FUA)
365 control |= NVME_RW_FUA;
366 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
367 control |= NVME_RW_LR;
368
369 if (req->cmd_flags & REQ_RAHEAD)
370 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
371
372 memset(cmnd, 0, sizeof(*cmnd));
373 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600374 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
375 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
376 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
377
378 if (ns->ms) {
379 switch (ns->pi_type) {
380 case NVME_NS_DPS_PI_TYPE3:
381 control |= NVME_RW_PRINFO_PRCHK_GUARD;
382 break;
383 case NVME_NS_DPS_PI_TYPE1:
384 case NVME_NS_DPS_PI_TYPE2:
385 control |= NVME_RW_PRINFO_PRCHK_GUARD |
386 NVME_RW_PRINFO_PRCHK_REF;
387 cmnd->rw.reftag = cpu_to_le32(
388 nvme_block_nr(ns, blk_rq_pos(req)));
389 break;
390 }
391 if (!blk_integrity_rq(req))
392 control |= NVME_RW_PRINFO_PRACT;
393 }
394
395 cmnd->rw.control = cpu_to_le16(control);
396 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200397 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600398}
399
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200400blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600401 struct nvme_command *cmd)
402{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200403 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600404
Christoph Hellwig987f6992017-04-05 19:18:08 +0200405 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200406 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200407 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200408 req->rq_flags |= RQF_DONTPREP;
409 }
410
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100411 switch (req_op(req)) {
412 case REQ_OP_DRV_IN:
413 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800414 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100415 break;
416 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600417 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100418 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200419 case REQ_OP_WRITE_ZEROES:
420 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100421 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600422 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100423 break;
424 case REQ_OP_READ:
425 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200426 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100427 break;
428 default:
429 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200430 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100431 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600432
James Smart721b3912016-10-21 23:33:34 +0300433 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600434 return ret;
435}
436EXPORT_SYMBOL_GPL(nvme_setup_cmd);
437
Christoph Hellwig41609822015-11-20 09:00:02 +0100438/*
439 * Returns 0 on success. If the result is negative, it's a Linux error code;
440 * if the result is positive, it's an NVM Express status code
441 */
442int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800443 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200444 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100445{
446 struct request *req;
447 int ret;
448
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200449 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100450 if (IS_ERR(req))
451 return PTR_ERR(req);
452
453 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
454
Christoph Hellwig21d34712015-11-26 09:08:36 +0100455 if (buffer && bufflen) {
456 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
457 if (ret)
458 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100459 }
460
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200461 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800462 if (result)
463 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200464 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
465 ret = -EINTR;
466 else
467 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100468 out:
469 blk_mq_free_request(req);
470 return ret;
471}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200472EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100473
474int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
475 void *buffer, unsigned bufflen)
476{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200477 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
478 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100479}
Ming Lin576d55d2016-02-10 10:03:32 -0800480EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100481
Keith Busch0b7f1f22015-10-23 09:47:28 -0600482int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
483 void __user *ubuffer, unsigned bufflen,
484 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
485 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100486{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200487 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600488 struct nvme_ns *ns = q->queuedata;
489 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100490 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600491 struct bio *bio = NULL;
492 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100493 int ret;
494
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200495 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100496 if (IS_ERR(req))
497 return PTR_ERR(req);
498
499 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
500
501 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100502 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
503 GFP_KERNEL);
504 if (ret)
505 goto out;
506 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100507
Keith Busch0b7f1f22015-10-23 09:47:28 -0600508 if (!disk)
509 goto submit;
510 bio->bi_bdev = bdget_disk(disk, 0);
511 if (!bio->bi_bdev) {
512 ret = -ENODEV;
513 goto out_unmap;
514 }
515
Keith Busche9fc63d2016-02-24 09:15:58 -0700516 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600517 struct bio_integrity_payload *bip;
518
519 meta = kmalloc(meta_len, GFP_KERNEL);
520 if (!meta) {
521 ret = -ENOMEM;
522 goto out_unmap;
523 }
524
525 if (write) {
526 if (copy_from_user(meta, meta_buffer,
527 meta_len)) {
528 ret = -EFAULT;
529 goto out_free_meta;
530 }
531 }
532
533 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700534 if (IS_ERR(bip)) {
535 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600536 goto out_free_meta;
537 }
538
539 bip->bip_iter.bi_size = meta_len;
540 bip->bip_iter.bi_sector = meta_seed;
541
542 ret = bio_integrity_add_page(bio, virt_to_page(meta),
543 meta_len, offset_in_page(meta));
544 if (ret != meta_len) {
545 ret = -ENOMEM;
546 goto out_free_meta;
547 }
548 }
549 }
550 submit:
551 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200552 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
553 ret = -EINTR;
554 else
555 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100556 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800557 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600558 if (meta && !ret && !write) {
559 if (copy_to_user(meta_buffer, meta, meta_len))
560 ret = -EFAULT;
561 }
562 out_free_meta:
563 kfree(meta);
564 out_unmap:
565 if (bio) {
566 if (disk && bio->bi_bdev)
567 bdput(bio->bi_bdev);
568 blk_rq_unmap_user(bio);
569 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100570 out:
571 blk_mq_free_request(req);
572 return ret;
573}
574
Keith Busch0b7f1f22015-10-23 09:47:28 -0600575int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
576 void __user *ubuffer, unsigned bufflen, u32 *result,
577 unsigned timeout)
578{
579 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
580 result, timeout);
581}
582
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200583static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200584{
585 struct nvme_ctrl *ctrl = rq->end_io_data;
586
587 blk_mq_free_request(rq);
588
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200589 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200590 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200591 "failed nvme_keep_alive_end_io error=%d\n",
592 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200593 return;
594 }
595
596 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
597}
598
599static int nvme_keep_alive(struct nvme_ctrl *ctrl)
600{
601 struct nvme_command c;
602 struct request *rq;
603
604 memset(&c, 0, sizeof(c));
605 c.common.opcode = nvme_admin_keep_alive;
606
607 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
608 NVME_QID_ANY);
609 if (IS_ERR(rq))
610 return PTR_ERR(rq);
611
612 rq->timeout = ctrl->kato * HZ;
613 rq->end_io_data = ctrl;
614
615 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
616
617 return 0;
618}
619
620static void nvme_keep_alive_work(struct work_struct *work)
621{
622 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
623 struct nvme_ctrl, ka_work);
624
625 if (nvme_keep_alive(ctrl)) {
626 /* allocation failure, reset the controller */
627 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200628 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200629 return;
630 }
631}
632
633void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
634{
635 if (unlikely(ctrl->kato == 0))
636 return;
637
638 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
639 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
640}
641EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
642
643void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
644{
645 if (unlikely(ctrl->kato == 0))
646 return;
647
648 cancel_delayed_work_sync(&ctrl->ka_work);
649}
650EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
651
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100652int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100653{
654 struct nvme_command c = { };
655 int error;
656
657 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
658 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200659 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100660
661 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
662 if (!*id)
663 return -ENOMEM;
664
665 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
666 sizeof(struct nvme_id_ctrl));
667 if (error)
668 kfree(*id);
669 return error;
670}
671
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200672static int nvme_identify_ns_descs(struct nvme_ns *ns, unsigned nsid)
673{
674 struct nvme_command c = { };
675 int status;
676 void *data;
677 int pos;
678 int len;
679
680 c.identify.opcode = nvme_admin_identify;
681 c.identify.nsid = cpu_to_le32(nsid);
682 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
683
684 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
685 if (!data)
686 return -ENOMEM;
687
688 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, data,
689 NVME_IDENTIFY_DATA_SIZE);
690 if (status)
691 goto free_data;
692
693 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
694 struct nvme_ns_id_desc *cur = data + pos;
695
696 if (cur->nidl == 0)
697 break;
698
699 switch (cur->nidt) {
700 case NVME_NIDT_EUI64:
701 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
702 dev_warn(ns->ctrl->device,
703 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
704 cur->nidl);
705 goto free_data;
706 }
707 len = NVME_NIDT_EUI64_LEN;
708 memcpy(ns->eui, data + pos + sizeof(*cur), len);
709 break;
710 case NVME_NIDT_NGUID:
711 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
712 dev_warn(ns->ctrl->device,
713 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
714 cur->nidl);
715 goto free_data;
716 }
717 len = NVME_NIDT_NGUID_LEN;
718 memcpy(ns->nguid, data + pos + sizeof(*cur), len);
719 break;
720 case NVME_NIDT_UUID:
721 if (cur->nidl != NVME_NIDT_UUID_LEN) {
722 dev_warn(ns->ctrl->device,
723 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
724 cur->nidl);
725 goto free_data;
726 }
727 len = NVME_NIDT_UUID_LEN;
728 uuid_copy(&ns->uuid, data + pos + sizeof(*cur));
729 break;
730 default:
731 /* Skip unnkown types */
732 len = cur->nidl;
733 break;
734 }
735
736 len += sizeof(*cur);
737 }
738free_data:
739 kfree(data);
740 return status;
741}
742
Keith Busch540c8012015-10-22 15:45:06 -0600743static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
744{
745 struct nvme_command c = { };
746
747 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200748 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600749 c.identify.nsid = cpu_to_le32(nsid);
750 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
751}
752
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100753int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100754 struct nvme_id_ns **id)
755{
756 struct nvme_command c = { };
757 int error;
758
759 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200760 c.identify.opcode = nvme_admin_identify;
761 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200762 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100763
764 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
765 if (!*id)
766 return -ENOMEM;
767
768 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
769 sizeof(struct nvme_id_ns));
770 if (error)
771 kfree(*id);
772 return error;
773}
774
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100775int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700776 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100777{
778 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800779 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100780 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100781
782 memset(&c, 0, sizeof(c));
783 c.features.opcode = nvme_admin_get_features;
784 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100785 c.features.fid = cpu_to_le32(fid);
786
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800787 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200788 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700789 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800790 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100791 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100792}
793
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100794int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700795 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100796{
797 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800798 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100799 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100800
801 memset(&c, 0, sizeof(c));
802 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100803 c.features.fid = cpu_to_le32(fid);
804 c.features.dword11 = cpu_to_le32(dword11);
805
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800806 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700807 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700808 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800809 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100810 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100811}
812
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100813int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100814{
815 struct nvme_command c = { };
816 int error;
817
818 c.common.opcode = nvme_admin_get_log_page,
819 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
820 c.common.cdw10[0] = cpu_to_le32(
821 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
822 NVME_LOG_SMART),
823
824 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
825 if (!*log)
826 return -ENOMEM;
827
828 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
829 sizeof(struct nvme_smart_log));
830 if (error)
831 kfree(*log);
832 return error;
833}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100834
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100835int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
836{
837 u32 q_count = (*count - 1) | ((*count - 1) << 16);
838 u32 result;
839 int status, nr_io_queues;
840
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700841 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100842 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200843 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100844 return status;
845
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200846 /*
847 * Degraded controllers might return an error when setting the queue
848 * count. We still want to be able to bring them online and offer
849 * access to the admin queue, as that might be only way to fix them up.
850 */
851 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200852 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200853 *count = 0;
854 } else {
855 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
856 *count = min(*count, nr_io_queues);
857 }
858
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100859 return 0;
860}
Ming Lin576d55d2016-02-10 10:03:32 -0800861EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100862
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100863static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
864{
865 struct nvme_user_io io;
866 struct nvme_command c;
867 unsigned length, meta_len;
868 void __user *metadata;
869
870 if (copy_from_user(&io, uio, sizeof(io)))
871 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700872 if (io.flags)
873 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100874
875 switch (io.opcode) {
876 case nvme_cmd_write:
877 case nvme_cmd_read:
878 case nvme_cmd_compare:
879 break;
880 default:
881 return -EINVAL;
882 }
883
884 length = (io.nblocks + 1) << ns->lba_shift;
885 meta_len = (io.nblocks + 1) * ns->ms;
886 metadata = (void __user *)(uintptr_t)io.metadata;
887
888 if (ns->ext) {
889 length += meta_len;
890 meta_len = 0;
891 } else if (meta_len) {
892 if ((io.metadata & 3) || !io.metadata)
893 return -EINVAL;
894 }
895
896 memset(&c, 0, sizeof(c));
897 c.rw.opcode = io.opcode;
898 c.rw.flags = io.flags;
899 c.rw.nsid = cpu_to_le32(ns->ns_id);
900 c.rw.slba = cpu_to_le64(io.slba);
901 c.rw.length = cpu_to_le16(io.nblocks);
902 c.rw.control = cpu_to_le16(io.control);
903 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
904 c.rw.reftag = cpu_to_le32(io.reftag);
905 c.rw.apptag = cpu_to_le16(io.apptag);
906 c.rw.appmask = cpu_to_le16(io.appmask);
907
908 return __nvme_submit_user_cmd(ns->queue, &c,
909 (void __user *)(uintptr_t)io.addr, length,
910 metadata, meta_len, io.slba, NULL, 0);
911}
912
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100913static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100914 struct nvme_passthru_cmd __user *ucmd)
915{
916 struct nvme_passthru_cmd cmd;
917 struct nvme_command c;
918 unsigned timeout = 0;
919 int status;
920
921 if (!capable(CAP_SYS_ADMIN))
922 return -EACCES;
923 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
924 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700925 if (cmd.flags)
926 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100927
928 memset(&c, 0, sizeof(c));
929 c.common.opcode = cmd.opcode;
930 c.common.flags = cmd.flags;
931 c.common.nsid = cpu_to_le32(cmd.nsid);
932 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
933 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
934 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
935 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
936 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
937 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
938 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
939 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
940
941 if (cmd.timeout_ms)
942 timeout = msecs_to_jiffies(cmd.timeout_ms);
943
944 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100945 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100946 &cmd.result, timeout);
947 if (status >= 0) {
948 if (put_user(cmd.result, &ucmd->result))
949 return -EFAULT;
950 }
951
952 return status;
953}
954
955static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
956 unsigned int cmd, unsigned long arg)
957{
958 struct nvme_ns *ns = bdev->bd_disk->private_data;
959
960 switch (cmd) {
961 case NVME_IOCTL_ID:
962 force_successful_syscall_return();
963 return ns->ns_id;
964 case NVME_IOCTL_ADMIN_CMD:
965 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
966 case NVME_IOCTL_IO_CMD:
967 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
968 case NVME_IOCTL_SUBMIT_IO:
969 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100970#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100971 case SG_GET_VERSION_NUM:
972 return nvme_sg_get_version_num((void __user *)arg);
973 case SG_IO:
974 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100975#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100976 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100977#ifdef CONFIG_NVM
978 if (ns->ndev)
979 return nvme_nvm_ioctl(ns, cmd, arg);
980#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700981 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100982 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700983 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100984 return -ENOTTY;
985 }
986}
987
988#ifdef CONFIG_COMPAT
989static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
990 unsigned int cmd, unsigned long arg)
991{
992 switch (cmd) {
993 case SG_IO:
994 return -ENOIOCTLCMD;
995 }
996 return nvme_ioctl(bdev, mode, cmd, arg);
997}
998#else
999#define nvme_compat_ioctl NULL
1000#endif
1001
1002static int nvme_open(struct block_device *bdev, fmode_t mode)
1003{
1004 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1005}
1006
1007static void nvme_release(struct gendisk *disk, fmode_t mode)
1008{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001009 struct nvme_ns *ns = disk->private_data;
1010
1011 module_put(ns->ctrl->ops->module);
1012 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001013}
1014
1015static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1016{
1017 /* some standard values */
1018 geo->heads = 1 << 6;
1019 geo->sectors = 1 << 5;
1020 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1021 return 0;
1022}
1023
1024#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001025static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1026 u16 bs)
1027{
1028 struct nvme_ns *ns = disk->private_data;
1029 u16 old_ms = ns->ms;
1030 u8 pi_type = 0;
1031
1032 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1033 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1034
1035 /* PI implementation requires metadata equal t10 pi tuple size */
1036 if (ns->ms == sizeof(struct t10_pi_tuple))
1037 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1038
1039 if (blk_get_integrity(disk) &&
1040 (ns->pi_type != pi_type || ns->ms != old_ms ||
1041 bs != queue_logical_block_size(disk->queue) ||
1042 (ns->ms && ns->ext)))
1043 blk_integrity_unregister(disk);
1044
1045 ns->pi_type = pi_type;
1046}
1047
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001048static void nvme_init_integrity(struct nvme_ns *ns)
1049{
1050 struct blk_integrity integrity;
1051
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001052 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001053 switch (ns->pi_type) {
1054 case NVME_NS_DPS_PI_TYPE3:
1055 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001056 integrity.tag_size = sizeof(u16) + sizeof(u32);
1057 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001058 break;
1059 case NVME_NS_DPS_PI_TYPE1:
1060 case NVME_NS_DPS_PI_TYPE2:
1061 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001062 integrity.tag_size = sizeof(u16);
1063 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001064 break;
1065 default:
1066 integrity.profile = NULL;
1067 break;
1068 }
1069 integrity.tuple_size = ns->ms;
1070 blk_integrity_register(ns->disk, &integrity);
1071 blk_queue_max_integrity_segments(ns->queue, 1);
1072}
1073#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001074static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1075 u16 bs)
1076{
1077}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001078static void nvme_init_integrity(struct nvme_ns *ns)
1079{
1080}
1081#endif /* CONFIG_BLK_DEV_INTEGRITY */
1082
1083static void nvme_config_discard(struct nvme_ns *ns)
1084{
Keith Busch08095e72016-03-04 13:15:17 -07001085 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001086 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001087
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001088 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1089 NVME_DSM_MAX_RANGES);
1090
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001091 ns->queue->limits.discard_alignment = logical_block_size;
1092 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +08001093 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001094 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001095 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001096
1097 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1098 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001099}
1100
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001101static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001102{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001103 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001104 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001105 return -ENODEV;
1106 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001107
1108 if ((*id)->ncap == 0) {
1109 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001110 return -ENODEV;
1111 }
1112
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001113 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001114 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001115 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001116 memcpy(ns->nguid, (*id)->nguid, sizeof(ns->nguid));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001117 if (ns->ctrl->vs >= NVME_VS(1, 3, 0)) {
1118 /* Don't treat error as fatal we potentially
1119 * already have a NGUID or EUI-64
1120 */
1121 if (nvme_identify_ns_descs(ns, ns->ns_id))
1122 dev_warn(ns->ctrl->device,
1123 "%s: Identify Descriptors failed\n", __func__);
1124 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001125
1126 return 0;
1127}
1128
1129static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1130{
1131 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001132 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001133
1134 /*
1135 * If identify namespace failed, use default 512 byte block size so
1136 * block layer can use before failing read/write for 0 capacity.
1137 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001138 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001139 if (ns->lba_shift == 0)
1140 ns->lba_shift = 9;
1141 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001142
1143 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001144
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001145 if (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
1146 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001147 blk_queue_logical_block_size(ns->queue, bs);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001148 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001149 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001150 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1151 set_capacity(disk, 0);
1152 else
1153 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1154
1155 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1156 nvme_config_discard(ns);
1157 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001158}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001159
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001160static int nvme_revalidate_disk(struct gendisk *disk)
1161{
1162 struct nvme_ns *ns = disk->private_data;
1163 struct nvme_id_ns *id = NULL;
1164 int ret;
1165
1166 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1167 set_capacity(disk, 0);
1168 return -ENODEV;
1169 }
1170
1171 ret = nvme_revalidate_ns(ns, &id);
1172 if (ret)
1173 return ret;
1174
1175 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001176 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001177
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001178 return 0;
1179}
1180
1181static char nvme_pr_type(enum pr_type type)
1182{
1183 switch (type) {
1184 case PR_WRITE_EXCLUSIVE:
1185 return 1;
1186 case PR_EXCLUSIVE_ACCESS:
1187 return 2;
1188 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1189 return 3;
1190 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1191 return 4;
1192 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1193 return 5;
1194 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1195 return 6;
1196 default:
1197 return 0;
1198 }
1199};
1200
1201static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1202 u64 key, u64 sa_key, u8 op)
1203{
1204 struct nvme_ns *ns = bdev->bd_disk->private_data;
1205 struct nvme_command c;
1206 u8 data[16] = { 0, };
1207
1208 put_unaligned_le64(key, &data[0]);
1209 put_unaligned_le64(sa_key, &data[8]);
1210
1211 memset(&c, 0, sizeof(c));
1212 c.common.opcode = op;
1213 c.common.nsid = cpu_to_le32(ns->ns_id);
1214 c.common.cdw10[0] = cpu_to_le32(cdw10);
1215
1216 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1217}
1218
1219static int nvme_pr_register(struct block_device *bdev, u64 old,
1220 u64 new, unsigned flags)
1221{
1222 u32 cdw10;
1223
1224 if (flags & ~PR_FL_IGNORE_KEY)
1225 return -EOPNOTSUPP;
1226
1227 cdw10 = old ? 2 : 0;
1228 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1229 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1230 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1231}
1232
1233static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1234 enum pr_type type, unsigned flags)
1235{
1236 u32 cdw10;
1237
1238 if (flags & ~PR_FL_IGNORE_KEY)
1239 return -EOPNOTSUPP;
1240
1241 cdw10 = nvme_pr_type(type) << 8;
1242 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1243 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1244}
1245
1246static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1247 enum pr_type type, bool abort)
1248{
1249 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1250 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1251}
1252
1253static int nvme_pr_clear(struct block_device *bdev, u64 key)
1254{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001255 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001256 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1257}
1258
1259static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1260{
1261 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1262 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1263}
1264
1265static const struct pr_ops nvme_pr_ops = {
1266 .pr_register = nvme_pr_register,
1267 .pr_reserve = nvme_pr_reserve,
1268 .pr_release = nvme_pr_release,
1269 .pr_preempt = nvme_pr_preempt,
1270 .pr_clear = nvme_pr_clear,
1271};
1272
Scott Bauera98e58e52017-02-03 12:50:32 -07001273#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001274int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1275 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001276{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001277 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001278 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001279
1280 memset(&cmd, 0, sizeof(cmd));
1281 if (send)
1282 cmd.common.opcode = nvme_admin_security_send;
1283 else
1284 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001285 cmd.common.nsid = 0;
1286 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1287 cmd.common.cdw10[1] = cpu_to_le32(len);
1288
1289 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1290 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1291}
1292EXPORT_SYMBOL_GPL(nvme_sec_submit);
1293#endif /* CONFIG_BLK_SED_OPAL */
1294
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001295static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001296 .owner = THIS_MODULE,
1297 .ioctl = nvme_ioctl,
1298 .compat_ioctl = nvme_compat_ioctl,
1299 .open = nvme_open,
1300 .release = nvme_release,
1301 .getgeo = nvme_getgeo,
1302 .revalidate_disk= nvme_revalidate_disk,
1303 .pr_ops = &nvme_pr_ops,
1304};
1305
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001306static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1307{
1308 unsigned long timeout =
1309 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1310 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1311 int ret;
1312
1313 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001314 if (csts == ~0)
1315 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001316 if ((csts & NVME_CSTS_RDY) == bit)
1317 break;
1318
1319 msleep(100);
1320 if (fatal_signal_pending(current))
1321 return -EINTR;
1322 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001323 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001324 "Device not ready; aborting %s\n", enabled ?
1325 "initialisation" : "reset");
1326 return -ENODEV;
1327 }
1328 }
1329
1330 return ret;
1331}
1332
1333/*
1334 * If the device has been passed off to us in an enabled state, just clear
1335 * the enabled bit. The spec says we should set the 'shutdown notification
1336 * bits', but doing so may cause the device to complete commands to the
1337 * admin queue ... and we don't know what memory that might be pointing at!
1338 */
1339int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1340{
1341 int ret;
1342
1343 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1344 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1345
1346 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1347 if (ret)
1348 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001349
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001350 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001351 msleep(NVME_QUIRK_DELAY_AMOUNT);
1352
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001353 return nvme_wait_ready(ctrl, cap, false);
1354}
Ming Lin576d55d2016-02-10 10:03:32 -08001355EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001356
1357int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1358{
1359 /*
1360 * Default to a 4K page size, with the intention to update this
1361 * path in the future to accomodate architectures with differing
1362 * kernel and IO page sizes.
1363 */
1364 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1365 int ret;
1366
1367 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001368 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001369 "Minimum device page size %u too large for host (%u)\n",
1370 1 << dev_page_min, 1 << page_shift);
1371 return -ENODEV;
1372 }
1373
1374 ctrl->page_size = 1 << page_shift;
1375
1376 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1377 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1378 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1379 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1380 ctrl->ctrl_config |= NVME_CC_ENABLE;
1381
1382 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1383 if (ret)
1384 return ret;
1385 return nvme_wait_ready(ctrl, cap, true);
1386}
Ming Lin576d55d2016-02-10 10:03:32 -08001387EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001388
1389int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1390{
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +02001391 unsigned long timeout = jiffies + (shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001392 u32 csts;
1393 int ret;
1394
1395 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1396 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1397
1398 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1399 if (ret)
1400 return ret;
1401
1402 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1403 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1404 break;
1405
1406 msleep(100);
1407 if (fatal_signal_pending(current))
1408 return -EINTR;
1409 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001410 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001411 "Device shutdown incomplete; abort shutdown\n");
1412 return -ENODEV;
1413 }
1414 }
1415
1416 return ret;
1417}
Ming Lin576d55d2016-02-10 10:03:32 -08001418EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001419
Christoph Hellwigda358252016-03-02 18:07:11 +01001420static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1421 struct request_queue *q)
1422{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001423 bool vwc = false;
1424
Christoph Hellwigda358252016-03-02 18:07:11 +01001425 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001426 u32 max_segments =
1427 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1428
Christoph Hellwigda358252016-03-02 18:07:11 +01001429 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001430 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001431 }
Keith Busche6282ae2016-12-19 11:37:50 -05001432 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1433 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001434 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001435 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1436 vwc = true;
1437 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001438}
1439
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001440static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1441{
1442 /*
1443 * APST (Autonomous Power State Transition) lets us program a
1444 * table of power state transitions that the controller will
1445 * perform automatically. We configure it with a simple
1446 * heuristic: we are willing to spend at most 2% of the time
1447 * transitioning between power states. Therefore, when running
1448 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001449 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001450 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001451 * the requested maximum latency.
1452 *
1453 * We will not autonomously enter any non-operational state for
1454 * which the total latency exceeds ps_max_latency_us. Users
1455 * can set ps_max_latency_us to zero to turn off APST.
1456 */
1457
1458 unsigned apste;
1459 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001460 u64 max_lat_us = 0;
1461 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001462 int ret;
1463
1464 /*
1465 * If APST isn't supported or if we haven't been initialized yet,
1466 * then don't do anything.
1467 */
1468 if (!ctrl->apsta)
1469 return;
1470
1471 if (ctrl->npss > 31) {
1472 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1473 return;
1474 }
1475
1476 table = kzalloc(sizeof(*table), GFP_KERNEL);
1477 if (!table)
1478 return;
1479
1480 if (ctrl->ps_max_latency_us == 0) {
1481 /* Turn off APST. */
1482 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001483 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001484 } else {
1485 __le64 target = cpu_to_le64(0);
1486 int state;
1487
1488 /*
1489 * Walk through all states from lowest- to highest-power.
1490 * According to the spec, lower-numbered states use more
1491 * power. NPSS, despite the name, is the index of the
1492 * lowest-power state, not the number of states.
1493 */
1494 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001495 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001496
1497 if (target)
1498 table->entries[state] = target;
1499
1500 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001501 * Don't allow transitions to the deepest state
1502 * if it's quirked off.
1503 */
1504 if (state == ctrl->npss &&
1505 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1506 continue;
1507
1508 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001509 * Is this state a useful non-operational state for
1510 * higher-power states to autonomously transition to?
1511 */
1512 if (!(ctrl->psd[state].flags &
1513 NVME_PS_FLAGS_NON_OP_STATE))
1514 continue;
1515
Kai-Heng Fengda875912017-06-07 15:25:42 +08001516 exit_latency_us =
1517 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1518 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001519 continue;
1520
Kai-Heng Fengda875912017-06-07 15:25:42 +08001521 total_latency_us =
1522 exit_latency_us +
1523 le32_to_cpu(ctrl->psd[state].entry_lat);
1524
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001525 /*
1526 * This state is good. Use it as the APST idle
1527 * target for higher power states.
1528 */
1529 transition_ms = total_latency_us + 19;
1530 do_div(transition_ms, 20);
1531 if (transition_ms > (1 << 24) - 1)
1532 transition_ms = (1 << 24) - 1;
1533
1534 target = cpu_to_le64((state << 3) |
1535 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001536
1537 if (max_ps == -1)
1538 max_ps = state;
1539
1540 if (total_latency_us > max_lat_us)
1541 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001542 }
1543
1544 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001545
1546 if (max_ps == -1) {
1547 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1548 } else {
1549 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1550 max_ps, max_lat_us, (int)sizeof(*table), table);
1551 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001552 }
1553
1554 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1555 table, sizeof(*table), NULL);
1556 if (ret)
1557 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1558
1559 kfree(table);
1560}
1561
1562static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1563{
1564 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1565 u64 latency;
1566
1567 switch (val) {
1568 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1569 case PM_QOS_LATENCY_ANY:
1570 latency = U64_MAX;
1571 break;
1572
1573 default:
1574 latency = val;
1575 }
1576
1577 if (ctrl->ps_max_latency_us != latency) {
1578 ctrl->ps_max_latency_us = latency;
1579 nvme_configure_apst(ctrl);
1580 }
1581}
1582
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001583struct nvme_core_quirk_entry {
1584 /*
1585 * NVMe model and firmware strings are padded with spaces. For
1586 * simplicity, strings in the quirk table are padded with NULLs
1587 * instead.
1588 */
1589 u16 vid;
1590 const char *mn;
1591 const char *fr;
1592 unsigned long quirks;
1593};
1594
1595static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001596 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001597 /*
1598 * This Toshiba device seems to die using any APST states. See:
1599 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1600 */
1601 .vid = 0x1179,
1602 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001603 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001604 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001605};
1606
1607/* match is null-terminated but idstr is space-padded. */
1608static bool string_matches(const char *idstr, const char *match, size_t len)
1609{
1610 size_t matchlen;
1611
1612 if (!match)
1613 return true;
1614
1615 matchlen = strlen(match);
1616 WARN_ON_ONCE(matchlen > len);
1617
1618 if (memcmp(idstr, match, matchlen))
1619 return false;
1620
1621 for (; matchlen < len; matchlen++)
1622 if (idstr[matchlen] != ' ')
1623 return false;
1624
1625 return true;
1626}
1627
1628static bool quirk_matches(const struct nvme_id_ctrl *id,
1629 const struct nvme_core_quirk_entry *q)
1630{
1631 return q->vid == le16_to_cpu(id->vid) &&
1632 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1633 string_matches(id->fr, q->fr, sizeof(id->fr));
1634}
1635
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001636/*
1637 * Initialize the cached copies of the Identify data and various controller
1638 * register in our nvme_ctrl structure. This should be called as soon as
1639 * the admin queue is fully up and running.
1640 */
1641int nvme_init_identify(struct nvme_ctrl *ctrl)
1642{
1643 struct nvme_id_ctrl *id;
1644 u64 cap;
1645 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001646 u32 max_hw_sectors;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001647 u8 prev_apsta;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001648
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001649 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1650 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001651 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001652 return ret;
1653 }
1654
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001655 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1656 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001657 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001658 return ret;
1659 }
1660 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1661
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001662 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001663 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1664
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001665 ret = nvme_identify_ctrl(ctrl, &id);
1666 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001667 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001668 return -EIO;
1669 }
1670
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001671 if (!ctrl->identified) {
1672 /*
1673 * Check for quirks. Quirk can depend on firmware version,
1674 * so, in principle, the set of quirks present can change
1675 * across a reset. As a possible future enhancement, we
1676 * could re-scan for quirks every time we reinitialize
1677 * the device, but we'd have to make sure that the driver
1678 * behaves intelligently if the quirks change.
1679 */
1680
1681 int i;
1682
1683 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1684 if (quirk_matches(id, &core_quirks[i]))
1685 ctrl->quirks |= core_quirks[i].quirks;
1686 }
1687 }
1688
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001689 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001690 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 -07001691 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1692 }
1693
Scott Bauer8a9ae522017-02-17 13:59:40 +01001694 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001695 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001696 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001697 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001698 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001699 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001700 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1701 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1702 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1703 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001704 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001705 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001706 max_hw_sectors = UINT_MAX;
1707 ctrl->max_hw_sectors =
1708 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001709
Christoph Hellwigda358252016-03-02 18:07:11 +01001710 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001711 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001712 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001713
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001714 ctrl->npss = id->npss;
1715 prev_apsta = ctrl->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001716 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1717 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001718 dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001719 ctrl->apsta = 1;
1720 } else {
1721 ctrl->apsta = 0;
1722 }
1723 } else {
1724 ctrl->apsta = id->apsta;
1725 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001726 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1727
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001728 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001729 ctrl->icdoff = le16_to_cpu(id->icdoff);
1730 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1731 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1732 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1733
1734 /*
1735 * In fabrics we need to verify the cntlid matches the
1736 * admin connect
1737 */
1738 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1739 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001740
1741 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001742 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001743 "keep-alive support is mandatory for fabrics\n");
1744 ret = -EINVAL;
1745 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001746 } else {
1747 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001748 ctrl->hmpre = le32_to_cpu(id->hmpre);
1749 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001750 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001751
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001752 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001753
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001754 if (ctrl->apsta && !prev_apsta)
1755 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1756 else if (!ctrl->apsta && prev_apsta)
1757 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1758
1759 nvme_configure_apst(ctrl);
1760
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001761 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001762
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001763 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001764}
Ming Lin576d55d2016-02-10 10:03:32 -08001765EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001766
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001767static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001768{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001769 struct nvme_ctrl *ctrl;
1770 int instance = iminor(inode);
1771 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001772
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001773 spin_lock(&dev_list_lock);
1774 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1775 if (ctrl->instance != instance)
1776 continue;
1777
1778 if (!ctrl->admin_q) {
1779 ret = -EWOULDBLOCK;
1780 break;
1781 }
1782 if (!kref_get_unless_zero(&ctrl->kref))
1783 break;
1784 file->private_data = ctrl;
1785 ret = 0;
1786 break;
1787 }
1788 spin_unlock(&dev_list_lock);
1789
1790 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001791}
1792
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001793static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001794{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001795 nvme_put_ctrl(file->private_data);
1796 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001797}
1798
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001799static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1800{
1801 struct nvme_ns *ns;
1802 int ret;
1803
1804 mutex_lock(&ctrl->namespaces_mutex);
1805 if (list_empty(&ctrl->namespaces)) {
1806 ret = -ENOTTY;
1807 goto out_unlock;
1808 }
1809
1810 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1811 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001812 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001813 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1814 ret = -EINVAL;
1815 goto out_unlock;
1816 }
1817
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001818 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001819 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1820 kref_get(&ns->kref);
1821 mutex_unlock(&ctrl->namespaces_mutex);
1822
1823 ret = nvme_user_cmd(ctrl, ns, argp);
1824 nvme_put_ns(ns);
1825 return ret;
1826
1827out_unlock:
1828 mutex_unlock(&ctrl->namespaces_mutex);
1829 return ret;
1830}
1831
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001832static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1833 unsigned long arg)
1834{
1835 struct nvme_ctrl *ctrl = file->private_data;
1836 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001837
1838 switch (cmd) {
1839 case NVME_IOCTL_ADMIN_CMD:
1840 return nvme_user_cmd(ctrl, NULL, argp);
1841 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001842 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001843 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001844 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001845 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001846 case NVME_IOCTL_SUBSYS_RESET:
1847 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001848 case NVME_IOCTL_RESCAN:
1849 nvme_queue_scan(ctrl);
1850 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001851 default:
1852 return -ENOTTY;
1853 }
1854}
1855
1856static const struct file_operations nvme_dev_fops = {
1857 .owner = THIS_MODULE,
1858 .open = nvme_dev_open,
1859 .release = nvme_dev_release,
1860 .unlocked_ioctl = nvme_dev_ioctl,
1861 .compat_ioctl = nvme_dev_ioctl,
1862};
1863
1864static ssize_t nvme_sysfs_reset(struct device *dev,
1865 struct device_attribute *attr, const char *buf,
1866 size_t count)
1867{
1868 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1869 int ret;
1870
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001871 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001872 if (ret < 0)
1873 return ret;
1874 return count;
1875}
1876static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1877
Keith Busch9ec3bb22016-04-29 15:45:18 -06001878static ssize_t nvme_sysfs_rescan(struct device *dev,
1879 struct device_attribute *attr, const char *buf,
1880 size_t count)
1881{
1882 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1883
1884 nvme_queue_scan(ctrl);
1885 return count;
1886}
1887static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1888
Keith Busch118472a2016-02-18 09:57:48 -07001889static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1890 char *buf)
1891{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001892 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001893 struct nvme_ctrl *ctrl = ns->ctrl;
1894 int serial_len = sizeof(ctrl->serial);
1895 int model_len = sizeof(ctrl->model);
1896
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001897 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
1898 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07001899
1900 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1901 return sprintf(buf, "eui.%8phN\n", ns->eui);
1902
1903 while (ctrl->serial[serial_len - 1] == ' ')
1904 serial_len--;
1905 while (ctrl->model[model_len - 1] == ' ')
1906 model_len--;
1907
1908 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1909 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1910}
1911static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1912
Johannes Thumshirnd934f982017-06-07 11:45:35 +02001913static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
1914 char *buf)
1915{
1916 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1917 return sprintf(buf, "%pU\n", ns->nguid);
1918}
1919static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
1920
Keith Busch2b9b6e82015-12-22 10:10:45 -07001921static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1922 char *buf)
1923{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001924 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02001925
1926 /* For backward compatibility expose the NGUID to userspace if
1927 * we have no UUID set
1928 */
1929 if (uuid_is_null(&ns->uuid)) {
1930 printk_ratelimited(KERN_WARNING
1931 "No UUID available providing old NGUID\n");
1932 return sprintf(buf, "%pU\n", ns->nguid);
1933 }
1934 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001935}
1936static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1937
1938static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1939 char *buf)
1940{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001941 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001942 return sprintf(buf, "%8phd\n", ns->eui);
1943}
1944static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1945
1946static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1947 char *buf)
1948{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001949 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001950 return sprintf(buf, "%d\n", ns->ns_id);
1951}
1952static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1953
1954static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001955 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001956 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02001957 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001958 &dev_attr_eui.attr,
1959 &dev_attr_nsid.attr,
1960 NULL,
1961};
1962
Ming Lin1a353d82016-06-13 16:45:24 +02001963static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001964 struct attribute *a, int n)
1965{
1966 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001967 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001968
1969 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02001970 if (uuid_is_null(&ns->uuid) ||
1971 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
1972 return 0;
1973 }
1974 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001975 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07001976 return 0;
1977 }
1978 if (a == &dev_attr_eui.attr) {
1979 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1980 return 0;
1981 }
1982 return a->mode;
1983}
1984
1985static const struct attribute_group nvme_ns_attr_group = {
1986 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001987 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001988};
1989
Ming Lin931e1c22016-02-26 13:24:19 -08001990#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001991static ssize_t field##_show(struct device *dev, \
1992 struct device_attribute *attr, char *buf) \
1993{ \
1994 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1995 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1996} \
1997static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1998
Ming Lin931e1c22016-02-26 13:24:19 -08001999#define nvme_show_int_function(field) \
2000static ssize_t field##_show(struct device *dev, \
2001 struct device_attribute *attr, char *buf) \
2002{ \
2003 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2004 return sprintf(buf, "%d\n", ctrl->field); \
2005} \
2006static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2007
2008nvme_show_str_function(model);
2009nvme_show_str_function(serial);
2010nvme_show_str_function(firmware_rev);
2011nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002012
Ming Lin1a353d82016-06-13 16:45:24 +02002013static ssize_t nvme_sysfs_delete(struct device *dev,
2014 struct device_attribute *attr, const char *buf,
2015 size_t count)
2016{
2017 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2018
2019 if (device_remove_file_self(dev, attr))
2020 ctrl->ops->delete_ctrl(ctrl);
2021 return count;
2022}
2023static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2024
2025static ssize_t nvme_sysfs_show_transport(struct device *dev,
2026 struct device_attribute *attr,
2027 char *buf)
2028{
2029 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2030
2031 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2032}
2033static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2034
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002035static ssize_t nvme_sysfs_show_state(struct device *dev,
2036 struct device_attribute *attr,
2037 char *buf)
2038{
2039 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2040 static const char *const state_name[] = {
2041 [NVME_CTRL_NEW] = "new",
2042 [NVME_CTRL_LIVE] = "live",
2043 [NVME_CTRL_RESETTING] = "resetting",
2044 [NVME_CTRL_RECONNECTING]= "reconnecting",
2045 [NVME_CTRL_DELETING] = "deleting",
2046 [NVME_CTRL_DEAD] = "dead",
2047 };
2048
2049 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2050 state_name[ctrl->state])
2051 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2052
2053 return sprintf(buf, "unknown state\n");
2054}
2055
2056static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2057
Ming Lin1a353d82016-06-13 16:45:24 +02002058static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2059 struct device_attribute *attr,
2060 char *buf)
2061{
2062 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2063
2064 return snprintf(buf, PAGE_SIZE, "%s\n",
2065 ctrl->ops->get_subsysnqn(ctrl));
2066}
2067static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2068
2069static ssize_t nvme_sysfs_show_address(struct device *dev,
2070 struct device_attribute *attr,
2071 char *buf)
2072{
2073 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2074
2075 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2076}
2077static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2078
Keith Busch779ff7562016-01-12 15:09:31 -07002079static struct attribute *nvme_dev_attrs[] = {
2080 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002081 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002082 &dev_attr_model.attr,
2083 &dev_attr_serial.attr,
2084 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002085 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002086 &dev_attr_delete_controller.attr,
2087 &dev_attr_transport.attr,
2088 &dev_attr_subsysnqn.attr,
2089 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002090 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002091 NULL
2092};
2093
Ming Lin1a353d82016-06-13 16:45:24 +02002094#define CHECK_ATTR(ctrl, a, name) \
2095 if ((a) == &dev_attr_##name.attr && \
2096 !(ctrl)->ops->get_##name) \
2097 return 0
2098
2099static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2100 struct attribute *a, int n)
2101{
2102 struct device *dev = container_of(kobj, struct device, kobj);
2103 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2104
2105 if (a == &dev_attr_delete_controller.attr) {
2106 if (!ctrl->ops->delete_ctrl)
2107 return 0;
2108 }
2109
2110 CHECK_ATTR(ctrl, a, subsysnqn);
2111 CHECK_ATTR(ctrl, a, address);
2112
2113 return a->mode;
2114}
2115
Keith Busch779ff7562016-01-12 15:09:31 -07002116static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002117 .attrs = nvme_dev_attrs,
2118 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002119};
2120
2121static const struct attribute_group *nvme_dev_attr_groups[] = {
2122 &nvme_dev_attrs_group,
2123 NULL,
2124};
2125
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002126static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2127{
2128 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2129 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2130
2131 return nsa->ns_id - nsb->ns_id;
2132}
2133
Keith Busch32f0c4a2016-07-13 11:45:02 -06002134static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002135{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002136 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002137
Keith Busch32f0c4a2016-07-13 11:45:02 -06002138 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002139 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002140 if (ns->ns_id == nsid) {
2141 kref_get(&ns->kref);
2142 ret = ns;
2143 break;
2144 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002145 if (ns->ns_id > nsid)
2146 break;
2147 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002148 mutex_unlock(&ctrl->namespaces_mutex);
2149 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002150}
2151
2152static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2153{
2154 struct nvme_ns *ns;
2155 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002156 struct nvme_id_ns *id;
2157 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002158 int node = dev_to_node(ctrl->dev);
2159
2160 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2161 if (!ns)
2162 return;
2163
Keith Busch075790e2016-02-24 09:15:53 -07002164 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2165 if (ns->instance < 0)
2166 goto out_free_ns;
2167
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002168 ns->queue = blk_mq_init_queue(ctrl->tagset);
2169 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002170 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002171 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2172 ns->queue->queuedata = ns;
2173 ns->ctrl = ctrl;
2174
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002175 kref_init(&ns->kref);
2176 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002177 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002178
2179 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002180 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002181
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002182 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002183
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002184 if (nvme_revalidate_ns(ns, &id))
2185 goto out_free_queue;
2186
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002187 if (nvme_nvm_ns_supported(ns, id) &&
2188 nvme_nvm_register(ns, disk_name, node)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002189 dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002190 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002191 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002192
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002193 disk = alloc_disk_node(0, node);
2194 if (!disk)
2195 goto out_free_id;
2196
2197 disk->fops = &nvme_fops;
2198 disk->private_data = ns;
2199 disk->queue = ns->queue;
2200 disk->flags = GENHD_FL_EXT_DEVT;
2201 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2202 ns->disk = disk;
2203
2204 __nvme_revalidate_disk(disk, id);
2205
Keith Busch32f0c4a2016-07-13 11:45:02 -06002206 mutex_lock(&ctrl->namespaces_mutex);
2207 list_add_tail(&ns->list, &ctrl->namespaces);
2208 mutex_unlock(&ctrl->namespaces_mutex);
2209
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002210 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002211
2212 kfree(id);
2213
Dan Williams0d52c7562016-06-15 19:44:20 -07002214 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002215 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2216 &nvme_ns_attr_group))
2217 pr_warn("%s: failed to create sysfs group for identification\n",
2218 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002219 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2220 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2221 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002222 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002223 out_free_id:
2224 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002225 out_free_queue:
2226 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002227 out_release_instance:
2228 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002229 out_free_ns:
2230 kfree(ns);
2231}
2232
2233static void nvme_ns_remove(struct nvme_ns *ns)
2234{
Keith Busch646017a2016-02-24 09:15:54 -07002235 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2236 return;
2237
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002238 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002239 if (blk_get_integrity(ns->disk))
2240 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002241 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2242 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002243 if (ns->ndev)
2244 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002245 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002246 blk_cleanup_queue(ns->queue);
2247 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002248
2249 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002250 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002251 mutex_unlock(&ns->ctrl->namespaces_mutex);
2252
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002253 nvme_put_ns(ns);
2254}
2255
Keith Busch540c8012015-10-22 15:45:06 -06002256static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2257{
2258 struct nvme_ns *ns;
2259
Keith Busch32f0c4a2016-07-13 11:45:02 -06002260 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002261 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002262 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002263 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002264 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002265 } else
2266 nvme_alloc_ns(ctrl, nsid);
2267}
2268
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302269static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2270 unsigned nsid)
2271{
2272 struct nvme_ns *ns, *next;
2273
2274 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2275 if (ns->ns_id > nsid)
2276 nvme_ns_remove(ns);
2277 }
2278}
2279
Keith Busch540c8012015-10-22 15:45:06 -06002280static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2281{
2282 struct nvme_ns *ns;
2283 __le32 *ns_list;
2284 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2285 int ret = 0;
2286
2287 ns_list = kzalloc(0x1000, GFP_KERNEL);
2288 if (!ns_list)
2289 return -ENOMEM;
2290
2291 for (i = 0; i < num_lists; i++) {
2292 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2293 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302294 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002295
2296 for (j = 0; j < min(nn, 1024U); j++) {
2297 nsid = le32_to_cpu(ns_list[j]);
2298 if (!nsid)
2299 goto out;
2300
2301 nvme_validate_ns(ctrl, nsid);
2302
2303 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002304 ns = nvme_find_get_ns(ctrl, prev);
2305 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002306 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002307 nvme_put_ns(ns);
2308 }
Keith Busch540c8012015-10-22 15:45:06 -06002309 }
2310 }
2311 nn -= j;
2312 }
2313 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302314 nvme_remove_invalid_namespaces(ctrl, prev);
2315 free:
Keith Busch540c8012015-10-22 15:45:06 -06002316 kfree(ns_list);
2317 return ret;
2318}
2319
Christoph Hellwig5955be22016-04-26 13:51:59 +02002320static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002321{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002322 unsigned i;
2323
Keith Busch540c8012015-10-22 15:45:06 -06002324 for (i = 1; i <= nn; i++)
2325 nvme_validate_ns(ctrl, i);
2326
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302327 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002328}
2329
Christoph Hellwig5955be22016-04-26 13:51:59 +02002330static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002331{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002332 struct nvme_ctrl *ctrl =
2333 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002334 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002335 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002336
Christoph Hellwig5955be22016-04-26 13:51:59 +02002337 if (ctrl->state != NVME_CTRL_LIVE)
2338 return;
2339
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002340 if (nvme_identify_ctrl(ctrl, &id))
2341 return;
Keith Busch540c8012015-10-22 15:45:06 -06002342
2343 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002344 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002345 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2346 if (!nvme_scan_ns_list(ctrl, nn))
2347 goto done;
2348 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002349 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002350 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002351 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002352 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002353 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002354 kfree(id);
2355}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002356
2357void nvme_queue_scan(struct nvme_ctrl *ctrl)
2358{
2359 /*
2360 * Do not queue new scan work when a controller is reset during
2361 * removal.
2362 */
2363 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002364 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002365}
2366EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002367
Keith Busch32f0c4a2016-07-13 11:45:02 -06002368/*
2369 * This function iterates the namespace list unlocked to allow recovery from
2370 * controller failure. It is up to the caller to ensure the namespace list is
2371 * not modified by scan work while this function is executing.
2372 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002373void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2374{
2375 struct nvme_ns *ns, *next;
2376
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002377 /*
2378 * The dead states indicates the controller was not gracefully
2379 * disconnected. In that case, we won't be able to flush any data while
2380 * removing the namespaces' disks; fail all the queues now to avoid
2381 * potentially having to clean up the failed sync later.
2382 */
2383 if (ctrl->state == NVME_CTRL_DEAD)
2384 nvme_kill_queues(ctrl);
2385
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002386 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2387 nvme_ns_remove(ns);
2388}
Ming Lin576d55d2016-02-10 10:03:32 -08002389EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002390
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002391static void nvme_async_event_work(struct work_struct *work)
2392{
2393 struct nvme_ctrl *ctrl =
2394 container_of(work, struct nvme_ctrl, async_event_work);
2395
2396 spin_lock_irq(&ctrl->lock);
2397 while (ctrl->event_limit > 0) {
2398 int aer_idx = --ctrl->event_limit;
2399
2400 spin_unlock_irq(&ctrl->lock);
2401 ctrl->ops->submit_async_event(ctrl, aer_idx);
2402 spin_lock_irq(&ctrl->lock);
2403 }
2404 spin_unlock_irq(&ctrl->lock);
2405}
2406
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002407void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2408 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002409{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002410 u32 result = le32_to_cpu(res->u32);
2411 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002412
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002413 switch (le16_to_cpu(status) >> 1) {
2414 case NVME_SC_SUCCESS:
2415 done = false;
2416 /*FALLTHRU*/
2417 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002418 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002419 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002420 break;
2421 default:
2422 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002423 }
2424
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002425 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002426 return;
2427
2428 switch (result & 0xff07) {
2429 case NVME_AER_NOTICE_NS_CHANGED:
2430 dev_info(ctrl->device, "rescanning\n");
2431 nvme_queue_scan(ctrl);
2432 break;
2433 default:
2434 dev_warn(ctrl->device, "async event result %08x\n", result);
2435 }
2436}
2437EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2438
2439void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2440{
2441 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002442 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002443}
2444EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2445
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002446static DEFINE_IDA(nvme_instance_ida);
2447
2448static int nvme_set_instance(struct nvme_ctrl *ctrl)
2449{
2450 int instance, error;
2451
2452 do {
2453 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2454 return -ENODEV;
2455
2456 spin_lock(&dev_list_lock);
2457 error = ida_get_new(&nvme_instance_ida, &instance);
2458 spin_unlock(&dev_list_lock);
2459 } while (error == -EAGAIN);
2460
2461 if (error)
2462 return -ENODEV;
2463
2464 ctrl->instance = instance;
2465 return 0;
2466}
2467
2468static void nvme_release_instance(struct nvme_ctrl *ctrl)
2469{
2470 spin_lock(&dev_list_lock);
2471 ida_remove(&nvme_instance_ida, ctrl->instance);
2472 spin_unlock(&dev_list_lock);
2473}
2474
Keith Busch53029b02015-11-28 15:41:02 +01002475void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002476{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002477 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002478 flush_work(&ctrl->scan_work);
2479 nvme_remove_namespaces(ctrl);
2480
Keith Busch53029b02015-11-28 15:41:02 +01002481 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002482
2483 spin_lock(&dev_list_lock);
2484 list_del(&ctrl->node);
2485 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002486}
Ming Lin576d55d2016-02-10 10:03:32 -08002487EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002488
2489static void nvme_free_ctrl(struct kref *kref)
2490{
2491 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002492
2493 put_device(ctrl->device);
2494 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002495 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002496
2497 ctrl->ops->free_ctrl(ctrl);
2498}
2499
2500void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2501{
2502 kref_put(&ctrl->kref, nvme_free_ctrl);
2503}
Ming Lin576d55d2016-02-10 10:03:32 -08002504EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002505
2506/*
2507 * Initialize a NVMe controller structures. This needs to be called during
2508 * earliest initialization so that we have the initialized structured around
2509 * during probing.
2510 */
2511int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2512 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2513{
2514 int ret;
2515
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002516 ctrl->state = NVME_CTRL_NEW;
2517 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002518 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002519 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002520 kref_init(&ctrl->kref);
2521 ctrl->dev = dev;
2522 ctrl->ops = ops;
2523 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002524 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002525 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002526
2527 ret = nvme_set_instance(ctrl);
2528 if (ret)
2529 goto out;
2530
Keith Busch779ff7562016-01-12 15:09:31 -07002531 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002532 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002533 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002534 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002535 if (IS_ERR(ctrl->device)) {
2536 ret = PTR_ERR(ctrl->device);
2537 goto out_release_instance;
2538 }
2539 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002540 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002541
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002542 spin_lock(&dev_list_lock);
2543 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2544 spin_unlock(&dev_list_lock);
2545
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002546 /*
2547 * Initialize latency tolerance controls. The sysfs files won't
2548 * be visible to userspace unless the device actually supports APST.
2549 */
2550 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2551 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2552 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2553
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002554 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002555out_release_instance:
2556 nvme_release_instance(ctrl);
2557out:
2558 return ret;
2559}
Ming Lin576d55d2016-02-10 10:03:32 -08002560EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002561
Keith Busch69d9a992016-02-24 09:15:56 -07002562/**
2563 * nvme_kill_queues(): Ends all namespace queues
2564 * @ctrl: the dead controller that needs to end
2565 *
2566 * Call this function when the driver determines it is unable to get the
2567 * controller in a state capable of servicing IO.
2568 */
2569void nvme_kill_queues(struct nvme_ctrl *ctrl)
2570{
2571 struct nvme_ns *ns;
2572
Keith Busch32f0c4a2016-07-13 11:45:02 -06002573 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002574
2575 /* Forcibly start all queues to avoid having stuck requests */
2576 blk_mq_start_hw_queues(ctrl->admin_q);
2577
Keith Busch32f0c4a2016-07-13 11:45:02 -06002578 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002579 /*
2580 * Revalidating a dead namespace sets capacity to 0. This will
2581 * end buffered writers dirtying pages that can't be synced.
2582 */
Keith Buschf33447b2017-02-10 18:15:51 -05002583 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2584 continue;
2585 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002586 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002587
2588 /*
2589 * Forcibly start all queues to avoid having stuck requests.
2590 * Note that we must ensure the queues are not stopped
2591 * when the final removal happens.
2592 */
2593 blk_mq_start_hw_queues(ns->queue);
Ming Lei986f75c2017-05-22 23:05:04 +08002594
2595 /* draining requests in requeue list */
2596 blk_mq_kick_requeue_list(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002597 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002598 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002599}
Linus Torvalds237045f2016-03-18 17:13:31 -07002600EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002601
Keith Busch302ad8c2017-03-01 14:22:12 -05002602void nvme_unfreeze(struct nvme_ctrl *ctrl)
2603{
2604 struct nvme_ns *ns;
2605
2606 mutex_lock(&ctrl->namespaces_mutex);
2607 list_for_each_entry(ns, &ctrl->namespaces, list)
2608 blk_mq_unfreeze_queue(ns->queue);
2609 mutex_unlock(&ctrl->namespaces_mutex);
2610}
2611EXPORT_SYMBOL_GPL(nvme_unfreeze);
2612
2613void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2614{
2615 struct nvme_ns *ns;
2616
2617 mutex_lock(&ctrl->namespaces_mutex);
2618 list_for_each_entry(ns, &ctrl->namespaces, list) {
2619 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2620 if (timeout <= 0)
2621 break;
2622 }
2623 mutex_unlock(&ctrl->namespaces_mutex);
2624}
2625EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2626
2627void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2628{
2629 struct nvme_ns *ns;
2630
2631 mutex_lock(&ctrl->namespaces_mutex);
2632 list_for_each_entry(ns, &ctrl->namespaces, list)
2633 blk_mq_freeze_queue_wait(ns->queue);
2634 mutex_unlock(&ctrl->namespaces_mutex);
2635}
2636EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2637
2638void nvme_start_freeze(struct nvme_ctrl *ctrl)
2639{
2640 struct nvme_ns *ns;
2641
2642 mutex_lock(&ctrl->namespaces_mutex);
2643 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002644 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002645 mutex_unlock(&ctrl->namespaces_mutex);
2646}
2647EXPORT_SYMBOL_GPL(nvme_start_freeze);
2648
Keith Busch25646262016-01-04 09:10:57 -07002649void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002650{
2651 struct nvme_ns *ns;
2652
Keith Busch32f0c4a2016-07-13 11:45:02 -06002653 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002654 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002655 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002656 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002657}
Ming Lin576d55d2016-02-10 10:03:32 -08002658EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002659
Keith Busch25646262016-01-04 09:10:57 -07002660void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002661{
2662 struct nvme_ns *ns;
2663
Keith Busch32f0c4a2016-07-13 11:45:02 -06002664 mutex_lock(&ctrl->namespaces_mutex);
2665 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002666 blk_mq_start_stopped_hw_queues(ns->queue, true);
2667 blk_mq_kick_requeue_list(ns->queue);
2668 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002669 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002670}
Ming Lin576d55d2016-02-10 10:03:32 -08002671EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002672
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002673int __init nvme_core_init(void)
2674{
2675 int result;
2676
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002677 nvme_wq = alloc_workqueue("nvme-wq",
2678 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2679 if (!nvme_wq)
2680 return -ENOMEM;
2681
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002682 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2683 &nvme_dev_fops);
2684 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002685 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002686 else if (result > 0)
2687 nvme_char_major = result;
2688
2689 nvme_class = class_create(THIS_MODULE, "nvme");
2690 if (IS_ERR(nvme_class)) {
2691 result = PTR_ERR(nvme_class);
2692 goto unregister_chrdev;
2693 }
2694
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002695 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002696
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002697unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002698 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002699destroy_wq:
2700 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002701 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002702}
2703
2704void nvme_core_exit(void)
2705{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002706 class_destroy(nvme_class);
2707 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002708 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002709}
Ming Lin576d55d2016-02-10 10:03:32 -08002710
2711MODULE_LICENSE("GPL");
2712MODULE_VERSION("1.0");
2713module_init(nvme_core_init);
2714module_exit(nvme_core_exit);