blob: 0f397a1c96977f32b9ee94aa4f76dd4b87076e92 [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
48unsigned char shutdown_timeout = 5;
49module_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 Hellwig2a842ac2017-06-03 09:38:04 +020076static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020077{
78 switch (nvme_req(req)->status & 0x7ff) {
79 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020080 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020081 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020082 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +020083 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020084 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +020085 case NVME_SC_WRITE_FAULT:
86 case NVME_SC_READ_ERROR:
87 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020088 return BLK_STS_MEDIUM;
89 default:
90 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020091 }
92}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020093
Christoph Hellwigf6324b12017-04-05 19:18:09 +020094static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +020095{
Christoph Hellwigf6324b12017-04-05 19:18:09 +020096 if (blk_noretry_request(req))
97 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020098 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +020099 return false;
100 if (jiffies - req->start_time >= req->timeout)
101 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200102 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200103 return false;
104 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200105}
106
107void nvme_complete_rq(struct request *req)
108{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200109 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
110 nvme_req(req)->retries++;
111 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
112 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200113 }
114
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200115 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200116}
117EXPORT_SYMBOL_GPL(nvme_complete_rq);
118
Ming Linc55a2fd2016-05-18 14:05:02 -0700119void nvme_cancel_request(struct request *req, void *data, bool reserved)
120{
121 int status;
122
123 if (!blk_mq_request_started(req))
124 return;
125
126 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
127 "Cancelling I/O %d", req->tag);
128
129 status = NVME_SC_ABORT_REQ;
130 if (blk_queue_dying(req->q))
131 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200132 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200133 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200134
Ming Linc55a2fd2016-05-18 14:05:02 -0700135}
136EXPORT_SYMBOL_GPL(nvme_cancel_request);
137
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200138bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
139 enum nvme_ctrl_state new_state)
140{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300141 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200142 bool changed = false;
143
144 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300145
146 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200147 switch (new_state) {
148 case NVME_CTRL_LIVE:
149 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200150 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200151 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900152 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200153 changed = true;
154 /* FALLTHRU */
155 default:
156 break;
157 }
158 break;
159 case NVME_CTRL_RESETTING:
160 switch (old_state) {
161 case NVME_CTRL_NEW:
162 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900163 changed = true;
164 /* FALLTHRU */
165 default:
166 break;
167 }
168 break;
169 case NVME_CTRL_RECONNECTING:
170 switch (old_state) {
171 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200172 changed = true;
173 /* FALLTHRU */
174 default:
175 break;
176 }
177 break;
178 case NVME_CTRL_DELETING:
179 switch (old_state) {
180 case NVME_CTRL_LIVE:
181 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900182 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200183 changed = true;
184 /* FALLTHRU */
185 default:
186 break;
187 }
188 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600189 case NVME_CTRL_DEAD:
190 switch (old_state) {
191 case NVME_CTRL_DELETING:
192 changed = true;
193 /* FALLTHRU */
194 default:
195 break;
196 }
197 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200198 default:
199 break;
200 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200201
202 if (changed)
203 ctrl->state = new_state;
204
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300205 spin_unlock_irq(&ctrl->lock);
206
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200207 return changed;
208}
209EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
210
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100211static void nvme_free_ns(struct kref *kref)
212{
213 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
214
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200215 if (ns->ndev)
216 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100217
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200218 if (ns->disk) {
219 spin_lock(&dev_list_lock);
220 ns->disk->private_data = NULL;
221 spin_unlock(&dev_list_lock);
222 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100223
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100224 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700225 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
226 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100227 kfree(ns);
228}
229
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100230static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100231{
232 kref_put(&ns->kref, nvme_free_ns);
233}
234
235static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
236{
237 struct nvme_ns *ns;
238
239 spin_lock(&dev_list_lock);
240 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800241 if (ns) {
242 if (!kref_get_unless_zero(&ns->kref))
243 goto fail;
244 if (!try_module_get(ns->ctrl->ops->module))
245 goto fail_put_ns;
246 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100247 spin_unlock(&dev_list_lock);
248
249 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800250
251fail_put_ns:
252 kref_put(&ns->kref, nvme_free_ns);
253fail:
254 spin_unlock(&dev_list_lock);
255 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100256}
257
Christoph Hellwig41609822015-11-20 09:00:02 +0100258struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200259 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100260{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100261 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100262 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100263
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200264 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100265 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200266 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100267 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200268 qid ? qid - 1 : 0);
269 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100270 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100271 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100272
Christoph Hellwig21d34712015-11-26 09:08:36 +0100273 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800274 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100275
Christoph Hellwig41609822015-11-20 09:00:02 +0100276 return req;
277}
Ming Lin576d55d2016-02-10 10:03:32 -0800278EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100279
Ming Lin8093f7c2016-04-12 13:10:14 -0600280static inline void nvme_setup_flush(struct nvme_ns *ns,
281 struct nvme_command *cmnd)
282{
283 memset(cmnd, 0, sizeof(*cmnd));
284 cmnd->common.opcode = nvme_cmd_flush;
285 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
286}
287
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200288static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600289 struct nvme_command *cmnd)
290{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100291 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600292 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100293 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600294
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100295 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600296 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200297 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600298
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100299 __rq_for_each_bio(bio, req) {
300 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
301 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
302
303 range[n].cattr = cpu_to_le32(0);
304 range[n].nlb = cpu_to_le32(nlb);
305 range[n].slba = cpu_to_le64(slba);
306 n++;
307 }
308
309 if (WARN_ON_ONCE(n != segments)) {
310 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200311 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100312 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600313
314 memset(cmnd, 0, sizeof(*cmnd));
315 cmnd->dsm.opcode = nvme_cmd_dsm;
316 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200317 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600318 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
319
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700320 req->special_vec.bv_page = virt_to_page(range);
321 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100322 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700323 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600324
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200325 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600326}
Ming Lin8093f7c2016-04-12 13:10:14 -0600327
Ming Lin8093f7c2016-04-12 13:10:14 -0600328static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
329 struct nvme_command *cmnd)
330{
331 u16 control = 0;
332 u32 dsmgmt = 0;
333
334 if (req->cmd_flags & REQ_FUA)
335 control |= NVME_RW_FUA;
336 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
337 control |= NVME_RW_LR;
338
339 if (req->cmd_flags & REQ_RAHEAD)
340 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
341
342 memset(cmnd, 0, sizeof(*cmnd));
343 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600344 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
345 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
346 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
347
348 if (ns->ms) {
349 switch (ns->pi_type) {
350 case NVME_NS_DPS_PI_TYPE3:
351 control |= NVME_RW_PRINFO_PRCHK_GUARD;
352 break;
353 case NVME_NS_DPS_PI_TYPE1:
354 case NVME_NS_DPS_PI_TYPE2:
355 control |= NVME_RW_PRINFO_PRCHK_GUARD |
356 NVME_RW_PRINFO_PRCHK_REF;
357 cmnd->rw.reftag = cpu_to_le32(
358 nvme_block_nr(ns, blk_rq_pos(req)));
359 break;
360 }
361 if (!blk_integrity_rq(req))
362 control |= NVME_RW_PRINFO_PRACT;
363 }
364
365 cmnd->rw.control = cpu_to_le16(control);
366 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
367}
368
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200369blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600370 struct nvme_command *cmd)
371{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200372 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600373
Christoph Hellwig987f6992017-04-05 19:18:08 +0200374 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200375 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200376 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200377 req->rq_flags |= RQF_DONTPREP;
378 }
379
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100380 switch (req_op(req)) {
381 case REQ_OP_DRV_IN:
382 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800383 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100384 break;
385 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600386 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100387 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200388 case REQ_OP_WRITE_ZEROES:
389 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100390 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600391 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100392 break;
393 case REQ_OP_READ:
394 case REQ_OP_WRITE:
Ming Lin8093f7c2016-04-12 13:10:14 -0600395 nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100396 break;
397 default:
398 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200399 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100400 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600401
James Smart721b3912016-10-21 23:33:34 +0300402 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600403 return ret;
404}
405EXPORT_SYMBOL_GPL(nvme_setup_cmd);
406
Christoph Hellwig41609822015-11-20 09:00:02 +0100407/*
408 * Returns 0 on success. If the result is negative, it's a Linux error code;
409 * if the result is positive, it's an NVM Express status code
410 */
411int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800412 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200413 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100414{
415 struct request *req;
416 int ret;
417
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200418 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100419 if (IS_ERR(req))
420 return PTR_ERR(req);
421
422 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
423
Christoph Hellwig21d34712015-11-26 09:08:36 +0100424 if (buffer && bufflen) {
425 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
426 if (ret)
427 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100428 }
429
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200430 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800431 if (result)
432 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200433 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
434 ret = -EINTR;
435 else
436 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100437 out:
438 blk_mq_free_request(req);
439 return ret;
440}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200441EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100442
443int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
444 void *buffer, unsigned bufflen)
445{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200446 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
447 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100448}
Ming Lin576d55d2016-02-10 10:03:32 -0800449EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100450
Keith Busch0b7f1f22015-10-23 09:47:28 -0600451int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
452 void __user *ubuffer, unsigned bufflen,
453 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
454 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100455{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200456 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600457 struct nvme_ns *ns = q->queuedata;
458 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100459 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600460 struct bio *bio = NULL;
461 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100462 int ret;
463
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200464 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100465 if (IS_ERR(req))
466 return PTR_ERR(req);
467
468 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
469
470 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100471 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
472 GFP_KERNEL);
473 if (ret)
474 goto out;
475 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100476
Keith Busch0b7f1f22015-10-23 09:47:28 -0600477 if (!disk)
478 goto submit;
479 bio->bi_bdev = bdget_disk(disk, 0);
480 if (!bio->bi_bdev) {
481 ret = -ENODEV;
482 goto out_unmap;
483 }
484
Keith Busche9fc63d2016-02-24 09:15:58 -0700485 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600486 struct bio_integrity_payload *bip;
487
488 meta = kmalloc(meta_len, GFP_KERNEL);
489 if (!meta) {
490 ret = -ENOMEM;
491 goto out_unmap;
492 }
493
494 if (write) {
495 if (copy_from_user(meta, meta_buffer,
496 meta_len)) {
497 ret = -EFAULT;
498 goto out_free_meta;
499 }
500 }
501
502 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700503 if (IS_ERR(bip)) {
504 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600505 goto out_free_meta;
506 }
507
508 bip->bip_iter.bi_size = meta_len;
509 bip->bip_iter.bi_sector = meta_seed;
510
511 ret = bio_integrity_add_page(bio, virt_to_page(meta),
512 meta_len, offset_in_page(meta));
513 if (ret != meta_len) {
514 ret = -ENOMEM;
515 goto out_free_meta;
516 }
517 }
518 }
519 submit:
520 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200521 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
522 ret = -EINTR;
523 else
524 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100525 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800526 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600527 if (meta && !ret && !write) {
528 if (copy_to_user(meta_buffer, meta, meta_len))
529 ret = -EFAULT;
530 }
531 out_free_meta:
532 kfree(meta);
533 out_unmap:
534 if (bio) {
535 if (disk && bio->bi_bdev)
536 bdput(bio->bi_bdev);
537 blk_rq_unmap_user(bio);
538 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100539 out:
540 blk_mq_free_request(req);
541 return ret;
542}
543
Keith Busch0b7f1f22015-10-23 09:47:28 -0600544int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
545 void __user *ubuffer, unsigned bufflen, u32 *result,
546 unsigned timeout)
547{
548 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
549 result, timeout);
550}
551
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200552static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200553{
554 struct nvme_ctrl *ctrl = rq->end_io_data;
555
556 blk_mq_free_request(rq);
557
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200558 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200559 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200560 "failed nvme_keep_alive_end_io error=%d\n",
561 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200562 return;
563 }
564
565 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
566}
567
568static int nvme_keep_alive(struct nvme_ctrl *ctrl)
569{
570 struct nvme_command c;
571 struct request *rq;
572
573 memset(&c, 0, sizeof(c));
574 c.common.opcode = nvme_admin_keep_alive;
575
576 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
577 NVME_QID_ANY);
578 if (IS_ERR(rq))
579 return PTR_ERR(rq);
580
581 rq->timeout = ctrl->kato * HZ;
582 rq->end_io_data = ctrl;
583
584 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
585
586 return 0;
587}
588
589static void nvme_keep_alive_work(struct work_struct *work)
590{
591 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
592 struct nvme_ctrl, ka_work);
593
594 if (nvme_keep_alive(ctrl)) {
595 /* allocation failure, reset the controller */
596 dev_err(ctrl->device, "keep-alive failed\n");
597 ctrl->ops->reset_ctrl(ctrl);
598 return;
599 }
600}
601
602void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
603{
604 if (unlikely(ctrl->kato == 0))
605 return;
606
607 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
608 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
609}
610EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
611
612void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
613{
614 if (unlikely(ctrl->kato == 0))
615 return;
616
617 cancel_delayed_work_sync(&ctrl->ka_work);
618}
619EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
620
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100621int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100622{
623 struct nvme_command c = { };
624 int error;
625
626 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
627 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200628 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100629
630 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
631 if (!*id)
632 return -ENOMEM;
633
634 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
635 sizeof(struct nvme_id_ctrl));
636 if (error)
637 kfree(*id);
638 return error;
639}
640
Keith Busch540c8012015-10-22 15:45:06 -0600641static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
642{
643 struct nvme_command c = { };
644
645 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200646 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600647 c.identify.nsid = cpu_to_le32(nsid);
648 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
649}
650
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100651int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100652 struct nvme_id_ns **id)
653{
654 struct nvme_command c = { };
655 int error;
656
657 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200658 c.identify.opcode = nvme_admin_identify;
659 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200660 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100661
662 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
663 if (!*id)
664 return -ENOMEM;
665
666 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
667 sizeof(struct nvme_id_ns));
668 if (error)
669 kfree(*id);
670 return error;
671}
672
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100673int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700674 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100675{
676 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800677 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100678 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100679
680 memset(&c, 0, sizeof(c));
681 c.features.opcode = nvme_admin_get_features;
682 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100683 c.features.fid = cpu_to_le32(fid);
684
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800685 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200686 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700687 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800688 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100689 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100690}
691
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100692int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700693 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100694{
695 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800696 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100697 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100698
699 memset(&c, 0, sizeof(c));
700 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100701 c.features.fid = cpu_to_le32(fid);
702 c.features.dword11 = cpu_to_le32(dword11);
703
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800704 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700705 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700706 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800707 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100708 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100709}
710
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100711int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100712{
713 struct nvme_command c = { };
714 int error;
715
716 c.common.opcode = nvme_admin_get_log_page,
717 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
718 c.common.cdw10[0] = cpu_to_le32(
719 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
720 NVME_LOG_SMART),
721
722 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
723 if (!*log)
724 return -ENOMEM;
725
726 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
727 sizeof(struct nvme_smart_log));
728 if (error)
729 kfree(*log);
730 return error;
731}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100732
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100733int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
734{
735 u32 q_count = (*count - 1) | ((*count - 1) << 16);
736 u32 result;
737 int status, nr_io_queues;
738
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700739 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100740 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200741 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100742 return status;
743
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200744 /*
745 * Degraded controllers might return an error when setting the queue
746 * count. We still want to be able to bring them online and offer
747 * access to the admin queue, as that might be only way to fix them up.
748 */
749 if (status > 0) {
750 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
751 *count = 0;
752 } else {
753 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
754 *count = min(*count, nr_io_queues);
755 }
756
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100757 return 0;
758}
Ming Lin576d55d2016-02-10 10:03:32 -0800759EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100760
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100761static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
762{
763 struct nvme_user_io io;
764 struct nvme_command c;
765 unsigned length, meta_len;
766 void __user *metadata;
767
768 if (copy_from_user(&io, uio, sizeof(io)))
769 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700770 if (io.flags)
771 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100772
773 switch (io.opcode) {
774 case nvme_cmd_write:
775 case nvme_cmd_read:
776 case nvme_cmd_compare:
777 break;
778 default:
779 return -EINVAL;
780 }
781
782 length = (io.nblocks + 1) << ns->lba_shift;
783 meta_len = (io.nblocks + 1) * ns->ms;
784 metadata = (void __user *)(uintptr_t)io.metadata;
785
786 if (ns->ext) {
787 length += meta_len;
788 meta_len = 0;
789 } else if (meta_len) {
790 if ((io.metadata & 3) || !io.metadata)
791 return -EINVAL;
792 }
793
794 memset(&c, 0, sizeof(c));
795 c.rw.opcode = io.opcode;
796 c.rw.flags = io.flags;
797 c.rw.nsid = cpu_to_le32(ns->ns_id);
798 c.rw.slba = cpu_to_le64(io.slba);
799 c.rw.length = cpu_to_le16(io.nblocks);
800 c.rw.control = cpu_to_le16(io.control);
801 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
802 c.rw.reftag = cpu_to_le32(io.reftag);
803 c.rw.apptag = cpu_to_le16(io.apptag);
804 c.rw.appmask = cpu_to_le16(io.appmask);
805
806 return __nvme_submit_user_cmd(ns->queue, &c,
807 (void __user *)(uintptr_t)io.addr, length,
808 metadata, meta_len, io.slba, NULL, 0);
809}
810
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100811static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100812 struct nvme_passthru_cmd __user *ucmd)
813{
814 struct nvme_passthru_cmd cmd;
815 struct nvme_command c;
816 unsigned timeout = 0;
817 int status;
818
819 if (!capable(CAP_SYS_ADMIN))
820 return -EACCES;
821 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
822 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700823 if (cmd.flags)
824 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100825
826 memset(&c, 0, sizeof(c));
827 c.common.opcode = cmd.opcode;
828 c.common.flags = cmd.flags;
829 c.common.nsid = cpu_to_le32(cmd.nsid);
830 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
831 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
832 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
833 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
834 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
835 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
836 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
837 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
838
839 if (cmd.timeout_ms)
840 timeout = msecs_to_jiffies(cmd.timeout_ms);
841
842 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100843 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100844 &cmd.result, timeout);
845 if (status >= 0) {
846 if (put_user(cmd.result, &ucmd->result))
847 return -EFAULT;
848 }
849
850 return status;
851}
852
853static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
854 unsigned int cmd, unsigned long arg)
855{
856 struct nvme_ns *ns = bdev->bd_disk->private_data;
857
858 switch (cmd) {
859 case NVME_IOCTL_ID:
860 force_successful_syscall_return();
861 return ns->ns_id;
862 case NVME_IOCTL_ADMIN_CMD:
863 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
864 case NVME_IOCTL_IO_CMD:
865 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
866 case NVME_IOCTL_SUBMIT_IO:
867 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100868#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100869 case SG_GET_VERSION_NUM:
870 return nvme_sg_get_version_num((void __user *)arg);
871 case SG_IO:
872 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100873#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100874 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100875#ifdef CONFIG_NVM
876 if (ns->ndev)
877 return nvme_nvm_ioctl(ns, cmd, arg);
878#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700879 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100880 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700881 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100882 return -ENOTTY;
883 }
884}
885
886#ifdef CONFIG_COMPAT
887static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
888 unsigned int cmd, unsigned long arg)
889{
890 switch (cmd) {
891 case SG_IO:
892 return -ENOIOCTLCMD;
893 }
894 return nvme_ioctl(bdev, mode, cmd, arg);
895}
896#else
897#define nvme_compat_ioctl NULL
898#endif
899
900static int nvme_open(struct block_device *bdev, fmode_t mode)
901{
902 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
903}
904
905static void nvme_release(struct gendisk *disk, fmode_t mode)
906{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800907 struct nvme_ns *ns = disk->private_data;
908
909 module_put(ns->ctrl->ops->module);
910 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100911}
912
913static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
914{
915 /* some standard values */
916 geo->heads = 1 << 6;
917 geo->sectors = 1 << 5;
918 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
919 return 0;
920}
921
922#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +0200923static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
924 u16 bs)
925{
926 struct nvme_ns *ns = disk->private_data;
927 u16 old_ms = ns->ms;
928 u8 pi_type = 0;
929
930 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
931 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
932
933 /* PI implementation requires metadata equal t10 pi tuple size */
934 if (ns->ms == sizeof(struct t10_pi_tuple))
935 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
936
937 if (blk_get_integrity(disk) &&
938 (ns->pi_type != pi_type || ns->ms != old_ms ||
939 bs != queue_logical_block_size(disk->queue) ||
940 (ns->ms && ns->ext)))
941 blk_integrity_unregister(disk);
942
943 ns->pi_type = pi_type;
944}
945
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100946static void nvme_init_integrity(struct nvme_ns *ns)
947{
948 struct blk_integrity integrity;
949
Jay Freyenseefa9a89f2016-07-20 21:26:16 -0600950 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100951 switch (ns->pi_type) {
952 case NVME_NS_DPS_PI_TYPE3:
953 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000954 integrity.tag_size = sizeof(u16) + sizeof(u32);
955 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100956 break;
957 case NVME_NS_DPS_PI_TYPE1:
958 case NVME_NS_DPS_PI_TYPE2:
959 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000960 integrity.tag_size = sizeof(u16);
961 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100962 break;
963 default:
964 integrity.profile = NULL;
965 break;
966 }
967 integrity.tuple_size = ns->ms;
968 blk_integrity_register(ns->disk, &integrity);
969 blk_queue_max_integrity_segments(ns->queue, 1);
970}
971#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +0200972static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
973 u16 bs)
974{
975}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100976static void nvme_init_integrity(struct nvme_ns *ns)
977{
978}
979#endif /* CONFIG_BLK_DEV_INTEGRITY */
980
981static void nvme_config_discard(struct nvme_ns *ns)
982{
Keith Busch08095e72016-03-04 13:15:17 -0700983 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100984 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700985
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100986 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
987 NVME_DSM_MAX_RANGES);
988
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100989 ns->queue->limits.discard_alignment = logical_block_size;
990 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800991 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100992 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100993 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +0200994
995 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
996 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100997}
998
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200999static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001000{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001001 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001002 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001003 return -ENODEV;
1004 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001005
1006 if ((*id)->ncap == 0) {
1007 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001008 return -ENODEV;
1009 }
1010
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001011 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001012 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001013 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001014 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
1015
1016 return 0;
1017}
1018
1019static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1020{
1021 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001022 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001023
1024 /*
1025 * If identify namespace failed, use default 512 byte block size so
1026 * block layer can use before failing read/write for 0 capacity.
1027 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001028 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001029 if (ns->lba_shift == 0)
1030 ns->lba_shift = 9;
1031 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001032
1033 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001034
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001035 if (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
1036 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001037 blk_queue_logical_block_size(ns->queue, bs);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001038 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001039 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001040 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1041 set_capacity(disk, 0);
1042 else
1043 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1044
1045 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1046 nvme_config_discard(ns);
1047 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001048}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001049
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001050static int nvme_revalidate_disk(struct gendisk *disk)
1051{
1052 struct nvme_ns *ns = disk->private_data;
1053 struct nvme_id_ns *id = NULL;
1054 int ret;
1055
1056 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1057 set_capacity(disk, 0);
1058 return -ENODEV;
1059 }
1060
1061 ret = nvme_revalidate_ns(ns, &id);
1062 if (ret)
1063 return ret;
1064
1065 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001066 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001067
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001068 return 0;
1069}
1070
1071static char nvme_pr_type(enum pr_type type)
1072{
1073 switch (type) {
1074 case PR_WRITE_EXCLUSIVE:
1075 return 1;
1076 case PR_EXCLUSIVE_ACCESS:
1077 return 2;
1078 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1079 return 3;
1080 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1081 return 4;
1082 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1083 return 5;
1084 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1085 return 6;
1086 default:
1087 return 0;
1088 }
1089};
1090
1091static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1092 u64 key, u64 sa_key, u8 op)
1093{
1094 struct nvme_ns *ns = bdev->bd_disk->private_data;
1095 struct nvme_command c;
1096 u8 data[16] = { 0, };
1097
1098 put_unaligned_le64(key, &data[0]);
1099 put_unaligned_le64(sa_key, &data[8]);
1100
1101 memset(&c, 0, sizeof(c));
1102 c.common.opcode = op;
1103 c.common.nsid = cpu_to_le32(ns->ns_id);
1104 c.common.cdw10[0] = cpu_to_le32(cdw10);
1105
1106 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1107}
1108
1109static int nvme_pr_register(struct block_device *bdev, u64 old,
1110 u64 new, unsigned flags)
1111{
1112 u32 cdw10;
1113
1114 if (flags & ~PR_FL_IGNORE_KEY)
1115 return -EOPNOTSUPP;
1116
1117 cdw10 = old ? 2 : 0;
1118 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1119 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1120 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1121}
1122
1123static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1124 enum pr_type type, unsigned flags)
1125{
1126 u32 cdw10;
1127
1128 if (flags & ~PR_FL_IGNORE_KEY)
1129 return -EOPNOTSUPP;
1130
1131 cdw10 = nvme_pr_type(type) << 8;
1132 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1133 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1134}
1135
1136static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1137 enum pr_type type, bool abort)
1138{
1139 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1140 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1141}
1142
1143static int nvme_pr_clear(struct block_device *bdev, u64 key)
1144{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001145 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001146 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1147}
1148
1149static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1150{
1151 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1152 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1153}
1154
1155static const struct pr_ops nvme_pr_ops = {
1156 .pr_register = nvme_pr_register,
1157 .pr_reserve = nvme_pr_reserve,
1158 .pr_release = nvme_pr_release,
1159 .pr_preempt = nvme_pr_preempt,
1160 .pr_clear = nvme_pr_clear,
1161};
1162
Scott Bauera98e58e52017-02-03 12:50:32 -07001163#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001164int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1165 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001166{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001167 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001168 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001169
1170 memset(&cmd, 0, sizeof(cmd));
1171 if (send)
1172 cmd.common.opcode = nvme_admin_security_send;
1173 else
1174 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001175 cmd.common.nsid = 0;
1176 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1177 cmd.common.cdw10[1] = cpu_to_le32(len);
1178
1179 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1180 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1181}
1182EXPORT_SYMBOL_GPL(nvme_sec_submit);
1183#endif /* CONFIG_BLK_SED_OPAL */
1184
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001185static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001186 .owner = THIS_MODULE,
1187 .ioctl = nvme_ioctl,
1188 .compat_ioctl = nvme_compat_ioctl,
1189 .open = nvme_open,
1190 .release = nvme_release,
1191 .getgeo = nvme_getgeo,
1192 .revalidate_disk= nvme_revalidate_disk,
1193 .pr_ops = &nvme_pr_ops,
1194};
1195
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001196static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1197{
1198 unsigned long timeout =
1199 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1200 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1201 int ret;
1202
1203 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001204 if (csts == ~0)
1205 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001206 if ((csts & NVME_CSTS_RDY) == bit)
1207 break;
1208
1209 msleep(100);
1210 if (fatal_signal_pending(current))
1211 return -EINTR;
1212 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001213 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001214 "Device not ready; aborting %s\n", enabled ?
1215 "initialisation" : "reset");
1216 return -ENODEV;
1217 }
1218 }
1219
1220 return ret;
1221}
1222
1223/*
1224 * If the device has been passed off to us in an enabled state, just clear
1225 * the enabled bit. The spec says we should set the 'shutdown notification
1226 * bits', but doing so may cause the device to complete commands to the
1227 * admin queue ... and we don't know what memory that might be pointing at!
1228 */
1229int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1230{
1231 int ret;
1232
1233 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1234 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1235
1236 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1237 if (ret)
1238 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001239
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001240 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001241 msleep(NVME_QUIRK_DELAY_AMOUNT);
1242
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001243 return nvme_wait_ready(ctrl, cap, false);
1244}
Ming Lin576d55d2016-02-10 10:03:32 -08001245EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001246
1247int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1248{
1249 /*
1250 * Default to a 4K page size, with the intention to update this
1251 * path in the future to accomodate architectures with differing
1252 * kernel and IO page sizes.
1253 */
1254 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1255 int ret;
1256
1257 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001258 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001259 "Minimum device page size %u too large for host (%u)\n",
1260 1 << dev_page_min, 1 << page_shift);
1261 return -ENODEV;
1262 }
1263
1264 ctrl->page_size = 1 << page_shift;
1265
1266 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1267 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1268 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1269 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1270 ctrl->ctrl_config |= NVME_CC_ENABLE;
1271
1272 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1273 if (ret)
1274 return ret;
1275 return nvme_wait_ready(ctrl, cap, true);
1276}
Ming Lin576d55d2016-02-10 10:03:32 -08001277EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001278
1279int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1280{
1281 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1282 u32 csts;
1283 int ret;
1284
1285 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1286 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1287
1288 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1289 if (ret)
1290 return ret;
1291
1292 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1293 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1294 break;
1295
1296 msleep(100);
1297 if (fatal_signal_pending(current))
1298 return -EINTR;
1299 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001300 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001301 "Device shutdown incomplete; abort shutdown\n");
1302 return -ENODEV;
1303 }
1304 }
1305
1306 return ret;
1307}
Ming Lin576d55d2016-02-10 10:03:32 -08001308EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001309
Christoph Hellwigda358252016-03-02 18:07:11 +01001310static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1311 struct request_queue *q)
1312{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001313 bool vwc = false;
1314
Christoph Hellwigda358252016-03-02 18:07:11 +01001315 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001316 u32 max_segments =
1317 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1318
Christoph Hellwigda358252016-03-02 18:07:11 +01001319 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001320 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001321 }
Keith Busche6282ae2016-12-19 11:37:50 -05001322 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1323 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001324 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001325 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1326 vwc = true;
1327 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001328}
1329
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001330static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1331{
1332 /*
1333 * APST (Autonomous Power State Transition) lets us program a
1334 * table of power state transitions that the controller will
1335 * perform automatically. We configure it with a simple
1336 * heuristic: we are willing to spend at most 2% of the time
1337 * transitioning between power states. Therefore, when running
1338 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001339 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001340 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001341 * the requested maximum latency.
1342 *
1343 * We will not autonomously enter any non-operational state for
1344 * which the total latency exceeds ps_max_latency_us. Users
1345 * can set ps_max_latency_us to zero to turn off APST.
1346 */
1347
1348 unsigned apste;
1349 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001350 u64 max_lat_us = 0;
1351 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001352 int ret;
1353
1354 /*
1355 * If APST isn't supported or if we haven't been initialized yet,
1356 * then don't do anything.
1357 */
1358 if (!ctrl->apsta)
1359 return;
1360
1361 if (ctrl->npss > 31) {
1362 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1363 return;
1364 }
1365
1366 table = kzalloc(sizeof(*table), GFP_KERNEL);
1367 if (!table)
1368 return;
1369
1370 if (ctrl->ps_max_latency_us == 0) {
1371 /* Turn off APST. */
1372 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001373 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001374 } else {
1375 __le64 target = cpu_to_le64(0);
1376 int state;
1377
1378 /*
1379 * Walk through all states from lowest- to highest-power.
1380 * According to the spec, lower-numbered states use more
1381 * power. NPSS, despite the name, is the index of the
1382 * lowest-power state, not the number of states.
1383 */
1384 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001385 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001386
1387 if (target)
1388 table->entries[state] = target;
1389
1390 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001391 * Don't allow transitions to the deepest state
1392 * if it's quirked off.
1393 */
1394 if (state == ctrl->npss &&
1395 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1396 continue;
1397
1398 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001399 * Is this state a useful non-operational state for
1400 * higher-power states to autonomously transition to?
1401 */
1402 if (!(ctrl->psd[state].flags &
1403 NVME_PS_FLAGS_NON_OP_STATE))
1404 continue;
1405
Kai-Heng Fengda875912017-06-07 15:25:42 +08001406 exit_latency_us =
1407 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1408 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001409 continue;
1410
Kai-Heng Fengda875912017-06-07 15:25:42 +08001411 total_latency_us =
1412 exit_latency_us +
1413 le32_to_cpu(ctrl->psd[state].entry_lat);
1414
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001415 /*
1416 * This state is good. Use it as the APST idle
1417 * target for higher power states.
1418 */
1419 transition_ms = total_latency_us + 19;
1420 do_div(transition_ms, 20);
1421 if (transition_ms > (1 << 24) - 1)
1422 transition_ms = (1 << 24) - 1;
1423
1424 target = cpu_to_le64((state << 3) |
1425 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001426
1427 if (max_ps == -1)
1428 max_ps = state;
1429
1430 if (total_latency_us > max_lat_us)
1431 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001432 }
1433
1434 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001435
1436 if (max_ps == -1) {
1437 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1438 } else {
1439 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1440 max_ps, max_lat_us, (int)sizeof(*table), table);
1441 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001442 }
1443
1444 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1445 table, sizeof(*table), NULL);
1446 if (ret)
1447 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1448
1449 kfree(table);
1450}
1451
1452static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1453{
1454 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1455 u64 latency;
1456
1457 switch (val) {
1458 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1459 case PM_QOS_LATENCY_ANY:
1460 latency = U64_MAX;
1461 break;
1462
1463 default:
1464 latency = val;
1465 }
1466
1467 if (ctrl->ps_max_latency_us != latency) {
1468 ctrl->ps_max_latency_us = latency;
1469 nvme_configure_apst(ctrl);
1470 }
1471}
1472
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001473struct nvme_core_quirk_entry {
1474 /*
1475 * NVMe model and firmware strings are padded with spaces. For
1476 * simplicity, strings in the quirk table are padded with NULLs
1477 * instead.
1478 */
1479 u16 vid;
1480 const char *mn;
1481 const char *fr;
1482 unsigned long quirks;
1483};
1484
1485static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001486 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001487 /*
1488 * This Toshiba device seems to die using any APST states. See:
1489 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1490 */
1491 .vid = 0x1179,
1492 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001493 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001494 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001495};
1496
1497/* match is null-terminated but idstr is space-padded. */
1498static bool string_matches(const char *idstr, const char *match, size_t len)
1499{
1500 size_t matchlen;
1501
1502 if (!match)
1503 return true;
1504
1505 matchlen = strlen(match);
1506 WARN_ON_ONCE(matchlen > len);
1507
1508 if (memcmp(idstr, match, matchlen))
1509 return false;
1510
1511 for (; matchlen < len; matchlen++)
1512 if (idstr[matchlen] != ' ')
1513 return false;
1514
1515 return true;
1516}
1517
1518static bool quirk_matches(const struct nvme_id_ctrl *id,
1519 const struct nvme_core_quirk_entry *q)
1520{
1521 return q->vid == le16_to_cpu(id->vid) &&
1522 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1523 string_matches(id->fr, q->fr, sizeof(id->fr));
1524}
1525
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001526/*
1527 * Initialize the cached copies of the Identify data and various controller
1528 * register in our nvme_ctrl structure. This should be called as soon as
1529 * the admin queue is fully up and running.
1530 */
1531int nvme_init_identify(struct nvme_ctrl *ctrl)
1532{
1533 struct nvme_id_ctrl *id;
1534 u64 cap;
1535 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001536 u32 max_hw_sectors;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001537 u8 prev_apsta;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001538
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001539 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1540 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001541 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001542 return ret;
1543 }
1544
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001545 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1546 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001547 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001548 return ret;
1549 }
1550 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1551
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001552 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001553 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1554
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001555 ret = nvme_identify_ctrl(ctrl, &id);
1556 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001557 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001558 return -EIO;
1559 }
1560
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001561 if (!ctrl->identified) {
1562 /*
1563 * Check for quirks. Quirk can depend on firmware version,
1564 * so, in principle, the set of quirks present can change
1565 * across a reset. As a possible future enhancement, we
1566 * could re-scan for quirks every time we reinitialize
1567 * the device, but we'd have to make sure that the driver
1568 * behaves intelligently if the quirks change.
1569 */
1570
1571 int i;
1572
1573 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1574 if (quirk_matches(id, &core_quirks[i]))
1575 ctrl->quirks |= core_quirks[i].quirks;
1576 }
1577 }
1578
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001579 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
1580 dev_warn(ctrl->dev, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
1581 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1582 }
1583
Scott Bauer8a9ae522017-02-17 13:59:40 +01001584 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001585 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001586 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001587 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001588 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001589 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001590 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1591 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1592 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1593 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001594 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001595 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001596 max_hw_sectors = UINT_MAX;
1597 ctrl->max_hw_sectors =
1598 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001599
Christoph Hellwigda358252016-03-02 18:07:11 +01001600 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001601 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001602 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001603
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001604 ctrl->npss = id->npss;
1605 prev_apsta = ctrl->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001606 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1607 if (force_apst && id->apsta) {
1608 dev_warn(ctrl->dev, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
1609 ctrl->apsta = 1;
1610 } else {
1611 ctrl->apsta = 0;
1612 }
1613 } else {
1614 ctrl->apsta = id->apsta;
1615 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001616 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1617
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001618 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001619 ctrl->icdoff = le16_to_cpu(id->icdoff);
1620 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1621 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1622 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1623
1624 /*
1625 * In fabrics we need to verify the cntlid matches the
1626 * admin connect
1627 */
1628 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1629 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001630
1631 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1632 dev_err(ctrl->dev,
1633 "keep-alive support is mandatory for fabrics\n");
1634 ret = -EINVAL;
1635 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001636 } else {
1637 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001638 ctrl->hmpre = le32_to_cpu(id->hmpre);
1639 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001640 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001641
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001642 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001643
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001644 if (ctrl->apsta && !prev_apsta)
1645 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1646 else if (!ctrl->apsta && prev_apsta)
1647 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1648
1649 nvme_configure_apst(ctrl);
1650
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001651 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001652
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001653 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001654}
Ming Lin576d55d2016-02-10 10:03:32 -08001655EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001656
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001657static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001658{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001659 struct nvme_ctrl *ctrl;
1660 int instance = iminor(inode);
1661 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001662
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001663 spin_lock(&dev_list_lock);
1664 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1665 if (ctrl->instance != instance)
1666 continue;
1667
1668 if (!ctrl->admin_q) {
1669 ret = -EWOULDBLOCK;
1670 break;
1671 }
1672 if (!kref_get_unless_zero(&ctrl->kref))
1673 break;
1674 file->private_data = ctrl;
1675 ret = 0;
1676 break;
1677 }
1678 spin_unlock(&dev_list_lock);
1679
1680 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001681}
1682
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001683static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001684{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001685 nvme_put_ctrl(file->private_data);
1686 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001687}
1688
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001689static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1690{
1691 struct nvme_ns *ns;
1692 int ret;
1693
1694 mutex_lock(&ctrl->namespaces_mutex);
1695 if (list_empty(&ctrl->namespaces)) {
1696 ret = -ENOTTY;
1697 goto out_unlock;
1698 }
1699
1700 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1701 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001702 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001703 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1704 ret = -EINVAL;
1705 goto out_unlock;
1706 }
1707
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001708 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001709 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1710 kref_get(&ns->kref);
1711 mutex_unlock(&ctrl->namespaces_mutex);
1712
1713 ret = nvme_user_cmd(ctrl, ns, argp);
1714 nvme_put_ns(ns);
1715 return ret;
1716
1717out_unlock:
1718 mutex_unlock(&ctrl->namespaces_mutex);
1719 return ret;
1720}
1721
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001722static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1723 unsigned long arg)
1724{
1725 struct nvme_ctrl *ctrl = file->private_data;
1726 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001727
1728 switch (cmd) {
1729 case NVME_IOCTL_ADMIN_CMD:
1730 return nvme_user_cmd(ctrl, NULL, argp);
1731 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001732 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001733 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001734 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001735 return ctrl->ops->reset_ctrl(ctrl);
1736 case NVME_IOCTL_SUBSYS_RESET:
1737 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001738 case NVME_IOCTL_RESCAN:
1739 nvme_queue_scan(ctrl);
1740 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001741 default:
1742 return -ENOTTY;
1743 }
1744}
1745
1746static const struct file_operations nvme_dev_fops = {
1747 .owner = THIS_MODULE,
1748 .open = nvme_dev_open,
1749 .release = nvme_dev_release,
1750 .unlocked_ioctl = nvme_dev_ioctl,
1751 .compat_ioctl = nvme_dev_ioctl,
1752};
1753
1754static ssize_t nvme_sysfs_reset(struct device *dev,
1755 struct device_attribute *attr, const char *buf,
1756 size_t count)
1757{
1758 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1759 int ret;
1760
1761 ret = ctrl->ops->reset_ctrl(ctrl);
1762 if (ret < 0)
1763 return ret;
1764 return count;
1765}
1766static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1767
Keith Busch9ec3bb22016-04-29 15:45:18 -06001768static ssize_t nvme_sysfs_rescan(struct device *dev,
1769 struct device_attribute *attr, const char *buf,
1770 size_t count)
1771{
1772 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1773
1774 nvme_queue_scan(ctrl);
1775 return count;
1776}
1777static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1778
Keith Busch118472a2016-02-18 09:57:48 -07001779static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1780 char *buf)
1781{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001782 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001783 struct nvme_ctrl *ctrl = ns->ctrl;
1784 int serial_len = sizeof(ctrl->serial);
1785 int model_len = sizeof(ctrl->model);
1786
1787 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1788 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1789
1790 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1791 return sprintf(buf, "eui.%8phN\n", ns->eui);
1792
1793 while (ctrl->serial[serial_len - 1] == ' ')
1794 serial_len--;
1795 while (ctrl->model[model_len - 1] == ' ')
1796 model_len--;
1797
1798 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1799 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1800}
1801static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1802
Keith Busch2b9b6e82015-12-22 10:10:45 -07001803static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1804 char *buf)
1805{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001806 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001807 return sprintf(buf, "%pU\n", ns->uuid);
1808}
1809static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1810
1811static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1812 char *buf)
1813{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001814 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001815 return sprintf(buf, "%8phd\n", ns->eui);
1816}
1817static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1818
1819static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1820 char *buf)
1821{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001822 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001823 return sprintf(buf, "%d\n", ns->ns_id);
1824}
1825static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1826
1827static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001828 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001829 &dev_attr_uuid.attr,
1830 &dev_attr_eui.attr,
1831 &dev_attr_nsid.attr,
1832 NULL,
1833};
1834
Ming Lin1a353d82016-06-13 16:45:24 +02001835static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001836 struct attribute *a, int n)
1837{
1838 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001839 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001840
1841 if (a == &dev_attr_uuid.attr) {
1842 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1843 return 0;
1844 }
1845 if (a == &dev_attr_eui.attr) {
1846 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1847 return 0;
1848 }
1849 return a->mode;
1850}
1851
1852static const struct attribute_group nvme_ns_attr_group = {
1853 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001854 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001855};
1856
Ming Lin931e1c22016-02-26 13:24:19 -08001857#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001858static ssize_t field##_show(struct device *dev, \
1859 struct device_attribute *attr, char *buf) \
1860{ \
1861 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1862 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1863} \
1864static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1865
Ming Lin931e1c22016-02-26 13:24:19 -08001866#define nvme_show_int_function(field) \
1867static ssize_t field##_show(struct device *dev, \
1868 struct device_attribute *attr, char *buf) \
1869{ \
1870 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1871 return sprintf(buf, "%d\n", ctrl->field); \
1872} \
1873static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1874
1875nvme_show_str_function(model);
1876nvme_show_str_function(serial);
1877nvme_show_str_function(firmware_rev);
1878nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001879
Ming Lin1a353d82016-06-13 16:45:24 +02001880static ssize_t nvme_sysfs_delete(struct device *dev,
1881 struct device_attribute *attr, const char *buf,
1882 size_t count)
1883{
1884 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1885
1886 if (device_remove_file_self(dev, attr))
1887 ctrl->ops->delete_ctrl(ctrl);
1888 return count;
1889}
1890static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1891
1892static ssize_t nvme_sysfs_show_transport(struct device *dev,
1893 struct device_attribute *attr,
1894 char *buf)
1895{
1896 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1897
1898 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1899}
1900static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1901
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001902static ssize_t nvme_sysfs_show_state(struct device *dev,
1903 struct device_attribute *attr,
1904 char *buf)
1905{
1906 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1907 static const char *const state_name[] = {
1908 [NVME_CTRL_NEW] = "new",
1909 [NVME_CTRL_LIVE] = "live",
1910 [NVME_CTRL_RESETTING] = "resetting",
1911 [NVME_CTRL_RECONNECTING]= "reconnecting",
1912 [NVME_CTRL_DELETING] = "deleting",
1913 [NVME_CTRL_DEAD] = "dead",
1914 };
1915
1916 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1917 state_name[ctrl->state])
1918 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1919
1920 return sprintf(buf, "unknown state\n");
1921}
1922
1923static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
1924
Ming Lin1a353d82016-06-13 16:45:24 +02001925static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1926 struct device_attribute *attr,
1927 char *buf)
1928{
1929 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1930
1931 return snprintf(buf, PAGE_SIZE, "%s\n",
1932 ctrl->ops->get_subsysnqn(ctrl));
1933}
1934static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1935
1936static ssize_t nvme_sysfs_show_address(struct device *dev,
1937 struct device_attribute *attr,
1938 char *buf)
1939{
1940 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1941
1942 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1943}
1944static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1945
Keith Busch779ff7562016-01-12 15:09:31 -07001946static struct attribute *nvme_dev_attrs[] = {
1947 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001948 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001949 &dev_attr_model.attr,
1950 &dev_attr_serial.attr,
1951 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001952 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02001953 &dev_attr_delete_controller.attr,
1954 &dev_attr_transport.attr,
1955 &dev_attr_subsysnqn.attr,
1956 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001957 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001958 NULL
1959};
1960
Ming Lin1a353d82016-06-13 16:45:24 +02001961#define CHECK_ATTR(ctrl, a, name) \
1962 if ((a) == &dev_attr_##name.attr && \
1963 !(ctrl)->ops->get_##name) \
1964 return 0
1965
1966static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1967 struct attribute *a, int n)
1968{
1969 struct device *dev = container_of(kobj, struct device, kobj);
1970 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1971
1972 if (a == &dev_attr_delete_controller.attr) {
1973 if (!ctrl->ops->delete_ctrl)
1974 return 0;
1975 }
1976
1977 CHECK_ATTR(ctrl, a, subsysnqn);
1978 CHECK_ATTR(ctrl, a, address);
1979
1980 return a->mode;
1981}
1982
Keith Busch779ff7562016-01-12 15:09:31 -07001983static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02001984 .attrs = nvme_dev_attrs,
1985 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07001986};
1987
1988static const struct attribute_group *nvme_dev_attr_groups[] = {
1989 &nvme_dev_attrs_group,
1990 NULL,
1991};
1992
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001993static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1994{
1995 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1996 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1997
1998 return nsa->ns_id - nsb->ns_id;
1999}
2000
Keith Busch32f0c4a2016-07-13 11:45:02 -06002001static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002002{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002003 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002004
Keith Busch32f0c4a2016-07-13 11:45:02 -06002005 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002006 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002007 if (ns->ns_id == nsid) {
2008 kref_get(&ns->kref);
2009 ret = ns;
2010 break;
2011 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002012 if (ns->ns_id > nsid)
2013 break;
2014 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002015 mutex_unlock(&ctrl->namespaces_mutex);
2016 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002017}
2018
2019static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2020{
2021 struct nvme_ns *ns;
2022 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002023 struct nvme_id_ns *id;
2024 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002025 int node = dev_to_node(ctrl->dev);
2026
2027 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2028 if (!ns)
2029 return;
2030
Keith Busch075790e2016-02-24 09:15:53 -07002031 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2032 if (ns->instance < 0)
2033 goto out_free_ns;
2034
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002035 ns->queue = blk_mq_init_queue(ctrl->tagset);
2036 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002037 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002038 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2039 ns->queue->queuedata = ns;
2040 ns->ctrl = ctrl;
2041
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002042 kref_init(&ns->kref);
2043 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002044 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002045
2046 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002047 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002048
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002049 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002050
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002051 if (nvme_revalidate_ns(ns, &id))
2052 goto out_free_queue;
2053
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002054 if (nvme_nvm_ns_supported(ns, id) &&
2055 nvme_nvm_register(ns, disk_name, node)) {
2056 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
2057 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002058 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002059
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002060 disk = alloc_disk_node(0, node);
2061 if (!disk)
2062 goto out_free_id;
2063
2064 disk->fops = &nvme_fops;
2065 disk->private_data = ns;
2066 disk->queue = ns->queue;
2067 disk->flags = GENHD_FL_EXT_DEVT;
2068 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2069 ns->disk = disk;
2070
2071 __nvme_revalidate_disk(disk, id);
2072
Keith Busch32f0c4a2016-07-13 11:45:02 -06002073 mutex_lock(&ctrl->namespaces_mutex);
2074 list_add_tail(&ns->list, &ctrl->namespaces);
2075 mutex_unlock(&ctrl->namespaces_mutex);
2076
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002077 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002078
2079 kfree(id);
2080
Dan Williams0d52c7562016-06-15 19:44:20 -07002081 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002082 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2083 &nvme_ns_attr_group))
2084 pr_warn("%s: failed to create sysfs group for identification\n",
2085 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002086 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2087 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2088 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002089 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002090 out_free_id:
2091 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002092 out_free_queue:
2093 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002094 out_release_instance:
2095 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002096 out_free_ns:
2097 kfree(ns);
2098}
2099
2100static void nvme_ns_remove(struct nvme_ns *ns)
2101{
Keith Busch646017a2016-02-24 09:15:54 -07002102 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2103 return;
2104
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002105 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002106 if (blk_get_integrity(ns->disk))
2107 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002108 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2109 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002110 if (ns->ndev)
2111 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002112 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002113 blk_cleanup_queue(ns->queue);
2114 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002115
2116 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002117 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002118 mutex_unlock(&ns->ctrl->namespaces_mutex);
2119
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002120 nvme_put_ns(ns);
2121}
2122
Keith Busch540c8012015-10-22 15:45:06 -06002123static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2124{
2125 struct nvme_ns *ns;
2126
Keith Busch32f0c4a2016-07-13 11:45:02 -06002127 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002128 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002129 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002130 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002131 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002132 } else
2133 nvme_alloc_ns(ctrl, nsid);
2134}
2135
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302136static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2137 unsigned nsid)
2138{
2139 struct nvme_ns *ns, *next;
2140
2141 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2142 if (ns->ns_id > nsid)
2143 nvme_ns_remove(ns);
2144 }
2145}
2146
Keith Busch540c8012015-10-22 15:45:06 -06002147static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2148{
2149 struct nvme_ns *ns;
2150 __le32 *ns_list;
2151 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2152 int ret = 0;
2153
2154 ns_list = kzalloc(0x1000, GFP_KERNEL);
2155 if (!ns_list)
2156 return -ENOMEM;
2157
2158 for (i = 0; i < num_lists; i++) {
2159 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2160 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302161 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002162
2163 for (j = 0; j < min(nn, 1024U); j++) {
2164 nsid = le32_to_cpu(ns_list[j]);
2165 if (!nsid)
2166 goto out;
2167
2168 nvme_validate_ns(ctrl, nsid);
2169
2170 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002171 ns = nvme_find_get_ns(ctrl, prev);
2172 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002173 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002174 nvme_put_ns(ns);
2175 }
Keith Busch540c8012015-10-22 15:45:06 -06002176 }
2177 }
2178 nn -= j;
2179 }
2180 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302181 nvme_remove_invalid_namespaces(ctrl, prev);
2182 free:
Keith Busch540c8012015-10-22 15:45:06 -06002183 kfree(ns_list);
2184 return ret;
2185}
2186
Christoph Hellwig5955be22016-04-26 13:51:59 +02002187static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002188{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002189 unsigned i;
2190
Keith Busch540c8012015-10-22 15:45:06 -06002191 for (i = 1; i <= nn; i++)
2192 nvme_validate_ns(ctrl, i);
2193
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302194 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002195}
2196
Christoph Hellwig5955be22016-04-26 13:51:59 +02002197static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002198{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002199 struct nvme_ctrl *ctrl =
2200 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002201 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002202 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002203
Christoph Hellwig5955be22016-04-26 13:51:59 +02002204 if (ctrl->state != NVME_CTRL_LIVE)
2205 return;
2206
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002207 if (nvme_identify_ctrl(ctrl, &id))
2208 return;
Keith Busch540c8012015-10-22 15:45:06 -06002209
2210 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002211 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002212 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2213 if (!nvme_scan_ns_list(ctrl, nn))
2214 goto done;
2215 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002216 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002217 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002218 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002219 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002220 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002221 kfree(id);
2222}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002223
2224void nvme_queue_scan(struct nvme_ctrl *ctrl)
2225{
2226 /*
2227 * Do not queue new scan work when a controller is reset during
2228 * removal.
2229 */
2230 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002231 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002232}
2233EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002234
Keith Busch32f0c4a2016-07-13 11:45:02 -06002235/*
2236 * This function iterates the namespace list unlocked to allow recovery from
2237 * controller failure. It is up to the caller to ensure the namespace list is
2238 * not modified by scan work while this function is executing.
2239 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002240void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2241{
2242 struct nvme_ns *ns, *next;
2243
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002244 /*
2245 * The dead states indicates the controller was not gracefully
2246 * disconnected. In that case, we won't be able to flush any data while
2247 * removing the namespaces' disks; fail all the queues now to avoid
2248 * potentially having to clean up the failed sync later.
2249 */
2250 if (ctrl->state == NVME_CTRL_DEAD)
2251 nvme_kill_queues(ctrl);
2252
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002253 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2254 nvme_ns_remove(ns);
2255}
Ming Lin576d55d2016-02-10 10:03:32 -08002256EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002257
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002258static void nvme_async_event_work(struct work_struct *work)
2259{
2260 struct nvme_ctrl *ctrl =
2261 container_of(work, struct nvme_ctrl, async_event_work);
2262
2263 spin_lock_irq(&ctrl->lock);
2264 while (ctrl->event_limit > 0) {
2265 int aer_idx = --ctrl->event_limit;
2266
2267 spin_unlock_irq(&ctrl->lock);
2268 ctrl->ops->submit_async_event(ctrl, aer_idx);
2269 spin_lock_irq(&ctrl->lock);
2270 }
2271 spin_unlock_irq(&ctrl->lock);
2272}
2273
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002274void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2275 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002276{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002277 u32 result = le32_to_cpu(res->u32);
2278 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002279
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002280 switch (le16_to_cpu(status) >> 1) {
2281 case NVME_SC_SUCCESS:
2282 done = false;
2283 /*FALLTHRU*/
2284 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002285 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002286 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002287 break;
2288 default:
2289 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002290 }
2291
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002292 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002293 return;
2294
2295 switch (result & 0xff07) {
2296 case NVME_AER_NOTICE_NS_CHANGED:
2297 dev_info(ctrl->device, "rescanning\n");
2298 nvme_queue_scan(ctrl);
2299 break;
2300 default:
2301 dev_warn(ctrl->device, "async event result %08x\n", result);
2302 }
2303}
2304EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2305
2306void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2307{
2308 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002309 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002310}
2311EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2312
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002313static DEFINE_IDA(nvme_instance_ida);
2314
2315static int nvme_set_instance(struct nvme_ctrl *ctrl)
2316{
2317 int instance, error;
2318
2319 do {
2320 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2321 return -ENODEV;
2322
2323 spin_lock(&dev_list_lock);
2324 error = ida_get_new(&nvme_instance_ida, &instance);
2325 spin_unlock(&dev_list_lock);
2326 } while (error == -EAGAIN);
2327
2328 if (error)
2329 return -ENODEV;
2330
2331 ctrl->instance = instance;
2332 return 0;
2333}
2334
2335static void nvme_release_instance(struct nvme_ctrl *ctrl)
2336{
2337 spin_lock(&dev_list_lock);
2338 ida_remove(&nvme_instance_ida, ctrl->instance);
2339 spin_unlock(&dev_list_lock);
2340}
2341
Keith Busch53029b02015-11-28 15:41:02 +01002342void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002343{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002344 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002345 flush_work(&ctrl->scan_work);
2346 nvme_remove_namespaces(ctrl);
2347
Keith Busch53029b02015-11-28 15:41:02 +01002348 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002349
2350 spin_lock(&dev_list_lock);
2351 list_del(&ctrl->node);
2352 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002353}
Ming Lin576d55d2016-02-10 10:03:32 -08002354EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002355
2356static void nvme_free_ctrl(struct kref *kref)
2357{
2358 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002359
2360 put_device(ctrl->device);
2361 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002362 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002363
2364 ctrl->ops->free_ctrl(ctrl);
2365}
2366
2367void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2368{
2369 kref_put(&ctrl->kref, nvme_free_ctrl);
2370}
Ming Lin576d55d2016-02-10 10:03:32 -08002371EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002372
2373/*
2374 * Initialize a NVMe controller structures. This needs to be called during
2375 * earliest initialization so that we have the initialized structured around
2376 * during probing.
2377 */
2378int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2379 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2380{
2381 int ret;
2382
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002383 ctrl->state = NVME_CTRL_NEW;
2384 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002385 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002386 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002387 kref_init(&ctrl->kref);
2388 ctrl->dev = dev;
2389 ctrl->ops = ops;
2390 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002391 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002392 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002393
2394 ret = nvme_set_instance(ctrl);
2395 if (ret)
2396 goto out;
2397
Keith Busch779ff7562016-01-12 15:09:31 -07002398 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002399 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002400 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002401 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002402 if (IS_ERR(ctrl->device)) {
2403 ret = PTR_ERR(ctrl->device);
2404 goto out_release_instance;
2405 }
2406 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002407 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002408
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002409 spin_lock(&dev_list_lock);
2410 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2411 spin_unlock(&dev_list_lock);
2412
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002413 /*
2414 * Initialize latency tolerance controls. The sysfs files won't
2415 * be visible to userspace unless the device actually supports APST.
2416 */
2417 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2418 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2419 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2420
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002421 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002422out_release_instance:
2423 nvme_release_instance(ctrl);
2424out:
2425 return ret;
2426}
Ming Lin576d55d2016-02-10 10:03:32 -08002427EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002428
Keith Busch69d9a992016-02-24 09:15:56 -07002429/**
2430 * nvme_kill_queues(): Ends all namespace queues
2431 * @ctrl: the dead controller that needs to end
2432 *
2433 * Call this function when the driver determines it is unable to get the
2434 * controller in a state capable of servicing IO.
2435 */
2436void nvme_kill_queues(struct nvme_ctrl *ctrl)
2437{
2438 struct nvme_ns *ns;
2439
Keith Busch32f0c4a2016-07-13 11:45:02 -06002440 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002441
2442 /* Forcibly start all queues to avoid having stuck requests */
2443 blk_mq_start_hw_queues(ctrl->admin_q);
2444
Keith Busch32f0c4a2016-07-13 11:45:02 -06002445 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002446 /*
2447 * Revalidating a dead namespace sets capacity to 0. This will
2448 * end buffered writers dirtying pages that can't be synced.
2449 */
Keith Buschf33447b2017-02-10 18:15:51 -05002450 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2451 continue;
2452 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002453 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002454
2455 /*
2456 * Forcibly start all queues to avoid having stuck requests.
2457 * Note that we must ensure the queues are not stopped
2458 * when the final removal happens.
2459 */
2460 blk_mq_start_hw_queues(ns->queue);
Ming Lei986f75c2017-05-22 23:05:04 +08002461
2462 /* draining requests in requeue list */
2463 blk_mq_kick_requeue_list(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002464 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002465 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002466}
Linus Torvalds237045f2016-03-18 17:13:31 -07002467EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002468
Keith Busch302ad8c2017-03-01 14:22:12 -05002469void nvme_unfreeze(struct nvme_ctrl *ctrl)
2470{
2471 struct nvme_ns *ns;
2472
2473 mutex_lock(&ctrl->namespaces_mutex);
2474 list_for_each_entry(ns, &ctrl->namespaces, list)
2475 blk_mq_unfreeze_queue(ns->queue);
2476 mutex_unlock(&ctrl->namespaces_mutex);
2477}
2478EXPORT_SYMBOL_GPL(nvme_unfreeze);
2479
2480void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2481{
2482 struct nvme_ns *ns;
2483
2484 mutex_lock(&ctrl->namespaces_mutex);
2485 list_for_each_entry(ns, &ctrl->namespaces, list) {
2486 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2487 if (timeout <= 0)
2488 break;
2489 }
2490 mutex_unlock(&ctrl->namespaces_mutex);
2491}
2492EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2493
2494void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2495{
2496 struct nvme_ns *ns;
2497
2498 mutex_lock(&ctrl->namespaces_mutex);
2499 list_for_each_entry(ns, &ctrl->namespaces, list)
2500 blk_mq_freeze_queue_wait(ns->queue);
2501 mutex_unlock(&ctrl->namespaces_mutex);
2502}
2503EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2504
2505void nvme_start_freeze(struct nvme_ctrl *ctrl)
2506{
2507 struct nvme_ns *ns;
2508
2509 mutex_lock(&ctrl->namespaces_mutex);
2510 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002511 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002512 mutex_unlock(&ctrl->namespaces_mutex);
2513}
2514EXPORT_SYMBOL_GPL(nvme_start_freeze);
2515
Keith Busch25646262016-01-04 09:10:57 -07002516void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002517{
2518 struct nvme_ns *ns;
2519
Keith Busch32f0c4a2016-07-13 11:45:02 -06002520 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002521 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002522 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002523 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002524}
Ming Lin576d55d2016-02-10 10:03:32 -08002525EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002526
Keith Busch25646262016-01-04 09:10:57 -07002527void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002528{
2529 struct nvme_ns *ns;
2530
Keith Busch32f0c4a2016-07-13 11:45:02 -06002531 mutex_lock(&ctrl->namespaces_mutex);
2532 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002533 blk_mq_start_stopped_hw_queues(ns->queue, true);
2534 blk_mq_kick_requeue_list(ns->queue);
2535 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002536 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002537}
Ming Lin576d55d2016-02-10 10:03:32 -08002538EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002539
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002540int __init nvme_core_init(void)
2541{
2542 int result;
2543
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002544 nvme_wq = alloc_workqueue("nvme-wq",
2545 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2546 if (!nvme_wq)
2547 return -ENOMEM;
2548
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002549 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2550 &nvme_dev_fops);
2551 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002552 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002553 else if (result > 0)
2554 nvme_char_major = result;
2555
2556 nvme_class = class_create(THIS_MODULE, "nvme");
2557 if (IS_ERR(nvme_class)) {
2558 result = PTR_ERR(nvme_class);
2559 goto unregister_chrdev;
2560 }
2561
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002562 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002563
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002564unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002565 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002566destroy_wq:
2567 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002568 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002569}
2570
2571void nvme_core_exit(void)
2572{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002573 class_destroy(nvme_class);
2574 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002575 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002576}
Ming Lin576d55d2016-02-10 10:03:32 -08002577
2578MODULE_LICENSE("GPL");
2579MODULE_VERSION("1.0");
2580module_init(nvme_core_init);
2581module_exit(nvme_core_exit);