blob: 0437f44d00f9d922fee9a53964c7a1b5514e7b05 [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
Keith Buschf80ec962016-07-12 16:20:31 -070052unsigned int nvme_max_retries = 5;
53module_param_named(max_retries, nvme_max_retries, uint, 0644);
54MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
55EXPORT_SYMBOL_GPL(nvme_max_retries);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010056
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010057static int nvme_char_major;
58module_param(nvme_char_major, int, 0);
59
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080060static unsigned long default_ps_max_latency_us = 25000;
61module_param(default_ps_max_latency_us, ulong, 0644);
62MODULE_PARM_DESC(default_ps_max_latency_us,
63 "max power saving latency for new devices; use PM QOS to change per device");
64
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010065static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080066static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010067
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010068static struct class *nvme_class;
69
Christoph Hellwig77f02a72017-03-30 13:41:32 +020070static inline bool nvme_req_needs_retry(struct request *req, u16 status)
71{
72 return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
73 (jiffies - req->start_time) < req->timeout &&
74 req->retries < nvme_max_retries;
75}
76
77void nvme_complete_rq(struct request *req)
78{
79 int error = 0;
80
81 if (unlikely(req->errors)) {
82 if (nvme_req_needs_retry(req, req->errors)) {
83 req->retries++;
84 blk_mq_requeue_request(req,
85 !blk_mq_queue_stopped(req->q));
86 return;
87 }
88
89 if (blk_rq_is_passthrough(req))
90 error = req->errors;
91 else
92 error = nvme_error_status(req->errors);
93 }
94
95 blk_mq_end_request(req, error);
96}
97EXPORT_SYMBOL_GPL(nvme_complete_rq);
98
Ming Linc55a2fd2016-05-18 14:05:02 -070099void nvme_cancel_request(struct request *req, void *data, bool reserved)
100{
101 int status;
102
103 if (!blk_mq_request_started(req))
104 return;
105
106 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
107 "Cancelling I/O %d", req->tag);
108
109 status = NVME_SC_ABORT_REQ;
110 if (blk_queue_dying(req->q))
111 status |= NVME_SC_DNR;
112 blk_mq_complete_request(req, status);
113}
114EXPORT_SYMBOL_GPL(nvme_cancel_request);
115
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200116bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
117 enum nvme_ctrl_state new_state)
118{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300119 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200120 bool changed = false;
121
122 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300123
124 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200125 switch (new_state) {
126 case NVME_CTRL_LIVE:
127 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200128 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200129 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900130 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200131 changed = true;
132 /* FALLTHRU */
133 default:
134 break;
135 }
136 break;
137 case NVME_CTRL_RESETTING:
138 switch (old_state) {
139 case NVME_CTRL_NEW:
140 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900141 case NVME_CTRL_RECONNECTING:
142 changed = true;
143 /* FALLTHRU */
144 default:
145 break;
146 }
147 break;
148 case NVME_CTRL_RECONNECTING:
149 switch (old_state) {
150 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200151 changed = true;
152 /* FALLTHRU */
153 default:
154 break;
155 }
156 break;
157 case NVME_CTRL_DELETING:
158 switch (old_state) {
159 case NVME_CTRL_LIVE:
160 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900161 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200162 changed = true;
163 /* FALLTHRU */
164 default:
165 break;
166 }
167 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600168 case NVME_CTRL_DEAD:
169 switch (old_state) {
170 case NVME_CTRL_DELETING:
171 changed = true;
172 /* FALLTHRU */
173 default:
174 break;
175 }
176 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200177 default:
178 break;
179 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200180
181 if (changed)
182 ctrl->state = new_state;
183
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300184 spin_unlock_irq(&ctrl->lock);
185
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200186 return changed;
187}
188EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
189
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100190static void nvme_free_ns(struct kref *kref)
191{
192 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
193
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200194 if (ns->ndev)
195 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100196
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200197 if (ns->disk) {
198 spin_lock(&dev_list_lock);
199 ns->disk->private_data = NULL;
200 spin_unlock(&dev_list_lock);
201 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100202
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100203 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700204 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
205 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100206 kfree(ns);
207}
208
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100209static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100210{
211 kref_put(&ns->kref, nvme_free_ns);
212}
213
214static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
215{
216 struct nvme_ns *ns;
217
218 spin_lock(&dev_list_lock);
219 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800220 if (ns) {
221 if (!kref_get_unless_zero(&ns->kref))
222 goto fail;
223 if (!try_module_get(ns->ctrl->ops->module))
224 goto fail_put_ns;
225 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100226 spin_unlock(&dev_list_lock);
227
228 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800229
230fail_put_ns:
231 kref_put(&ns->kref, nvme_free_ns);
232fail:
233 spin_unlock(&dev_list_lock);
234 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100235}
236
Christoph Hellwig41609822015-11-20 09:00:02 +0100237struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200238 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100239{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100240 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100241 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100242
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200243 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100244 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200245 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100246 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200247 qid ? qid - 1 : 0);
248 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100249 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100250 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100251
Christoph Hellwig21d34712015-11-26 09:08:36 +0100252 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800253 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100254
Christoph Hellwig41609822015-11-20 09:00:02 +0100255 return req;
256}
Ming Lin576d55d2016-02-10 10:03:32 -0800257EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100258
Ming Lin8093f7c2016-04-12 13:10:14 -0600259static inline void nvme_setup_flush(struct nvme_ns *ns,
260 struct nvme_command *cmnd)
261{
262 memset(cmnd, 0, sizeof(*cmnd));
263 cmnd->common.opcode = nvme_cmd_flush;
264 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
265}
266
267static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
268 struct nvme_command *cmnd)
269{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100270 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600271 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100272 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600273
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100274 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600275 if (!range)
276 return BLK_MQ_RQ_QUEUE_BUSY;
277
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100278 __rq_for_each_bio(bio, req) {
279 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
280 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
281
282 range[n].cattr = cpu_to_le32(0);
283 range[n].nlb = cpu_to_le32(nlb);
284 range[n].slba = cpu_to_le64(slba);
285 n++;
286 }
287
288 if (WARN_ON_ONCE(n != segments)) {
289 kfree(range);
290 return BLK_MQ_RQ_QUEUE_ERROR;
291 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600292
293 memset(cmnd, 0, sizeof(*cmnd));
294 cmnd->dsm.opcode = nvme_cmd_dsm;
295 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100296 cmnd->dsm.nr = segments - 1;
Ming Lin8093f7c2016-04-12 13:10:14 -0600297 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
298
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700299 req->special_vec.bv_page = virt_to_page(range);
300 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100301 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700302 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600303
Omar Sandovalbac00002016-11-15 11:11:58 -0800304 return BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600305}
Ming Lin8093f7c2016-04-12 13:10:14 -0600306
Ming Lin8093f7c2016-04-12 13:10:14 -0600307static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
308 struct nvme_command *cmnd)
309{
310 u16 control = 0;
311 u32 dsmgmt = 0;
312
313 if (req->cmd_flags & REQ_FUA)
314 control |= NVME_RW_FUA;
315 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
316 control |= NVME_RW_LR;
317
318 if (req->cmd_flags & REQ_RAHEAD)
319 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
320
321 memset(cmnd, 0, sizeof(*cmnd));
322 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600323 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
324 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
325 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
326
327 if (ns->ms) {
328 switch (ns->pi_type) {
329 case NVME_NS_DPS_PI_TYPE3:
330 control |= NVME_RW_PRINFO_PRCHK_GUARD;
331 break;
332 case NVME_NS_DPS_PI_TYPE1:
333 case NVME_NS_DPS_PI_TYPE2:
334 control |= NVME_RW_PRINFO_PRCHK_GUARD |
335 NVME_RW_PRINFO_PRCHK_REF;
336 cmnd->rw.reftag = cpu_to_le32(
337 nvme_block_nr(ns, blk_rq_pos(req)));
338 break;
339 }
340 if (!blk_integrity_rq(req))
341 control |= NVME_RW_PRINFO_PRACT;
342 }
343
344 cmnd->rw.control = cpu_to_le16(control);
345 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
346}
347
348int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
349 struct nvme_command *cmd)
350{
Omar Sandovalbac00002016-11-15 11:11:58 -0800351 int ret = BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600352
Christoph Hellwig987f6992017-04-05 19:18:08 +0200353 if (!(req->rq_flags & RQF_DONTPREP)) {
354 req->retries = 0;
355 req->rq_flags |= RQF_DONTPREP;
356 }
357
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100358 switch (req_op(req)) {
359 case REQ_OP_DRV_IN:
360 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800361 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100362 break;
363 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600364 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100365 break;
366 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600367 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100368 break;
369 case REQ_OP_READ:
370 case REQ_OP_WRITE:
Ming Lin8093f7c2016-04-12 13:10:14 -0600371 nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100372 break;
373 default:
374 WARN_ON_ONCE(1);
375 return BLK_MQ_RQ_QUEUE_ERROR;
376 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600377
James Smart721b3912016-10-21 23:33:34 +0300378 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600379 return ret;
380}
381EXPORT_SYMBOL_GPL(nvme_setup_cmd);
382
Christoph Hellwig41609822015-11-20 09:00:02 +0100383/*
384 * Returns 0 on success. If the result is negative, it's a Linux error code;
385 * if the result is positive, it's an NVM Express status code
386 */
387int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800388 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200389 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100390{
391 struct request *req;
392 int ret;
393
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200394 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100395 if (IS_ERR(req))
396 return PTR_ERR(req);
397
398 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
399
Christoph Hellwig21d34712015-11-26 09:08:36 +0100400 if (buffer && bufflen) {
401 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
402 if (ret)
403 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100404 }
405
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200406 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800407 if (result)
408 *result = nvme_req(req)->result;
Christoph Hellwig41609822015-11-20 09:00:02 +0100409 ret = req->errors;
410 out:
411 blk_mq_free_request(req);
412 return ret;
413}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200414EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100415
416int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
417 void *buffer, unsigned bufflen)
418{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200419 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
420 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100421}
Ming Lin576d55d2016-02-10 10:03:32 -0800422EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100423
Keith Busch0b7f1f22015-10-23 09:47:28 -0600424int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
425 void __user *ubuffer, unsigned bufflen,
426 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
427 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100428{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200429 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600430 struct nvme_ns *ns = q->queuedata;
431 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100432 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600433 struct bio *bio = NULL;
434 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100435 int ret;
436
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200437 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100438 if (IS_ERR(req))
439 return PTR_ERR(req);
440
441 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
442
443 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100444 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
445 GFP_KERNEL);
446 if (ret)
447 goto out;
448 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100449
Keith Busch0b7f1f22015-10-23 09:47:28 -0600450 if (!disk)
451 goto submit;
452 bio->bi_bdev = bdget_disk(disk, 0);
453 if (!bio->bi_bdev) {
454 ret = -ENODEV;
455 goto out_unmap;
456 }
457
Keith Busche9fc63d2016-02-24 09:15:58 -0700458 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600459 struct bio_integrity_payload *bip;
460
461 meta = kmalloc(meta_len, GFP_KERNEL);
462 if (!meta) {
463 ret = -ENOMEM;
464 goto out_unmap;
465 }
466
467 if (write) {
468 if (copy_from_user(meta, meta_buffer,
469 meta_len)) {
470 ret = -EFAULT;
471 goto out_free_meta;
472 }
473 }
474
475 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700476 if (IS_ERR(bip)) {
477 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600478 goto out_free_meta;
479 }
480
481 bip->bip_iter.bi_size = meta_len;
482 bip->bip_iter.bi_sector = meta_seed;
483
484 ret = bio_integrity_add_page(bio, virt_to_page(meta),
485 meta_len, offset_in_page(meta));
486 if (ret != meta_len) {
487 ret = -ENOMEM;
488 goto out_free_meta;
489 }
490 }
491 }
492 submit:
493 blk_execute_rq(req->q, disk, req, 0);
494 ret = req->errors;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100495 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800496 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600497 if (meta && !ret && !write) {
498 if (copy_to_user(meta_buffer, meta, meta_len))
499 ret = -EFAULT;
500 }
501 out_free_meta:
502 kfree(meta);
503 out_unmap:
504 if (bio) {
505 if (disk && bio->bi_bdev)
506 bdput(bio->bi_bdev);
507 blk_rq_unmap_user(bio);
508 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100509 out:
510 blk_mq_free_request(req);
511 return ret;
512}
513
Keith Busch0b7f1f22015-10-23 09:47:28 -0600514int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
515 void __user *ubuffer, unsigned bufflen, u32 *result,
516 unsigned timeout)
517{
518 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
519 result, timeout);
520}
521
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200522static void nvme_keep_alive_end_io(struct request *rq, int error)
523{
524 struct nvme_ctrl *ctrl = rq->end_io_data;
525
526 blk_mq_free_request(rq);
527
528 if (error) {
529 dev_err(ctrl->device,
530 "failed nvme_keep_alive_end_io error=%d\n", error);
531 return;
532 }
533
534 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
535}
536
537static int nvme_keep_alive(struct nvme_ctrl *ctrl)
538{
539 struct nvme_command c;
540 struct request *rq;
541
542 memset(&c, 0, sizeof(c));
543 c.common.opcode = nvme_admin_keep_alive;
544
545 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
546 NVME_QID_ANY);
547 if (IS_ERR(rq))
548 return PTR_ERR(rq);
549
550 rq->timeout = ctrl->kato * HZ;
551 rq->end_io_data = ctrl;
552
553 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
554
555 return 0;
556}
557
558static void nvme_keep_alive_work(struct work_struct *work)
559{
560 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
561 struct nvme_ctrl, ka_work);
562
563 if (nvme_keep_alive(ctrl)) {
564 /* allocation failure, reset the controller */
565 dev_err(ctrl->device, "keep-alive failed\n");
566 ctrl->ops->reset_ctrl(ctrl);
567 return;
568 }
569}
570
571void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
572{
573 if (unlikely(ctrl->kato == 0))
574 return;
575
576 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
577 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
578}
579EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
580
581void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
582{
583 if (unlikely(ctrl->kato == 0))
584 return;
585
586 cancel_delayed_work_sync(&ctrl->ka_work);
587}
588EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
589
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100590int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100591{
592 struct nvme_command c = { };
593 int error;
594
595 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
596 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200597 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100598
599 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
600 if (!*id)
601 return -ENOMEM;
602
603 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
604 sizeof(struct nvme_id_ctrl));
605 if (error)
606 kfree(*id);
607 return error;
608}
609
Keith Busch540c8012015-10-22 15:45:06 -0600610static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
611{
612 struct nvme_command c = { };
613
614 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200615 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600616 c.identify.nsid = cpu_to_le32(nsid);
617 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
618}
619
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100620int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100621 struct nvme_id_ns **id)
622{
623 struct nvme_command c = { };
624 int error;
625
626 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200627 c.identify.opcode = nvme_admin_identify;
628 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200629 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100630
631 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
632 if (!*id)
633 return -ENOMEM;
634
635 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
636 sizeof(struct nvme_id_ns));
637 if (error)
638 kfree(*id);
639 return error;
640}
641
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100642int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700643 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100644{
645 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800646 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100647 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100648
649 memset(&c, 0, sizeof(c));
650 c.features.opcode = nvme_admin_get_features;
651 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100652 c.features.fid = cpu_to_le32(fid);
653
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800654 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200655 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700656 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800657 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100658 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100659}
660
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100661int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700662 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100663{
664 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800665 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100666 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100667
668 memset(&c, 0, sizeof(c));
669 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100670 c.features.fid = cpu_to_le32(fid);
671 c.features.dword11 = cpu_to_le32(dword11);
672
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800673 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700674 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700675 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800676 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100677 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100678}
679
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100680int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100681{
682 struct nvme_command c = { };
683 int error;
684
685 c.common.opcode = nvme_admin_get_log_page,
686 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
687 c.common.cdw10[0] = cpu_to_le32(
688 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
689 NVME_LOG_SMART),
690
691 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
692 if (!*log)
693 return -ENOMEM;
694
695 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
696 sizeof(struct nvme_smart_log));
697 if (error)
698 kfree(*log);
699 return error;
700}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100701
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100702int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
703{
704 u32 q_count = (*count - 1) | ((*count - 1) << 16);
705 u32 result;
706 int status, nr_io_queues;
707
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700708 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100709 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200710 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100711 return status;
712
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200713 /*
714 * Degraded controllers might return an error when setting the queue
715 * count. We still want to be able to bring them online and offer
716 * access to the admin queue, as that might be only way to fix them up.
717 */
718 if (status > 0) {
719 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
720 *count = 0;
721 } else {
722 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
723 *count = min(*count, nr_io_queues);
724 }
725
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100726 return 0;
727}
Ming Lin576d55d2016-02-10 10:03:32 -0800728EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100729
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100730static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
731{
732 struct nvme_user_io io;
733 struct nvme_command c;
734 unsigned length, meta_len;
735 void __user *metadata;
736
737 if (copy_from_user(&io, uio, sizeof(io)))
738 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700739 if (io.flags)
740 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100741
742 switch (io.opcode) {
743 case nvme_cmd_write:
744 case nvme_cmd_read:
745 case nvme_cmd_compare:
746 break;
747 default:
748 return -EINVAL;
749 }
750
751 length = (io.nblocks + 1) << ns->lba_shift;
752 meta_len = (io.nblocks + 1) * ns->ms;
753 metadata = (void __user *)(uintptr_t)io.metadata;
754
755 if (ns->ext) {
756 length += meta_len;
757 meta_len = 0;
758 } else if (meta_len) {
759 if ((io.metadata & 3) || !io.metadata)
760 return -EINVAL;
761 }
762
763 memset(&c, 0, sizeof(c));
764 c.rw.opcode = io.opcode;
765 c.rw.flags = io.flags;
766 c.rw.nsid = cpu_to_le32(ns->ns_id);
767 c.rw.slba = cpu_to_le64(io.slba);
768 c.rw.length = cpu_to_le16(io.nblocks);
769 c.rw.control = cpu_to_le16(io.control);
770 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
771 c.rw.reftag = cpu_to_le32(io.reftag);
772 c.rw.apptag = cpu_to_le16(io.apptag);
773 c.rw.appmask = cpu_to_le16(io.appmask);
774
775 return __nvme_submit_user_cmd(ns->queue, &c,
776 (void __user *)(uintptr_t)io.addr, length,
777 metadata, meta_len, io.slba, NULL, 0);
778}
779
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100780static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100781 struct nvme_passthru_cmd __user *ucmd)
782{
783 struct nvme_passthru_cmd cmd;
784 struct nvme_command c;
785 unsigned timeout = 0;
786 int status;
787
788 if (!capable(CAP_SYS_ADMIN))
789 return -EACCES;
790 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
791 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700792 if (cmd.flags)
793 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100794
795 memset(&c, 0, sizeof(c));
796 c.common.opcode = cmd.opcode;
797 c.common.flags = cmd.flags;
798 c.common.nsid = cpu_to_le32(cmd.nsid);
799 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
800 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
801 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
802 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
803 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
804 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
805 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
806 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
807
808 if (cmd.timeout_ms)
809 timeout = msecs_to_jiffies(cmd.timeout_ms);
810
811 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100812 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100813 &cmd.result, timeout);
814 if (status >= 0) {
815 if (put_user(cmd.result, &ucmd->result))
816 return -EFAULT;
817 }
818
819 return status;
820}
821
822static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
823 unsigned int cmd, unsigned long arg)
824{
825 struct nvme_ns *ns = bdev->bd_disk->private_data;
826
827 switch (cmd) {
828 case NVME_IOCTL_ID:
829 force_successful_syscall_return();
830 return ns->ns_id;
831 case NVME_IOCTL_ADMIN_CMD:
832 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
833 case NVME_IOCTL_IO_CMD:
834 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
835 case NVME_IOCTL_SUBMIT_IO:
836 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100837#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100838 case SG_GET_VERSION_NUM:
839 return nvme_sg_get_version_num((void __user *)arg);
840 case SG_IO:
841 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100842#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100843 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100844#ifdef CONFIG_NVM
845 if (ns->ndev)
846 return nvme_nvm_ioctl(ns, cmd, arg);
847#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700848 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100849 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700850 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100851 return -ENOTTY;
852 }
853}
854
855#ifdef CONFIG_COMPAT
856static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
857 unsigned int cmd, unsigned long arg)
858{
859 switch (cmd) {
860 case SG_IO:
861 return -ENOIOCTLCMD;
862 }
863 return nvme_ioctl(bdev, mode, cmd, arg);
864}
865#else
866#define nvme_compat_ioctl NULL
867#endif
868
869static int nvme_open(struct block_device *bdev, fmode_t mode)
870{
871 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
872}
873
874static void nvme_release(struct gendisk *disk, fmode_t mode)
875{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800876 struct nvme_ns *ns = disk->private_data;
877
878 module_put(ns->ctrl->ops->module);
879 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100880}
881
882static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
883{
884 /* some standard values */
885 geo->heads = 1 << 6;
886 geo->sectors = 1 << 5;
887 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
888 return 0;
889}
890
891#ifdef CONFIG_BLK_DEV_INTEGRITY
892static void nvme_init_integrity(struct nvme_ns *ns)
893{
894 struct blk_integrity integrity;
895
Jay Freyenseefa9a89f2016-07-20 21:26:16 -0600896 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100897 switch (ns->pi_type) {
898 case NVME_NS_DPS_PI_TYPE3:
899 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000900 integrity.tag_size = sizeof(u16) + sizeof(u32);
901 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100902 break;
903 case NVME_NS_DPS_PI_TYPE1:
904 case NVME_NS_DPS_PI_TYPE2:
905 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000906 integrity.tag_size = sizeof(u16);
907 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100908 break;
909 default:
910 integrity.profile = NULL;
911 break;
912 }
913 integrity.tuple_size = ns->ms;
914 blk_integrity_register(ns->disk, &integrity);
915 blk_queue_max_integrity_segments(ns->queue, 1);
916}
917#else
918static void nvme_init_integrity(struct nvme_ns *ns)
919{
920}
921#endif /* CONFIG_BLK_DEV_INTEGRITY */
922
923static void nvme_config_discard(struct nvme_ns *ns)
924{
Keith Busch08095e72016-03-04 13:15:17 -0700925 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100926 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700927
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100928 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
929 NVME_DSM_MAX_RANGES);
930
Keith Busch08095e72016-03-04 13:15:17 -0700931 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
932 ns->queue->limits.discard_zeroes_data = 1;
933 else
934 ns->queue->limits.discard_zeroes_data = 0;
935
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100936 ns->queue->limits.discard_alignment = logical_block_size;
937 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800938 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100939 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100940 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
941}
942
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200943static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100944{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200945 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200946 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100947 return -ENODEV;
948 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200949
950 if ((*id)->ncap == 0) {
951 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100952 return -ENODEV;
953 }
954
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600955 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200956 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600957 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200958 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
959
960 return 0;
961}
962
963static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
964{
965 struct nvme_ns *ns = disk->private_data;
966 u8 lbaf, pi_type;
967 u16 old_ms;
968 unsigned short bs;
Keith Busch2b9b6e82015-12-22 10:10:45 -0700969
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100970 old_ms = ns->ms;
971 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
972 ns->lba_shift = id->lbaf[lbaf].ds;
973 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
974 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
975
976 /*
977 * If identify namespace failed, use default 512 byte block size so
978 * block layer can use before failing read/write for 0 capacity.
979 */
980 if (ns->lba_shift == 0)
981 ns->lba_shift = 9;
982 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100983 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
984 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
985 id->dps & NVME_NS_DPS_PI_MASK : 0;
986
987 blk_mq_freeze_queue(disk->queue);
988 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
989 ns->ms != old_ms ||
990 bs != queue_logical_block_size(disk->queue) ||
991 (ns->ms && ns->ext)))
992 blk_integrity_unregister(disk);
993
994 ns->pi_type = pi_type;
995 blk_queue_logical_block_size(ns->queue, bs);
996
Keith Busch4b9d5b12015-11-20 09:13:30 +0100997 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100998 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100999 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1000 set_capacity(disk, 0);
1001 else
1002 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1003
1004 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1005 nvme_config_discard(ns);
1006 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001007}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001008
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001009static int nvme_revalidate_disk(struct gendisk *disk)
1010{
1011 struct nvme_ns *ns = disk->private_data;
1012 struct nvme_id_ns *id = NULL;
1013 int ret;
1014
1015 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1016 set_capacity(disk, 0);
1017 return -ENODEV;
1018 }
1019
1020 ret = nvme_revalidate_ns(ns, &id);
1021 if (ret)
1022 return ret;
1023
1024 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001025 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001026
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001027 return 0;
1028}
1029
1030static char nvme_pr_type(enum pr_type type)
1031{
1032 switch (type) {
1033 case PR_WRITE_EXCLUSIVE:
1034 return 1;
1035 case PR_EXCLUSIVE_ACCESS:
1036 return 2;
1037 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1038 return 3;
1039 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1040 return 4;
1041 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1042 return 5;
1043 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1044 return 6;
1045 default:
1046 return 0;
1047 }
1048};
1049
1050static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1051 u64 key, u64 sa_key, u8 op)
1052{
1053 struct nvme_ns *ns = bdev->bd_disk->private_data;
1054 struct nvme_command c;
1055 u8 data[16] = { 0, };
1056
1057 put_unaligned_le64(key, &data[0]);
1058 put_unaligned_le64(sa_key, &data[8]);
1059
1060 memset(&c, 0, sizeof(c));
1061 c.common.opcode = op;
1062 c.common.nsid = cpu_to_le32(ns->ns_id);
1063 c.common.cdw10[0] = cpu_to_le32(cdw10);
1064
1065 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1066}
1067
1068static int nvme_pr_register(struct block_device *bdev, u64 old,
1069 u64 new, unsigned flags)
1070{
1071 u32 cdw10;
1072
1073 if (flags & ~PR_FL_IGNORE_KEY)
1074 return -EOPNOTSUPP;
1075
1076 cdw10 = old ? 2 : 0;
1077 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1078 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1079 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1080}
1081
1082static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1083 enum pr_type type, unsigned flags)
1084{
1085 u32 cdw10;
1086
1087 if (flags & ~PR_FL_IGNORE_KEY)
1088 return -EOPNOTSUPP;
1089
1090 cdw10 = nvme_pr_type(type) << 8;
1091 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1092 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1093}
1094
1095static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1096 enum pr_type type, bool abort)
1097{
1098 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1099 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1100}
1101
1102static int nvme_pr_clear(struct block_device *bdev, u64 key)
1103{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001104 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001105 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1106}
1107
1108static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1109{
1110 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1111 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1112}
1113
1114static const struct pr_ops nvme_pr_ops = {
1115 .pr_register = nvme_pr_register,
1116 .pr_reserve = nvme_pr_reserve,
1117 .pr_release = nvme_pr_release,
1118 .pr_preempt = nvme_pr_preempt,
1119 .pr_clear = nvme_pr_clear,
1120};
1121
Scott Bauera98e58e52017-02-03 12:50:32 -07001122#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001123int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1124 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001125{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001126 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001127 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001128
1129 memset(&cmd, 0, sizeof(cmd));
1130 if (send)
1131 cmd.common.opcode = nvme_admin_security_send;
1132 else
1133 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001134 cmd.common.nsid = 0;
1135 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1136 cmd.common.cdw10[1] = cpu_to_le32(len);
1137
1138 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1139 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1140}
1141EXPORT_SYMBOL_GPL(nvme_sec_submit);
1142#endif /* CONFIG_BLK_SED_OPAL */
1143
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001144static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001145 .owner = THIS_MODULE,
1146 .ioctl = nvme_ioctl,
1147 .compat_ioctl = nvme_compat_ioctl,
1148 .open = nvme_open,
1149 .release = nvme_release,
1150 .getgeo = nvme_getgeo,
1151 .revalidate_disk= nvme_revalidate_disk,
1152 .pr_ops = &nvme_pr_ops,
1153};
1154
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001155static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1156{
1157 unsigned long timeout =
1158 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1159 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1160 int ret;
1161
1162 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001163 if (csts == ~0)
1164 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001165 if ((csts & NVME_CSTS_RDY) == bit)
1166 break;
1167
1168 msleep(100);
1169 if (fatal_signal_pending(current))
1170 return -EINTR;
1171 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001172 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001173 "Device not ready; aborting %s\n", enabled ?
1174 "initialisation" : "reset");
1175 return -ENODEV;
1176 }
1177 }
1178
1179 return ret;
1180}
1181
1182/*
1183 * If the device has been passed off to us in an enabled state, just clear
1184 * the enabled bit. The spec says we should set the 'shutdown notification
1185 * bits', but doing so may cause the device to complete commands to the
1186 * admin queue ... and we don't know what memory that might be pointing at!
1187 */
1188int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1189{
1190 int ret;
1191
1192 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1193 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1194
1195 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1196 if (ret)
1197 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001198
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001199 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001200 msleep(NVME_QUIRK_DELAY_AMOUNT);
1201
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001202 return nvme_wait_ready(ctrl, cap, false);
1203}
Ming Lin576d55d2016-02-10 10:03:32 -08001204EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001205
1206int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1207{
1208 /*
1209 * Default to a 4K page size, with the intention to update this
1210 * path in the future to accomodate architectures with differing
1211 * kernel and IO page sizes.
1212 */
1213 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1214 int ret;
1215
1216 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001217 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001218 "Minimum device page size %u too large for host (%u)\n",
1219 1 << dev_page_min, 1 << page_shift);
1220 return -ENODEV;
1221 }
1222
1223 ctrl->page_size = 1 << page_shift;
1224
1225 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1226 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1227 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1228 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1229 ctrl->ctrl_config |= NVME_CC_ENABLE;
1230
1231 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1232 if (ret)
1233 return ret;
1234 return nvme_wait_ready(ctrl, cap, true);
1235}
Ming Lin576d55d2016-02-10 10:03:32 -08001236EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001237
1238int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1239{
1240 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1241 u32 csts;
1242 int ret;
1243
1244 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1245 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1246
1247 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1248 if (ret)
1249 return ret;
1250
1251 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1252 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1253 break;
1254
1255 msleep(100);
1256 if (fatal_signal_pending(current))
1257 return -EINTR;
1258 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001259 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001260 "Device shutdown incomplete; abort shutdown\n");
1261 return -ENODEV;
1262 }
1263 }
1264
1265 return ret;
1266}
Ming Lin576d55d2016-02-10 10:03:32 -08001267EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001268
Christoph Hellwigda358252016-03-02 18:07:11 +01001269static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1270 struct request_queue *q)
1271{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001272 bool vwc = false;
1273
Christoph Hellwigda358252016-03-02 18:07:11 +01001274 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001275 u32 max_segments =
1276 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1277
Christoph Hellwigda358252016-03-02 18:07:11 +01001278 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001279 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001280 }
Keith Busche6282ae2016-12-19 11:37:50 -05001281 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1282 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001283 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001284 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1285 vwc = true;
1286 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001287}
1288
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001289static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1290{
1291 /*
1292 * APST (Autonomous Power State Transition) lets us program a
1293 * table of power state transitions that the controller will
1294 * perform automatically. We configure it with a simple
1295 * heuristic: we are willing to spend at most 2% of the time
1296 * transitioning between power states. Therefore, when running
1297 * in any given state, we will enter the next lower-power
1298 * non-operational state after waiting 100 * (enlat + exlat)
1299 * microseconds, as long as that state's total latency is under
1300 * the requested maximum latency.
1301 *
1302 * We will not autonomously enter any non-operational state for
1303 * which the total latency exceeds ps_max_latency_us. Users
1304 * can set ps_max_latency_us to zero to turn off APST.
1305 */
1306
1307 unsigned apste;
1308 struct nvme_feat_auto_pst *table;
1309 int ret;
1310
1311 /*
1312 * If APST isn't supported or if we haven't been initialized yet,
1313 * then don't do anything.
1314 */
1315 if (!ctrl->apsta)
1316 return;
1317
1318 if (ctrl->npss > 31) {
1319 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1320 return;
1321 }
1322
1323 table = kzalloc(sizeof(*table), GFP_KERNEL);
1324 if (!table)
1325 return;
1326
1327 if (ctrl->ps_max_latency_us == 0) {
1328 /* Turn off APST. */
1329 apste = 0;
1330 } else {
1331 __le64 target = cpu_to_le64(0);
1332 int state;
1333
1334 /*
1335 * Walk through all states from lowest- to highest-power.
1336 * According to the spec, lower-numbered states use more
1337 * power. NPSS, despite the name, is the index of the
1338 * lowest-power state, not the number of states.
1339 */
1340 for (state = (int)ctrl->npss; state >= 0; state--) {
1341 u64 total_latency_us, transition_ms;
1342
1343 if (target)
1344 table->entries[state] = target;
1345
1346 /*
1347 * Is this state a useful non-operational state for
1348 * higher-power states to autonomously transition to?
1349 */
1350 if (!(ctrl->psd[state].flags &
1351 NVME_PS_FLAGS_NON_OP_STATE))
1352 continue;
1353
1354 total_latency_us =
1355 (u64)le32_to_cpu(ctrl->psd[state].entry_lat) +
1356 + le32_to_cpu(ctrl->psd[state].exit_lat);
1357 if (total_latency_us > ctrl->ps_max_latency_us)
1358 continue;
1359
1360 /*
1361 * This state is good. Use it as the APST idle
1362 * target for higher power states.
1363 */
1364 transition_ms = total_latency_us + 19;
1365 do_div(transition_ms, 20);
1366 if (transition_ms > (1 << 24) - 1)
1367 transition_ms = (1 << 24) - 1;
1368
1369 target = cpu_to_le64((state << 3) |
1370 (transition_ms << 8));
1371 }
1372
1373 apste = 1;
1374 }
1375
1376 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1377 table, sizeof(*table), NULL);
1378 if (ret)
1379 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1380
1381 kfree(table);
1382}
1383
1384static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1385{
1386 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1387 u64 latency;
1388
1389 switch (val) {
1390 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1391 case PM_QOS_LATENCY_ANY:
1392 latency = U64_MAX;
1393 break;
1394
1395 default:
1396 latency = val;
1397 }
1398
1399 if (ctrl->ps_max_latency_us != latency) {
1400 ctrl->ps_max_latency_us = latency;
1401 nvme_configure_apst(ctrl);
1402 }
1403}
1404
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001405struct nvme_core_quirk_entry {
1406 /*
1407 * NVMe model and firmware strings are padded with spaces. For
1408 * simplicity, strings in the quirk table are padded with NULLs
1409 * instead.
1410 */
1411 u16 vid;
1412 const char *mn;
1413 const char *fr;
1414 unsigned long quirks;
1415};
1416
1417static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001418 /*
1419 * Seen on a Samsung "SM951 NVMe SAMSUNG 256GB": using APST causes
1420 * the controller to go out to lunch. It dies when the watchdog
1421 * timer reads CSTS and gets 0xffffffff.
1422 */
1423 {
1424 .vid = 0x144d,
1425 .fr = "BXW75D0Q",
1426 .quirks = NVME_QUIRK_NO_APST,
1427 },
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001428};
1429
1430/* match is null-terminated but idstr is space-padded. */
1431static bool string_matches(const char *idstr, const char *match, size_t len)
1432{
1433 size_t matchlen;
1434
1435 if (!match)
1436 return true;
1437
1438 matchlen = strlen(match);
1439 WARN_ON_ONCE(matchlen > len);
1440
1441 if (memcmp(idstr, match, matchlen))
1442 return false;
1443
1444 for (; matchlen < len; matchlen++)
1445 if (idstr[matchlen] != ' ')
1446 return false;
1447
1448 return true;
1449}
1450
1451static bool quirk_matches(const struct nvme_id_ctrl *id,
1452 const struct nvme_core_quirk_entry *q)
1453{
1454 return q->vid == le16_to_cpu(id->vid) &&
1455 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1456 string_matches(id->fr, q->fr, sizeof(id->fr));
1457}
1458
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001459/*
1460 * Initialize the cached copies of the Identify data and various controller
1461 * register in our nvme_ctrl structure. This should be called as soon as
1462 * the admin queue is fully up and running.
1463 */
1464int nvme_init_identify(struct nvme_ctrl *ctrl)
1465{
1466 struct nvme_id_ctrl *id;
1467 u64 cap;
1468 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001469 u32 max_hw_sectors;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001470 u8 prev_apsta;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001471
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001472 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1473 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001474 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001475 return ret;
1476 }
1477
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001478 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1479 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001480 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001481 return ret;
1482 }
1483 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1484
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001485 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001486 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1487
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001488 ret = nvme_identify_ctrl(ctrl, &id);
1489 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001490 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001491 return -EIO;
1492 }
1493
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001494 if (!ctrl->identified) {
1495 /*
1496 * Check for quirks. Quirk can depend on firmware version,
1497 * so, in principle, the set of quirks present can change
1498 * across a reset. As a possible future enhancement, we
1499 * could re-scan for quirks every time we reinitialize
1500 * the device, but we'd have to make sure that the driver
1501 * behaves intelligently if the quirks change.
1502 */
1503
1504 int i;
1505
1506 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1507 if (quirk_matches(id, &core_quirks[i]))
1508 ctrl->quirks |= core_quirks[i].quirks;
1509 }
1510 }
1511
Scott Bauer8a9ae522017-02-17 13:59:40 +01001512 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001513 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001514 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001515 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001516 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001517 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001518 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1519 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1520 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1521 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001522 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001523 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001524 max_hw_sectors = UINT_MAX;
1525 ctrl->max_hw_sectors =
1526 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001527
Christoph Hellwigda358252016-03-02 18:07:11 +01001528 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001529 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001530 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001531
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001532 ctrl->npss = id->npss;
1533 prev_apsta = ctrl->apsta;
1534 ctrl->apsta = (ctrl->quirks & NVME_QUIRK_NO_APST) ? 0 : id->apsta;
1535 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1536
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001537 if (ctrl->ops->is_fabrics) {
1538 ctrl->icdoff = le16_to_cpu(id->icdoff);
1539 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1540 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1541 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1542
1543 /*
1544 * In fabrics we need to verify the cntlid matches the
1545 * admin connect
1546 */
1547 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1548 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001549
1550 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1551 dev_err(ctrl->dev,
1552 "keep-alive support is mandatory for fabrics\n");
1553 ret = -EINVAL;
1554 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001555 } else {
1556 ctrl->cntlid = le16_to_cpu(id->cntlid);
1557 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001558
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001559 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001560
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001561 if (ctrl->apsta && !prev_apsta)
1562 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1563 else if (!ctrl->apsta && prev_apsta)
1564 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1565
1566 nvme_configure_apst(ctrl);
1567
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001568 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001569
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001570 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001571}
Ming Lin576d55d2016-02-10 10:03:32 -08001572EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001573
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001574static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001575{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001576 struct nvme_ctrl *ctrl;
1577 int instance = iminor(inode);
1578 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001579
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001580 spin_lock(&dev_list_lock);
1581 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1582 if (ctrl->instance != instance)
1583 continue;
1584
1585 if (!ctrl->admin_q) {
1586 ret = -EWOULDBLOCK;
1587 break;
1588 }
1589 if (!kref_get_unless_zero(&ctrl->kref))
1590 break;
1591 file->private_data = ctrl;
1592 ret = 0;
1593 break;
1594 }
1595 spin_unlock(&dev_list_lock);
1596
1597 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001598}
1599
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001600static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001601{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001602 nvme_put_ctrl(file->private_data);
1603 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001604}
1605
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001606static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1607{
1608 struct nvme_ns *ns;
1609 int ret;
1610
1611 mutex_lock(&ctrl->namespaces_mutex);
1612 if (list_empty(&ctrl->namespaces)) {
1613 ret = -ENOTTY;
1614 goto out_unlock;
1615 }
1616
1617 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1618 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001619 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001620 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1621 ret = -EINVAL;
1622 goto out_unlock;
1623 }
1624
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001625 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001626 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1627 kref_get(&ns->kref);
1628 mutex_unlock(&ctrl->namespaces_mutex);
1629
1630 ret = nvme_user_cmd(ctrl, ns, argp);
1631 nvme_put_ns(ns);
1632 return ret;
1633
1634out_unlock:
1635 mutex_unlock(&ctrl->namespaces_mutex);
1636 return ret;
1637}
1638
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001639static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1640 unsigned long arg)
1641{
1642 struct nvme_ctrl *ctrl = file->private_data;
1643 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001644
1645 switch (cmd) {
1646 case NVME_IOCTL_ADMIN_CMD:
1647 return nvme_user_cmd(ctrl, NULL, argp);
1648 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001649 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001650 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001651 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001652 return ctrl->ops->reset_ctrl(ctrl);
1653 case NVME_IOCTL_SUBSYS_RESET:
1654 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001655 case NVME_IOCTL_RESCAN:
1656 nvme_queue_scan(ctrl);
1657 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001658 default:
1659 return -ENOTTY;
1660 }
1661}
1662
1663static const struct file_operations nvme_dev_fops = {
1664 .owner = THIS_MODULE,
1665 .open = nvme_dev_open,
1666 .release = nvme_dev_release,
1667 .unlocked_ioctl = nvme_dev_ioctl,
1668 .compat_ioctl = nvme_dev_ioctl,
1669};
1670
1671static ssize_t nvme_sysfs_reset(struct device *dev,
1672 struct device_attribute *attr, const char *buf,
1673 size_t count)
1674{
1675 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1676 int ret;
1677
1678 ret = ctrl->ops->reset_ctrl(ctrl);
1679 if (ret < 0)
1680 return ret;
1681 return count;
1682}
1683static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1684
Keith Busch9ec3bb22016-04-29 15:45:18 -06001685static ssize_t nvme_sysfs_rescan(struct device *dev,
1686 struct device_attribute *attr, const char *buf,
1687 size_t count)
1688{
1689 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1690
1691 nvme_queue_scan(ctrl);
1692 return count;
1693}
1694static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1695
Keith Busch118472a2016-02-18 09:57:48 -07001696static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1697 char *buf)
1698{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001699 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001700 struct nvme_ctrl *ctrl = ns->ctrl;
1701 int serial_len = sizeof(ctrl->serial);
1702 int model_len = sizeof(ctrl->model);
1703
1704 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1705 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1706
1707 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1708 return sprintf(buf, "eui.%8phN\n", ns->eui);
1709
1710 while (ctrl->serial[serial_len - 1] == ' ')
1711 serial_len--;
1712 while (ctrl->model[model_len - 1] == ' ')
1713 model_len--;
1714
1715 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1716 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1717}
1718static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1719
Keith Busch2b9b6e82015-12-22 10:10:45 -07001720static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1721 char *buf)
1722{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001723 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001724 return sprintf(buf, "%pU\n", ns->uuid);
1725}
1726static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1727
1728static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1729 char *buf)
1730{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001731 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001732 return sprintf(buf, "%8phd\n", ns->eui);
1733}
1734static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1735
1736static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1737 char *buf)
1738{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001739 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001740 return sprintf(buf, "%d\n", ns->ns_id);
1741}
1742static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1743
1744static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001745 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001746 &dev_attr_uuid.attr,
1747 &dev_attr_eui.attr,
1748 &dev_attr_nsid.attr,
1749 NULL,
1750};
1751
Ming Lin1a353d82016-06-13 16:45:24 +02001752static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001753 struct attribute *a, int n)
1754{
1755 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001756 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001757
1758 if (a == &dev_attr_uuid.attr) {
1759 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1760 return 0;
1761 }
1762 if (a == &dev_attr_eui.attr) {
1763 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1764 return 0;
1765 }
1766 return a->mode;
1767}
1768
1769static const struct attribute_group nvme_ns_attr_group = {
1770 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001771 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001772};
1773
Ming Lin931e1c22016-02-26 13:24:19 -08001774#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001775static ssize_t field##_show(struct device *dev, \
1776 struct device_attribute *attr, char *buf) \
1777{ \
1778 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1779 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1780} \
1781static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1782
Ming Lin931e1c22016-02-26 13:24:19 -08001783#define nvme_show_int_function(field) \
1784static ssize_t field##_show(struct device *dev, \
1785 struct device_attribute *attr, char *buf) \
1786{ \
1787 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1788 return sprintf(buf, "%d\n", ctrl->field); \
1789} \
1790static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1791
1792nvme_show_str_function(model);
1793nvme_show_str_function(serial);
1794nvme_show_str_function(firmware_rev);
1795nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001796
Ming Lin1a353d82016-06-13 16:45:24 +02001797static ssize_t nvme_sysfs_delete(struct device *dev,
1798 struct device_attribute *attr, const char *buf,
1799 size_t count)
1800{
1801 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1802
1803 if (device_remove_file_self(dev, attr))
1804 ctrl->ops->delete_ctrl(ctrl);
1805 return count;
1806}
1807static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1808
1809static ssize_t nvme_sysfs_show_transport(struct device *dev,
1810 struct device_attribute *attr,
1811 char *buf)
1812{
1813 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1814
1815 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1816}
1817static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1818
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001819static ssize_t nvme_sysfs_show_state(struct device *dev,
1820 struct device_attribute *attr,
1821 char *buf)
1822{
1823 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1824 static const char *const state_name[] = {
1825 [NVME_CTRL_NEW] = "new",
1826 [NVME_CTRL_LIVE] = "live",
1827 [NVME_CTRL_RESETTING] = "resetting",
1828 [NVME_CTRL_RECONNECTING]= "reconnecting",
1829 [NVME_CTRL_DELETING] = "deleting",
1830 [NVME_CTRL_DEAD] = "dead",
1831 };
1832
1833 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1834 state_name[ctrl->state])
1835 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1836
1837 return sprintf(buf, "unknown state\n");
1838}
1839
1840static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
1841
Ming Lin1a353d82016-06-13 16:45:24 +02001842static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1843 struct device_attribute *attr,
1844 char *buf)
1845{
1846 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1847
1848 return snprintf(buf, PAGE_SIZE, "%s\n",
1849 ctrl->ops->get_subsysnqn(ctrl));
1850}
1851static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1852
1853static ssize_t nvme_sysfs_show_address(struct device *dev,
1854 struct device_attribute *attr,
1855 char *buf)
1856{
1857 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1858
1859 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1860}
1861static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1862
Keith Busch779ff7562016-01-12 15:09:31 -07001863static struct attribute *nvme_dev_attrs[] = {
1864 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001865 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001866 &dev_attr_model.attr,
1867 &dev_attr_serial.attr,
1868 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001869 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02001870 &dev_attr_delete_controller.attr,
1871 &dev_attr_transport.attr,
1872 &dev_attr_subsysnqn.attr,
1873 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001874 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001875 NULL
1876};
1877
Ming Lin1a353d82016-06-13 16:45:24 +02001878#define CHECK_ATTR(ctrl, a, name) \
1879 if ((a) == &dev_attr_##name.attr && \
1880 !(ctrl)->ops->get_##name) \
1881 return 0
1882
1883static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1884 struct attribute *a, int n)
1885{
1886 struct device *dev = container_of(kobj, struct device, kobj);
1887 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1888
1889 if (a == &dev_attr_delete_controller.attr) {
1890 if (!ctrl->ops->delete_ctrl)
1891 return 0;
1892 }
1893
1894 CHECK_ATTR(ctrl, a, subsysnqn);
1895 CHECK_ATTR(ctrl, a, address);
1896
1897 return a->mode;
1898}
1899
Keith Busch779ff7562016-01-12 15:09:31 -07001900static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02001901 .attrs = nvme_dev_attrs,
1902 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07001903};
1904
1905static const struct attribute_group *nvme_dev_attr_groups[] = {
1906 &nvme_dev_attrs_group,
1907 NULL,
1908};
1909
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001910static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1911{
1912 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1913 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1914
1915 return nsa->ns_id - nsb->ns_id;
1916}
1917
Keith Busch32f0c4a2016-07-13 11:45:02 -06001918static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001919{
Keith Busch32f0c4a2016-07-13 11:45:02 -06001920 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001921
Keith Busch32f0c4a2016-07-13 11:45:02 -06001922 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001923 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001924 if (ns->ns_id == nsid) {
1925 kref_get(&ns->kref);
1926 ret = ns;
1927 break;
1928 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001929 if (ns->ns_id > nsid)
1930 break;
1931 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001932 mutex_unlock(&ctrl->namespaces_mutex);
1933 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001934}
1935
1936static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1937{
1938 struct nvme_ns *ns;
1939 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001940 struct nvme_id_ns *id;
1941 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001942 int node = dev_to_node(ctrl->dev);
1943
1944 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1945 if (!ns)
1946 return;
1947
Keith Busch075790e2016-02-24 09:15:53 -07001948 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1949 if (ns->instance < 0)
1950 goto out_free_ns;
1951
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001952 ns->queue = blk_mq_init_queue(ctrl->tagset);
1953 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07001954 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001955 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1956 ns->queue->queuedata = ns;
1957 ns->ctrl = ctrl;
1958
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001959 kref_init(&ns->kref);
1960 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001961 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001962
1963 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01001964 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001965
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001966 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001967
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001968 if (nvme_revalidate_ns(ns, &id))
1969 goto out_free_queue;
1970
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001971 if (nvme_nvm_ns_supported(ns, id) &&
1972 nvme_nvm_register(ns, disk_name, node)) {
1973 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
1974 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001975 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001976
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001977 disk = alloc_disk_node(0, node);
1978 if (!disk)
1979 goto out_free_id;
1980
1981 disk->fops = &nvme_fops;
1982 disk->private_data = ns;
1983 disk->queue = ns->queue;
1984 disk->flags = GENHD_FL_EXT_DEVT;
1985 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
1986 ns->disk = disk;
1987
1988 __nvme_revalidate_disk(disk, id);
1989
Keith Busch32f0c4a2016-07-13 11:45:02 -06001990 mutex_lock(&ctrl->namespaces_mutex);
1991 list_add_tail(&ns->list, &ctrl->namespaces);
1992 mutex_unlock(&ctrl->namespaces_mutex);
1993
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001994 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001995
1996 kfree(id);
1997
Dan Williams0d52c7562016-06-15 19:44:20 -07001998 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001999 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2000 &nvme_ns_attr_group))
2001 pr_warn("%s: failed to create sysfs group for identification\n",
2002 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002003 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2004 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2005 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002006 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002007 out_free_id:
2008 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002009 out_free_queue:
2010 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002011 out_release_instance:
2012 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002013 out_free_ns:
2014 kfree(ns);
2015}
2016
2017static void nvme_ns_remove(struct nvme_ns *ns)
2018{
Keith Busch646017a2016-02-24 09:15:54 -07002019 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2020 return;
2021
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002022 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002023 if (blk_get_integrity(ns->disk))
2024 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002025 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2026 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002027 if (ns->ndev)
2028 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002029 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002030 blk_mq_abort_requeue_list(ns->queue);
2031 blk_cleanup_queue(ns->queue);
2032 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002033
2034 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002035 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002036 mutex_unlock(&ns->ctrl->namespaces_mutex);
2037
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002038 nvme_put_ns(ns);
2039}
2040
Keith Busch540c8012015-10-22 15:45:06 -06002041static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2042{
2043 struct nvme_ns *ns;
2044
Keith Busch32f0c4a2016-07-13 11:45:02 -06002045 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002046 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002047 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002048 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002049 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002050 } else
2051 nvme_alloc_ns(ctrl, nsid);
2052}
2053
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302054static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2055 unsigned nsid)
2056{
2057 struct nvme_ns *ns, *next;
2058
2059 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2060 if (ns->ns_id > nsid)
2061 nvme_ns_remove(ns);
2062 }
2063}
2064
Keith Busch540c8012015-10-22 15:45:06 -06002065static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2066{
2067 struct nvme_ns *ns;
2068 __le32 *ns_list;
2069 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2070 int ret = 0;
2071
2072 ns_list = kzalloc(0x1000, GFP_KERNEL);
2073 if (!ns_list)
2074 return -ENOMEM;
2075
2076 for (i = 0; i < num_lists; i++) {
2077 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2078 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302079 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002080
2081 for (j = 0; j < min(nn, 1024U); j++) {
2082 nsid = le32_to_cpu(ns_list[j]);
2083 if (!nsid)
2084 goto out;
2085
2086 nvme_validate_ns(ctrl, nsid);
2087
2088 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002089 ns = nvme_find_get_ns(ctrl, prev);
2090 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002091 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002092 nvme_put_ns(ns);
2093 }
Keith Busch540c8012015-10-22 15:45:06 -06002094 }
2095 }
2096 nn -= j;
2097 }
2098 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302099 nvme_remove_invalid_namespaces(ctrl, prev);
2100 free:
Keith Busch540c8012015-10-22 15:45:06 -06002101 kfree(ns_list);
2102 return ret;
2103}
2104
Christoph Hellwig5955be22016-04-26 13:51:59 +02002105static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002106{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002107 unsigned i;
2108
Keith Busch540c8012015-10-22 15:45:06 -06002109 for (i = 1; i <= nn; i++)
2110 nvme_validate_ns(ctrl, i);
2111
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302112 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002113}
2114
Christoph Hellwig5955be22016-04-26 13:51:59 +02002115static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002116{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002117 struct nvme_ctrl *ctrl =
2118 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002119 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002120 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002121
Christoph Hellwig5955be22016-04-26 13:51:59 +02002122 if (ctrl->state != NVME_CTRL_LIVE)
2123 return;
2124
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002125 if (nvme_identify_ctrl(ctrl, &id))
2126 return;
Keith Busch540c8012015-10-22 15:45:06 -06002127
2128 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002129 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002130 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2131 if (!nvme_scan_ns_list(ctrl, nn))
2132 goto done;
2133 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002134 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002135 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002136 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002137 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002138 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002139 kfree(id);
2140}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002141
2142void nvme_queue_scan(struct nvme_ctrl *ctrl)
2143{
2144 /*
2145 * Do not queue new scan work when a controller is reset during
2146 * removal.
2147 */
2148 if (ctrl->state == NVME_CTRL_LIVE)
2149 schedule_work(&ctrl->scan_work);
2150}
2151EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002152
Keith Busch32f0c4a2016-07-13 11:45:02 -06002153/*
2154 * This function iterates the namespace list unlocked to allow recovery from
2155 * controller failure. It is up to the caller to ensure the namespace list is
2156 * not modified by scan work while this function is executing.
2157 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002158void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2159{
2160 struct nvme_ns *ns, *next;
2161
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002162 /*
2163 * The dead states indicates the controller was not gracefully
2164 * disconnected. In that case, we won't be able to flush any data while
2165 * removing the namespaces' disks; fail all the queues now to avoid
2166 * potentially having to clean up the failed sync later.
2167 */
2168 if (ctrl->state == NVME_CTRL_DEAD)
2169 nvme_kill_queues(ctrl);
2170
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002171 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2172 nvme_ns_remove(ns);
2173}
Ming Lin576d55d2016-02-10 10:03:32 -08002174EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002175
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002176static void nvme_async_event_work(struct work_struct *work)
2177{
2178 struct nvme_ctrl *ctrl =
2179 container_of(work, struct nvme_ctrl, async_event_work);
2180
2181 spin_lock_irq(&ctrl->lock);
2182 while (ctrl->event_limit > 0) {
2183 int aer_idx = --ctrl->event_limit;
2184
2185 spin_unlock_irq(&ctrl->lock);
2186 ctrl->ops->submit_async_event(ctrl, aer_idx);
2187 spin_lock_irq(&ctrl->lock);
2188 }
2189 spin_unlock_irq(&ctrl->lock);
2190}
2191
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002192void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2193 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002194{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002195 u32 result = le32_to_cpu(res->u32);
2196 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002197
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002198 switch (le16_to_cpu(status) >> 1) {
2199 case NVME_SC_SUCCESS:
2200 done = false;
2201 /*FALLTHRU*/
2202 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002203 ++ctrl->event_limit;
2204 schedule_work(&ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002205 break;
2206 default:
2207 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002208 }
2209
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002210 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002211 return;
2212
2213 switch (result & 0xff07) {
2214 case NVME_AER_NOTICE_NS_CHANGED:
2215 dev_info(ctrl->device, "rescanning\n");
2216 nvme_queue_scan(ctrl);
2217 break;
2218 default:
2219 dev_warn(ctrl->device, "async event result %08x\n", result);
2220 }
2221}
2222EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2223
2224void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2225{
2226 ctrl->event_limit = NVME_NR_AERS;
2227 schedule_work(&ctrl->async_event_work);
2228}
2229EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2230
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002231static DEFINE_IDA(nvme_instance_ida);
2232
2233static int nvme_set_instance(struct nvme_ctrl *ctrl)
2234{
2235 int instance, error;
2236
2237 do {
2238 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2239 return -ENODEV;
2240
2241 spin_lock(&dev_list_lock);
2242 error = ida_get_new(&nvme_instance_ida, &instance);
2243 spin_unlock(&dev_list_lock);
2244 } while (error == -EAGAIN);
2245
2246 if (error)
2247 return -ENODEV;
2248
2249 ctrl->instance = instance;
2250 return 0;
2251}
2252
2253static void nvme_release_instance(struct nvme_ctrl *ctrl)
2254{
2255 spin_lock(&dev_list_lock);
2256 ida_remove(&nvme_instance_ida, ctrl->instance);
2257 spin_unlock(&dev_list_lock);
2258}
2259
Keith Busch53029b02015-11-28 15:41:02 +01002260void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002261{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002262 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002263 flush_work(&ctrl->scan_work);
2264 nvme_remove_namespaces(ctrl);
2265
Keith Busch53029b02015-11-28 15:41:02 +01002266 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002267
2268 spin_lock(&dev_list_lock);
2269 list_del(&ctrl->node);
2270 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002271}
Ming Lin576d55d2016-02-10 10:03:32 -08002272EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002273
2274static void nvme_free_ctrl(struct kref *kref)
2275{
2276 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002277
2278 put_device(ctrl->device);
2279 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002280 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002281
2282 ctrl->ops->free_ctrl(ctrl);
2283}
2284
2285void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2286{
2287 kref_put(&ctrl->kref, nvme_free_ctrl);
2288}
Ming Lin576d55d2016-02-10 10:03:32 -08002289EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002290
2291/*
2292 * Initialize a NVMe controller structures. This needs to be called during
2293 * earliest initialization so that we have the initialized structured around
2294 * during probing.
2295 */
2296int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2297 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2298{
2299 int ret;
2300
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002301 ctrl->state = NVME_CTRL_NEW;
2302 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002303 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002304 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002305 kref_init(&ctrl->kref);
2306 ctrl->dev = dev;
2307 ctrl->ops = ops;
2308 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002309 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002310 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002311
2312 ret = nvme_set_instance(ctrl);
2313 if (ret)
2314 goto out;
2315
Keith Busch779ff7562016-01-12 15:09:31 -07002316 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002317 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002318 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002319 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002320 if (IS_ERR(ctrl->device)) {
2321 ret = PTR_ERR(ctrl->device);
2322 goto out_release_instance;
2323 }
2324 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002325 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002326
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002327 spin_lock(&dev_list_lock);
2328 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2329 spin_unlock(&dev_list_lock);
2330
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002331 /*
2332 * Initialize latency tolerance controls. The sysfs files won't
2333 * be visible to userspace unless the device actually supports APST.
2334 */
2335 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2336 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2337 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2338
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002339 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002340out_release_instance:
2341 nvme_release_instance(ctrl);
2342out:
2343 return ret;
2344}
Ming Lin576d55d2016-02-10 10:03:32 -08002345EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002346
Keith Busch69d9a992016-02-24 09:15:56 -07002347/**
2348 * nvme_kill_queues(): Ends all namespace queues
2349 * @ctrl: the dead controller that needs to end
2350 *
2351 * Call this function when the driver determines it is unable to get the
2352 * controller in a state capable of servicing IO.
2353 */
2354void nvme_kill_queues(struct nvme_ctrl *ctrl)
2355{
2356 struct nvme_ns *ns;
2357
Keith Busch32f0c4a2016-07-13 11:45:02 -06002358 mutex_lock(&ctrl->namespaces_mutex);
2359 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002360 /*
2361 * Revalidating a dead namespace sets capacity to 0. This will
2362 * end buffered writers dirtying pages that can't be synced.
2363 */
Keith Buschf33447b2017-02-10 18:15:51 -05002364 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2365 continue;
2366 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002367 blk_set_queue_dying(ns->queue);
2368 blk_mq_abort_requeue_list(ns->queue);
2369 blk_mq_start_stopped_hw_queues(ns->queue, true);
Keith Busch69d9a992016-02-24 09:15:56 -07002370 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002371 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002372}
Linus Torvalds237045f2016-03-18 17:13:31 -07002373EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002374
Keith Busch302ad8c2017-03-01 14:22:12 -05002375void nvme_unfreeze(struct nvme_ctrl *ctrl)
2376{
2377 struct nvme_ns *ns;
2378
2379 mutex_lock(&ctrl->namespaces_mutex);
2380 list_for_each_entry(ns, &ctrl->namespaces, list)
2381 blk_mq_unfreeze_queue(ns->queue);
2382 mutex_unlock(&ctrl->namespaces_mutex);
2383}
2384EXPORT_SYMBOL_GPL(nvme_unfreeze);
2385
2386void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2387{
2388 struct nvme_ns *ns;
2389
2390 mutex_lock(&ctrl->namespaces_mutex);
2391 list_for_each_entry(ns, &ctrl->namespaces, list) {
2392 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2393 if (timeout <= 0)
2394 break;
2395 }
2396 mutex_unlock(&ctrl->namespaces_mutex);
2397}
2398EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2399
2400void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2401{
2402 struct nvme_ns *ns;
2403
2404 mutex_lock(&ctrl->namespaces_mutex);
2405 list_for_each_entry(ns, &ctrl->namespaces, list)
2406 blk_mq_freeze_queue_wait(ns->queue);
2407 mutex_unlock(&ctrl->namespaces_mutex);
2408}
2409EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2410
2411void nvme_start_freeze(struct nvme_ctrl *ctrl)
2412{
2413 struct nvme_ns *ns;
2414
2415 mutex_lock(&ctrl->namespaces_mutex);
2416 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002417 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002418 mutex_unlock(&ctrl->namespaces_mutex);
2419}
2420EXPORT_SYMBOL_GPL(nvme_start_freeze);
2421
Keith Busch25646262016-01-04 09:10:57 -07002422void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002423{
2424 struct nvme_ns *ns;
2425
Keith Busch32f0c4a2016-07-13 11:45:02 -06002426 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002427 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002428 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002429 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002430}
Ming Lin576d55d2016-02-10 10:03:32 -08002431EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002432
Keith Busch25646262016-01-04 09:10:57 -07002433void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002434{
2435 struct nvme_ns *ns;
2436
Keith Busch32f0c4a2016-07-13 11:45:02 -06002437 mutex_lock(&ctrl->namespaces_mutex);
2438 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002439 blk_mq_start_stopped_hw_queues(ns->queue, true);
2440 blk_mq_kick_requeue_list(ns->queue);
2441 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002442 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002443}
Ming Lin576d55d2016-02-10 10:03:32 -08002444EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002445
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002446int __init nvme_core_init(void)
2447{
2448 int result;
2449
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002450 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2451 &nvme_dev_fops);
2452 if (result < 0)
NeilBrownb09dcf52016-07-13 11:03:58 -07002453 return result;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002454 else if (result > 0)
2455 nvme_char_major = result;
2456
2457 nvme_class = class_create(THIS_MODULE, "nvme");
2458 if (IS_ERR(nvme_class)) {
2459 result = PTR_ERR(nvme_class);
2460 goto unregister_chrdev;
2461 }
2462
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002463 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002464
2465 unregister_chrdev:
2466 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002467 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002468}
2469
2470void nvme_core_exit(void)
2471{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002472 class_destroy(nvme_class);
2473 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002474}
Ming Lin576d55d2016-02-10 10:03:32 -08002475
2476MODULE_LICENSE("GPL");
2477MODULE_VERSION("1.0");
2478module_init(nvme_core_init);
2479module_exit(nvme_core_exit);