blob: c6f256d74b6b68df50d3da1f1685a7b6eefc5614 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <scsi/sg.h>
31#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010032
33#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020034#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010035
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010036#define NVME_MINORS (1U << MINORBITS)
37
Ming Linba0ba7d2016-02-10 10:03:30 -080038unsigned char admin_timeout = 60;
39module_param(admin_timeout, byte, 0644);
40MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080041EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080042
43unsigned char nvme_io_timeout = 30;
44module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
45MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080046EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080047
48unsigned char shutdown_timeout = 5;
49module_param(shutdown_timeout, byte, 0644);
50MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
51
Christoph Hellwig44e44b22017-04-05 19:18:11 +020052static u8 nvme_max_retries = 5;
53module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070054MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010055
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010056static int nvme_char_major;
57module_param(nvme_char_major, int, 0);
58
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059static unsigned long default_ps_max_latency_us = 25000;
60module_param(default_ps_max_latency_us, ulong, 0644);
61MODULE_PARM_DESC(default_ps_max_latency_us,
62 "max power saving latency for new devices; use PM QOS to change per device");
63
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010064static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080065static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010066
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010067static struct class *nvme_class;
68
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020069int nvme_error_status(struct request *req)
70{
71 switch (nvme_req(req)->status & 0x7ff) {
72 case NVME_SC_SUCCESS:
73 return 0;
74 case NVME_SC_CAP_EXCEEDED:
75 return -ENOSPC;
76 default:
77 return -EIO;
78 }
79}
80EXPORT_SYMBOL_GPL(nvme_error_status);
81
Christoph Hellwigf6324b12017-04-05 19:18:09 +020082static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +020083{
Christoph Hellwigf6324b12017-04-05 19:18:09 +020084 if (blk_noretry_request(req))
85 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020086 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +020087 return false;
88 if (jiffies - req->start_time >= req->timeout)
89 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +020090 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +020091 return false;
92 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +020093}
94
95void nvme_complete_rq(struct request *req)
96{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020097 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
98 nvme_req(req)->retries++;
99 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
100 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200101 }
102
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200103 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200104}
105EXPORT_SYMBOL_GPL(nvme_complete_rq);
106
Ming Linc55a2fd2016-05-18 14:05:02 -0700107void nvme_cancel_request(struct request *req, void *data, bool reserved)
108{
109 int status;
110
111 if (!blk_mq_request_started(req))
112 return;
113
114 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
115 "Cancelling I/O %d", req->tag);
116
117 status = NVME_SC_ABORT_REQ;
118 if (blk_queue_dying(req->q))
119 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200120 nvme_req(req)->status = status;
121 blk_mq_complete_request(req, 0);
122
Ming Linc55a2fd2016-05-18 14:05:02 -0700123}
124EXPORT_SYMBOL_GPL(nvme_cancel_request);
125
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200126bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
127 enum nvme_ctrl_state new_state)
128{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300129 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200130 bool changed = false;
131
132 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300133
134 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200135 switch (new_state) {
136 case NVME_CTRL_LIVE:
137 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200138 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200139 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900140 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200141 changed = true;
142 /* FALLTHRU */
143 default:
144 break;
145 }
146 break;
147 case NVME_CTRL_RESETTING:
148 switch (old_state) {
149 case NVME_CTRL_NEW:
150 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900151 case NVME_CTRL_RECONNECTING:
152 changed = true;
153 /* FALLTHRU */
154 default:
155 break;
156 }
157 break;
158 case NVME_CTRL_RECONNECTING:
159 switch (old_state) {
160 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200161 changed = true;
162 /* FALLTHRU */
163 default:
164 break;
165 }
166 break;
167 case NVME_CTRL_DELETING:
168 switch (old_state) {
169 case NVME_CTRL_LIVE:
170 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900171 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200172 changed = true;
173 /* FALLTHRU */
174 default:
175 break;
176 }
177 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600178 case NVME_CTRL_DEAD:
179 switch (old_state) {
180 case NVME_CTRL_DELETING:
181 changed = true;
182 /* FALLTHRU */
183 default:
184 break;
185 }
186 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200187 default:
188 break;
189 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200190
191 if (changed)
192 ctrl->state = new_state;
193
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300194 spin_unlock_irq(&ctrl->lock);
195
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200196 return changed;
197}
198EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
199
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100200static void nvme_free_ns(struct kref *kref)
201{
202 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
203
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200204 if (ns->ndev)
205 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100206
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200207 if (ns->disk) {
208 spin_lock(&dev_list_lock);
209 ns->disk->private_data = NULL;
210 spin_unlock(&dev_list_lock);
211 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100212
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100213 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700214 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
215 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100216 kfree(ns);
217}
218
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100219static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100220{
221 kref_put(&ns->kref, nvme_free_ns);
222}
223
224static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
225{
226 struct nvme_ns *ns;
227
228 spin_lock(&dev_list_lock);
229 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800230 if (ns) {
231 if (!kref_get_unless_zero(&ns->kref))
232 goto fail;
233 if (!try_module_get(ns->ctrl->ops->module))
234 goto fail_put_ns;
235 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100236 spin_unlock(&dev_list_lock);
237
238 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800239
240fail_put_ns:
241 kref_put(&ns->kref, nvme_free_ns);
242fail:
243 spin_unlock(&dev_list_lock);
244 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100245}
246
Christoph Hellwig41609822015-11-20 09:00:02 +0100247struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200248 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100249{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100250 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100251 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100252
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200253 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100254 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200255 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100256 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200257 qid ? qid - 1 : 0);
258 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100259 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100260 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100261
Christoph Hellwig21d34712015-11-26 09:08:36 +0100262 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800263 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100264
Christoph Hellwig41609822015-11-20 09:00:02 +0100265 return req;
266}
Ming Lin576d55d2016-02-10 10:03:32 -0800267EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100268
Ming Lin8093f7c2016-04-12 13:10:14 -0600269static inline void nvme_setup_flush(struct nvme_ns *ns,
270 struct nvme_command *cmnd)
271{
272 memset(cmnd, 0, sizeof(*cmnd));
273 cmnd->common.opcode = nvme_cmd_flush;
274 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
275}
276
277static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
278 struct nvme_command *cmnd)
279{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100280 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600281 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100282 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600283
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100284 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600285 if (!range)
286 return BLK_MQ_RQ_QUEUE_BUSY;
287
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100288 __rq_for_each_bio(bio, req) {
289 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
290 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
291
292 range[n].cattr = cpu_to_le32(0);
293 range[n].nlb = cpu_to_le32(nlb);
294 range[n].slba = cpu_to_le64(slba);
295 n++;
296 }
297
298 if (WARN_ON_ONCE(n != segments)) {
299 kfree(range);
300 return BLK_MQ_RQ_QUEUE_ERROR;
301 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600302
303 memset(cmnd, 0, sizeof(*cmnd));
304 cmnd->dsm.opcode = nvme_cmd_dsm;
305 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200306 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600307 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
308
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700309 req->special_vec.bv_page = virt_to_page(range);
310 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100311 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700312 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600313
Omar Sandovalbac00002016-11-15 11:11:58 -0800314 return BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600315}
Ming Lin8093f7c2016-04-12 13:10:14 -0600316
Ming Lin8093f7c2016-04-12 13:10:14 -0600317static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
318 struct nvme_command *cmnd)
319{
320 u16 control = 0;
321 u32 dsmgmt = 0;
322
323 if (req->cmd_flags & REQ_FUA)
324 control |= NVME_RW_FUA;
325 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
326 control |= NVME_RW_LR;
327
328 if (req->cmd_flags & REQ_RAHEAD)
329 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
330
331 memset(cmnd, 0, sizeof(*cmnd));
332 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600333 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
334 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
335 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
336
337 if (ns->ms) {
338 switch (ns->pi_type) {
339 case NVME_NS_DPS_PI_TYPE3:
340 control |= NVME_RW_PRINFO_PRCHK_GUARD;
341 break;
342 case NVME_NS_DPS_PI_TYPE1:
343 case NVME_NS_DPS_PI_TYPE2:
344 control |= NVME_RW_PRINFO_PRCHK_GUARD |
345 NVME_RW_PRINFO_PRCHK_REF;
346 cmnd->rw.reftag = cpu_to_le32(
347 nvme_block_nr(ns, blk_rq_pos(req)));
348 break;
349 }
350 if (!blk_integrity_rq(req))
351 control |= NVME_RW_PRINFO_PRACT;
352 }
353
354 cmnd->rw.control = cpu_to_le16(control);
355 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
356}
357
358int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
359 struct nvme_command *cmd)
360{
Omar Sandovalbac00002016-11-15 11:11:58 -0800361 int ret = BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600362
Christoph Hellwig987f6992017-04-05 19:18:08 +0200363 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200364 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200365 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200366 req->rq_flags |= RQF_DONTPREP;
367 }
368
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100369 switch (req_op(req)) {
370 case REQ_OP_DRV_IN:
371 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800372 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100373 break;
374 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600375 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100376 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200377 case REQ_OP_WRITE_ZEROES:
378 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100379 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600380 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100381 break;
382 case REQ_OP_READ:
383 case REQ_OP_WRITE:
Ming Lin8093f7c2016-04-12 13:10:14 -0600384 nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100385 break;
386 default:
387 WARN_ON_ONCE(1);
388 return BLK_MQ_RQ_QUEUE_ERROR;
389 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600390
James Smart721b3912016-10-21 23:33:34 +0300391 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600392 return ret;
393}
394EXPORT_SYMBOL_GPL(nvme_setup_cmd);
395
Christoph Hellwig41609822015-11-20 09:00:02 +0100396/*
397 * Returns 0 on success. If the result is negative, it's a Linux error code;
398 * if the result is positive, it's an NVM Express status code
399 */
400int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800401 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200402 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100403{
404 struct request *req;
405 int ret;
406
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200407 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100408 if (IS_ERR(req))
409 return PTR_ERR(req);
410
411 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
412
Christoph Hellwig21d34712015-11-26 09:08:36 +0100413 if (buffer && bufflen) {
414 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
415 if (ret)
416 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100417 }
418
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200419 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800420 if (result)
421 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200422 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
423 ret = -EINTR;
424 else
425 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100426 out:
427 blk_mq_free_request(req);
428 return ret;
429}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200430EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100431
432int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
433 void *buffer, unsigned bufflen)
434{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200435 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
436 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100437}
Ming Lin576d55d2016-02-10 10:03:32 -0800438EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100439
Keith Busch0b7f1f22015-10-23 09:47:28 -0600440int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
441 void __user *ubuffer, unsigned bufflen,
442 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
443 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100444{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200445 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600446 struct nvme_ns *ns = q->queuedata;
447 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100448 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600449 struct bio *bio = NULL;
450 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100451 int ret;
452
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200453 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100454 if (IS_ERR(req))
455 return PTR_ERR(req);
456
457 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
458
459 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100460 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
461 GFP_KERNEL);
462 if (ret)
463 goto out;
464 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100465
Keith Busch0b7f1f22015-10-23 09:47:28 -0600466 if (!disk)
467 goto submit;
468 bio->bi_bdev = bdget_disk(disk, 0);
469 if (!bio->bi_bdev) {
470 ret = -ENODEV;
471 goto out_unmap;
472 }
473
Keith Busche9fc63d2016-02-24 09:15:58 -0700474 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600475 struct bio_integrity_payload *bip;
476
477 meta = kmalloc(meta_len, GFP_KERNEL);
478 if (!meta) {
479 ret = -ENOMEM;
480 goto out_unmap;
481 }
482
483 if (write) {
484 if (copy_from_user(meta, meta_buffer,
485 meta_len)) {
486 ret = -EFAULT;
487 goto out_free_meta;
488 }
489 }
490
491 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700492 if (IS_ERR(bip)) {
493 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600494 goto out_free_meta;
495 }
496
497 bip->bip_iter.bi_size = meta_len;
498 bip->bip_iter.bi_sector = meta_seed;
499
500 ret = bio_integrity_add_page(bio, virt_to_page(meta),
501 meta_len, offset_in_page(meta));
502 if (ret != meta_len) {
503 ret = -ENOMEM;
504 goto out_free_meta;
505 }
506 }
507 }
508 submit:
509 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200510 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
511 ret = -EINTR;
512 else
513 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100514 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800515 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600516 if (meta && !ret && !write) {
517 if (copy_to_user(meta_buffer, meta, meta_len))
518 ret = -EFAULT;
519 }
520 out_free_meta:
521 kfree(meta);
522 out_unmap:
523 if (bio) {
524 if (disk && bio->bi_bdev)
525 bdput(bio->bi_bdev);
526 blk_rq_unmap_user(bio);
527 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100528 out:
529 blk_mq_free_request(req);
530 return ret;
531}
532
Keith Busch0b7f1f22015-10-23 09:47:28 -0600533int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
534 void __user *ubuffer, unsigned bufflen, u32 *result,
535 unsigned timeout)
536{
537 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
538 result, timeout);
539}
540
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200541static void nvme_keep_alive_end_io(struct request *rq, int error)
542{
543 struct nvme_ctrl *ctrl = rq->end_io_data;
544
545 blk_mq_free_request(rq);
546
547 if (error) {
548 dev_err(ctrl->device,
549 "failed nvme_keep_alive_end_io error=%d\n", error);
550 return;
551 }
552
553 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
554}
555
556static int nvme_keep_alive(struct nvme_ctrl *ctrl)
557{
558 struct nvme_command c;
559 struct request *rq;
560
561 memset(&c, 0, sizeof(c));
562 c.common.opcode = nvme_admin_keep_alive;
563
564 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
565 NVME_QID_ANY);
566 if (IS_ERR(rq))
567 return PTR_ERR(rq);
568
569 rq->timeout = ctrl->kato * HZ;
570 rq->end_io_data = ctrl;
571
572 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
573
574 return 0;
575}
576
577static void nvme_keep_alive_work(struct work_struct *work)
578{
579 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
580 struct nvme_ctrl, ka_work);
581
582 if (nvme_keep_alive(ctrl)) {
583 /* allocation failure, reset the controller */
584 dev_err(ctrl->device, "keep-alive failed\n");
585 ctrl->ops->reset_ctrl(ctrl);
586 return;
587 }
588}
589
590void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
591{
592 if (unlikely(ctrl->kato == 0))
593 return;
594
595 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
596 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
597}
598EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
599
600void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
601{
602 if (unlikely(ctrl->kato == 0))
603 return;
604
605 cancel_delayed_work_sync(&ctrl->ka_work);
606}
607EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
608
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100609int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100610{
611 struct nvme_command c = { };
612 int error;
613
614 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
615 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200616 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100617
618 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
619 if (!*id)
620 return -ENOMEM;
621
622 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
623 sizeof(struct nvme_id_ctrl));
624 if (error)
625 kfree(*id);
626 return error;
627}
628
Keith Busch540c8012015-10-22 15:45:06 -0600629static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
630{
631 struct nvme_command c = { };
632
633 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200634 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600635 c.identify.nsid = cpu_to_le32(nsid);
636 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
637}
638
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100639int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100640 struct nvme_id_ns **id)
641{
642 struct nvme_command c = { };
643 int error;
644
645 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200646 c.identify.opcode = nvme_admin_identify;
647 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200648 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100649
650 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
651 if (!*id)
652 return -ENOMEM;
653
654 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
655 sizeof(struct nvme_id_ns));
656 if (error)
657 kfree(*id);
658 return error;
659}
660
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100661int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
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_get_features;
670 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100671 c.features.fid = cpu_to_le32(fid);
672
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800673 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200674 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_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700681 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100682{
683 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800684 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100685 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100686
687 memset(&c, 0, sizeof(c));
688 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100689 c.features.fid = cpu_to_le32(fid);
690 c.features.dword11 = cpu_to_le32(dword11);
691
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800692 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700693 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700694 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800695 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100696 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100697}
698
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100699int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100700{
701 struct nvme_command c = { };
702 int error;
703
704 c.common.opcode = nvme_admin_get_log_page,
705 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
706 c.common.cdw10[0] = cpu_to_le32(
707 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
708 NVME_LOG_SMART),
709
710 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
711 if (!*log)
712 return -ENOMEM;
713
714 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
715 sizeof(struct nvme_smart_log));
716 if (error)
717 kfree(*log);
718 return error;
719}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100720
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100721int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
722{
723 u32 q_count = (*count - 1) | ((*count - 1) << 16);
724 u32 result;
725 int status, nr_io_queues;
726
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700727 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100728 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200729 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100730 return status;
731
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200732 /*
733 * Degraded controllers might return an error when setting the queue
734 * count. We still want to be able to bring them online and offer
735 * access to the admin queue, as that might be only way to fix them up.
736 */
737 if (status > 0) {
738 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
739 *count = 0;
740 } else {
741 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
742 *count = min(*count, nr_io_queues);
743 }
744
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100745 return 0;
746}
Ming Lin576d55d2016-02-10 10:03:32 -0800747EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100748
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100749static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
750{
751 struct nvme_user_io io;
752 struct nvme_command c;
753 unsigned length, meta_len;
754 void __user *metadata;
755
756 if (copy_from_user(&io, uio, sizeof(io)))
757 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700758 if (io.flags)
759 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100760
761 switch (io.opcode) {
762 case nvme_cmd_write:
763 case nvme_cmd_read:
764 case nvme_cmd_compare:
765 break;
766 default:
767 return -EINVAL;
768 }
769
770 length = (io.nblocks + 1) << ns->lba_shift;
771 meta_len = (io.nblocks + 1) * ns->ms;
772 metadata = (void __user *)(uintptr_t)io.metadata;
773
774 if (ns->ext) {
775 length += meta_len;
776 meta_len = 0;
777 } else if (meta_len) {
778 if ((io.metadata & 3) || !io.metadata)
779 return -EINVAL;
780 }
781
782 memset(&c, 0, sizeof(c));
783 c.rw.opcode = io.opcode;
784 c.rw.flags = io.flags;
785 c.rw.nsid = cpu_to_le32(ns->ns_id);
786 c.rw.slba = cpu_to_le64(io.slba);
787 c.rw.length = cpu_to_le16(io.nblocks);
788 c.rw.control = cpu_to_le16(io.control);
789 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
790 c.rw.reftag = cpu_to_le32(io.reftag);
791 c.rw.apptag = cpu_to_le16(io.apptag);
792 c.rw.appmask = cpu_to_le16(io.appmask);
793
794 return __nvme_submit_user_cmd(ns->queue, &c,
795 (void __user *)(uintptr_t)io.addr, length,
796 metadata, meta_len, io.slba, NULL, 0);
797}
798
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100799static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100800 struct nvme_passthru_cmd __user *ucmd)
801{
802 struct nvme_passthru_cmd cmd;
803 struct nvme_command c;
804 unsigned timeout = 0;
805 int status;
806
807 if (!capable(CAP_SYS_ADMIN))
808 return -EACCES;
809 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
810 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700811 if (cmd.flags)
812 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100813
814 memset(&c, 0, sizeof(c));
815 c.common.opcode = cmd.opcode;
816 c.common.flags = cmd.flags;
817 c.common.nsid = cpu_to_le32(cmd.nsid);
818 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
819 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
820 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
821 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
822 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
823 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
824 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
825 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
826
827 if (cmd.timeout_ms)
828 timeout = msecs_to_jiffies(cmd.timeout_ms);
829
830 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100831 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100832 &cmd.result, timeout);
833 if (status >= 0) {
834 if (put_user(cmd.result, &ucmd->result))
835 return -EFAULT;
836 }
837
838 return status;
839}
840
841static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
842 unsigned int cmd, unsigned long arg)
843{
844 struct nvme_ns *ns = bdev->bd_disk->private_data;
845
846 switch (cmd) {
847 case NVME_IOCTL_ID:
848 force_successful_syscall_return();
849 return ns->ns_id;
850 case NVME_IOCTL_ADMIN_CMD:
851 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
852 case NVME_IOCTL_IO_CMD:
853 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
854 case NVME_IOCTL_SUBMIT_IO:
855 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100856#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100857 case SG_GET_VERSION_NUM:
858 return nvme_sg_get_version_num((void __user *)arg);
859 case SG_IO:
860 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100861#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100862 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100863#ifdef CONFIG_NVM
864 if (ns->ndev)
865 return nvme_nvm_ioctl(ns, cmd, arg);
866#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700867 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100868 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700869 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100870 return -ENOTTY;
871 }
872}
873
874#ifdef CONFIG_COMPAT
875static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
876 unsigned int cmd, unsigned long arg)
877{
878 switch (cmd) {
879 case SG_IO:
880 return -ENOIOCTLCMD;
881 }
882 return nvme_ioctl(bdev, mode, cmd, arg);
883}
884#else
885#define nvme_compat_ioctl NULL
886#endif
887
888static int nvme_open(struct block_device *bdev, fmode_t mode)
889{
890 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
891}
892
893static void nvme_release(struct gendisk *disk, fmode_t mode)
894{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800895 struct nvme_ns *ns = disk->private_data;
896
897 module_put(ns->ctrl->ops->module);
898 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100899}
900
901static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
902{
903 /* some standard values */
904 geo->heads = 1 << 6;
905 geo->sectors = 1 << 5;
906 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
907 return 0;
908}
909
910#ifdef CONFIG_BLK_DEV_INTEGRITY
911static void nvme_init_integrity(struct nvme_ns *ns)
912{
913 struct blk_integrity integrity;
914
Jay Freyenseefa9a89f2016-07-20 21:26:16 -0600915 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100916 switch (ns->pi_type) {
917 case NVME_NS_DPS_PI_TYPE3:
918 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000919 integrity.tag_size = sizeof(u16) + sizeof(u32);
920 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100921 break;
922 case NVME_NS_DPS_PI_TYPE1:
923 case NVME_NS_DPS_PI_TYPE2:
924 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000925 integrity.tag_size = sizeof(u16);
926 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100927 break;
928 default:
929 integrity.profile = NULL;
930 break;
931 }
932 integrity.tuple_size = ns->ms;
933 blk_integrity_register(ns->disk, &integrity);
934 blk_queue_max_integrity_segments(ns->queue, 1);
935}
936#else
937static void nvme_init_integrity(struct nvme_ns *ns)
938{
939}
940#endif /* CONFIG_BLK_DEV_INTEGRITY */
941
942static void nvme_config_discard(struct nvme_ns *ns)
943{
Keith Busch08095e72016-03-04 13:15:17 -0700944 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100945 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700946
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100947 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
948 NVME_DSM_MAX_RANGES);
949
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100950 ns->queue->limits.discard_alignment = logical_block_size;
951 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800952 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100953 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100954 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +0200955
956 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
957 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100958}
959
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200960static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100961{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200962 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200963 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100964 return -ENODEV;
965 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200966
967 if ((*id)->ncap == 0) {
968 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100969 return -ENODEV;
970 }
971
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600972 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200973 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600974 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200975 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
976
977 return 0;
978}
979
980static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
981{
982 struct nvme_ns *ns = disk->private_data;
983 u8 lbaf, pi_type;
984 u16 old_ms;
985 unsigned short bs;
Keith Busch2b9b6e82015-12-22 10:10:45 -0700986
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100987 old_ms = ns->ms;
988 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
989 ns->lba_shift = id->lbaf[lbaf].ds;
990 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
991 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
992
993 /*
994 * If identify namespace failed, use default 512 byte block size so
995 * block layer can use before failing read/write for 0 capacity.
996 */
997 if (ns->lba_shift == 0)
998 ns->lba_shift = 9;
999 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001000 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1001 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1002 id->dps & NVME_NS_DPS_PI_MASK : 0;
1003
1004 blk_mq_freeze_queue(disk->queue);
1005 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1006 ns->ms != old_ms ||
1007 bs != queue_logical_block_size(disk->queue) ||
1008 (ns->ms && ns->ext)))
1009 blk_integrity_unregister(disk);
1010
1011 ns->pi_type = pi_type;
1012 blk_queue_logical_block_size(ns->queue, bs);
1013
Keith Busch4b9d5b12015-11-20 09:13:30 +01001014 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001015 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001016 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1017 set_capacity(disk, 0);
1018 else
1019 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1020
1021 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1022 nvme_config_discard(ns);
1023 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001024}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001025
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001026static int nvme_revalidate_disk(struct gendisk *disk)
1027{
1028 struct nvme_ns *ns = disk->private_data;
1029 struct nvme_id_ns *id = NULL;
1030 int ret;
1031
1032 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1033 set_capacity(disk, 0);
1034 return -ENODEV;
1035 }
1036
1037 ret = nvme_revalidate_ns(ns, &id);
1038 if (ret)
1039 return ret;
1040
1041 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001042 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001043
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001044 return 0;
1045}
1046
1047static char nvme_pr_type(enum pr_type type)
1048{
1049 switch (type) {
1050 case PR_WRITE_EXCLUSIVE:
1051 return 1;
1052 case PR_EXCLUSIVE_ACCESS:
1053 return 2;
1054 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1055 return 3;
1056 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1057 return 4;
1058 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1059 return 5;
1060 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1061 return 6;
1062 default:
1063 return 0;
1064 }
1065};
1066
1067static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1068 u64 key, u64 sa_key, u8 op)
1069{
1070 struct nvme_ns *ns = bdev->bd_disk->private_data;
1071 struct nvme_command c;
1072 u8 data[16] = { 0, };
1073
1074 put_unaligned_le64(key, &data[0]);
1075 put_unaligned_le64(sa_key, &data[8]);
1076
1077 memset(&c, 0, sizeof(c));
1078 c.common.opcode = op;
1079 c.common.nsid = cpu_to_le32(ns->ns_id);
1080 c.common.cdw10[0] = cpu_to_le32(cdw10);
1081
1082 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1083}
1084
1085static int nvme_pr_register(struct block_device *bdev, u64 old,
1086 u64 new, unsigned flags)
1087{
1088 u32 cdw10;
1089
1090 if (flags & ~PR_FL_IGNORE_KEY)
1091 return -EOPNOTSUPP;
1092
1093 cdw10 = old ? 2 : 0;
1094 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1095 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1096 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1097}
1098
1099static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1100 enum pr_type type, unsigned flags)
1101{
1102 u32 cdw10;
1103
1104 if (flags & ~PR_FL_IGNORE_KEY)
1105 return -EOPNOTSUPP;
1106
1107 cdw10 = nvme_pr_type(type) << 8;
1108 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1109 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1110}
1111
1112static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1113 enum pr_type type, bool abort)
1114{
1115 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1116 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1117}
1118
1119static int nvme_pr_clear(struct block_device *bdev, u64 key)
1120{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001121 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001122 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1123}
1124
1125static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1126{
1127 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1128 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1129}
1130
1131static const struct pr_ops nvme_pr_ops = {
1132 .pr_register = nvme_pr_register,
1133 .pr_reserve = nvme_pr_reserve,
1134 .pr_release = nvme_pr_release,
1135 .pr_preempt = nvme_pr_preempt,
1136 .pr_clear = nvme_pr_clear,
1137};
1138
Scott Bauera98e58e52017-02-03 12:50:32 -07001139#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001140int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1141 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001142{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001143 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001144 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001145
1146 memset(&cmd, 0, sizeof(cmd));
1147 if (send)
1148 cmd.common.opcode = nvme_admin_security_send;
1149 else
1150 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001151 cmd.common.nsid = 0;
1152 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1153 cmd.common.cdw10[1] = cpu_to_le32(len);
1154
1155 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1156 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1157}
1158EXPORT_SYMBOL_GPL(nvme_sec_submit);
1159#endif /* CONFIG_BLK_SED_OPAL */
1160
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001161static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001162 .owner = THIS_MODULE,
1163 .ioctl = nvme_ioctl,
1164 .compat_ioctl = nvme_compat_ioctl,
1165 .open = nvme_open,
1166 .release = nvme_release,
1167 .getgeo = nvme_getgeo,
1168 .revalidate_disk= nvme_revalidate_disk,
1169 .pr_ops = &nvme_pr_ops,
1170};
1171
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001172static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1173{
1174 unsigned long timeout =
1175 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1176 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1177 int ret;
1178
1179 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001180 if (csts == ~0)
1181 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001182 if ((csts & NVME_CSTS_RDY) == bit)
1183 break;
1184
1185 msleep(100);
1186 if (fatal_signal_pending(current))
1187 return -EINTR;
1188 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001189 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001190 "Device not ready; aborting %s\n", enabled ?
1191 "initialisation" : "reset");
1192 return -ENODEV;
1193 }
1194 }
1195
1196 return ret;
1197}
1198
1199/*
1200 * If the device has been passed off to us in an enabled state, just clear
1201 * the enabled bit. The spec says we should set the 'shutdown notification
1202 * bits', but doing so may cause the device to complete commands to the
1203 * admin queue ... and we don't know what memory that might be pointing at!
1204 */
1205int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1206{
1207 int ret;
1208
1209 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1210 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1211
1212 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1213 if (ret)
1214 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001215
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001216 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001217 msleep(NVME_QUIRK_DELAY_AMOUNT);
1218
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001219 return nvme_wait_ready(ctrl, cap, false);
1220}
Ming Lin576d55d2016-02-10 10:03:32 -08001221EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001222
1223int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1224{
1225 /*
1226 * Default to a 4K page size, with the intention to update this
1227 * path in the future to accomodate architectures with differing
1228 * kernel and IO page sizes.
1229 */
1230 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1231 int ret;
1232
1233 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001234 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001235 "Minimum device page size %u too large for host (%u)\n",
1236 1 << dev_page_min, 1 << page_shift);
1237 return -ENODEV;
1238 }
1239
1240 ctrl->page_size = 1 << page_shift;
1241
1242 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1243 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1244 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1245 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1246 ctrl->ctrl_config |= NVME_CC_ENABLE;
1247
1248 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1249 if (ret)
1250 return ret;
1251 return nvme_wait_ready(ctrl, cap, true);
1252}
Ming Lin576d55d2016-02-10 10:03:32 -08001253EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001254
1255int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1256{
1257 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1258 u32 csts;
1259 int ret;
1260
1261 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1262 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1263
1264 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1265 if (ret)
1266 return ret;
1267
1268 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1269 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1270 break;
1271
1272 msleep(100);
1273 if (fatal_signal_pending(current))
1274 return -EINTR;
1275 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001276 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001277 "Device shutdown incomplete; abort shutdown\n");
1278 return -ENODEV;
1279 }
1280 }
1281
1282 return ret;
1283}
Ming Lin576d55d2016-02-10 10:03:32 -08001284EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001285
Christoph Hellwigda358252016-03-02 18:07:11 +01001286static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1287 struct request_queue *q)
1288{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001289 bool vwc = false;
1290
Christoph Hellwigda358252016-03-02 18:07:11 +01001291 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001292 u32 max_segments =
1293 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1294
Christoph Hellwigda358252016-03-02 18:07:11 +01001295 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001296 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001297 }
Keith Busche6282ae2016-12-19 11:37:50 -05001298 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1299 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001300 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001301 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1302 vwc = true;
1303 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001304}
1305
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001306static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1307{
1308 /*
1309 * APST (Autonomous Power State Transition) lets us program a
1310 * table of power state transitions that the controller will
1311 * perform automatically. We configure it with a simple
1312 * heuristic: we are willing to spend at most 2% of the time
1313 * transitioning between power states. Therefore, when running
1314 * in any given state, we will enter the next lower-power
1315 * non-operational state after waiting 100 * (enlat + exlat)
1316 * microseconds, as long as that state's total latency is under
1317 * the requested maximum latency.
1318 *
1319 * We will not autonomously enter any non-operational state for
1320 * which the total latency exceeds ps_max_latency_us. Users
1321 * can set ps_max_latency_us to zero to turn off APST.
1322 */
1323
1324 unsigned apste;
1325 struct nvme_feat_auto_pst *table;
1326 int ret;
1327
1328 /*
1329 * If APST isn't supported or if we haven't been initialized yet,
1330 * then don't do anything.
1331 */
1332 if (!ctrl->apsta)
1333 return;
1334
1335 if (ctrl->npss > 31) {
1336 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1337 return;
1338 }
1339
1340 table = kzalloc(sizeof(*table), GFP_KERNEL);
1341 if (!table)
1342 return;
1343
1344 if (ctrl->ps_max_latency_us == 0) {
1345 /* Turn off APST. */
1346 apste = 0;
1347 } else {
1348 __le64 target = cpu_to_le64(0);
1349 int state;
1350
1351 /*
1352 * Walk through all states from lowest- to highest-power.
1353 * According to the spec, lower-numbered states use more
1354 * power. NPSS, despite the name, is the index of the
1355 * lowest-power state, not the number of states.
1356 */
1357 for (state = (int)ctrl->npss; state >= 0; state--) {
1358 u64 total_latency_us, transition_ms;
1359
1360 if (target)
1361 table->entries[state] = target;
1362
1363 /*
1364 * Is this state a useful non-operational state for
1365 * higher-power states to autonomously transition to?
1366 */
1367 if (!(ctrl->psd[state].flags &
1368 NVME_PS_FLAGS_NON_OP_STATE))
1369 continue;
1370
1371 total_latency_us =
1372 (u64)le32_to_cpu(ctrl->psd[state].entry_lat) +
1373 + le32_to_cpu(ctrl->psd[state].exit_lat);
1374 if (total_latency_us > ctrl->ps_max_latency_us)
1375 continue;
1376
1377 /*
1378 * This state is good. Use it as the APST idle
1379 * target for higher power states.
1380 */
1381 transition_ms = total_latency_us + 19;
1382 do_div(transition_ms, 20);
1383 if (transition_ms > (1 << 24) - 1)
1384 transition_ms = (1 << 24) - 1;
1385
1386 target = cpu_to_le64((state << 3) |
1387 (transition_ms << 8));
1388 }
1389
1390 apste = 1;
1391 }
1392
1393 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1394 table, sizeof(*table), NULL);
1395 if (ret)
1396 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1397
1398 kfree(table);
1399}
1400
1401static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1402{
1403 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1404 u64 latency;
1405
1406 switch (val) {
1407 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1408 case PM_QOS_LATENCY_ANY:
1409 latency = U64_MAX;
1410 break;
1411
1412 default:
1413 latency = val;
1414 }
1415
1416 if (ctrl->ps_max_latency_us != latency) {
1417 ctrl->ps_max_latency_us = latency;
1418 nvme_configure_apst(ctrl);
1419 }
1420}
1421
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001422struct nvme_core_quirk_entry {
1423 /*
1424 * NVMe model and firmware strings are padded with spaces. For
1425 * simplicity, strings in the quirk table are padded with NULLs
1426 * instead.
1427 */
1428 u16 vid;
1429 const char *mn;
1430 const char *fr;
1431 unsigned long quirks;
1432};
1433
1434static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001435 /*
1436 * Seen on a Samsung "SM951 NVMe SAMSUNG 256GB": using APST causes
1437 * the controller to go out to lunch. It dies when the watchdog
1438 * timer reads CSTS and gets 0xffffffff.
1439 */
1440 {
1441 .vid = 0x144d,
1442 .fr = "BXW75D0Q",
1443 .quirks = NVME_QUIRK_NO_APST,
1444 },
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001445};
1446
1447/* match is null-terminated but idstr is space-padded. */
1448static bool string_matches(const char *idstr, const char *match, size_t len)
1449{
1450 size_t matchlen;
1451
1452 if (!match)
1453 return true;
1454
1455 matchlen = strlen(match);
1456 WARN_ON_ONCE(matchlen > len);
1457
1458 if (memcmp(idstr, match, matchlen))
1459 return false;
1460
1461 for (; matchlen < len; matchlen++)
1462 if (idstr[matchlen] != ' ')
1463 return false;
1464
1465 return true;
1466}
1467
1468static bool quirk_matches(const struct nvme_id_ctrl *id,
1469 const struct nvme_core_quirk_entry *q)
1470{
1471 return q->vid == le16_to_cpu(id->vid) &&
1472 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1473 string_matches(id->fr, q->fr, sizeof(id->fr));
1474}
1475
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001476/*
1477 * Initialize the cached copies of the Identify data and various controller
1478 * register in our nvme_ctrl structure. This should be called as soon as
1479 * the admin queue is fully up and running.
1480 */
1481int nvme_init_identify(struct nvme_ctrl *ctrl)
1482{
1483 struct nvme_id_ctrl *id;
1484 u64 cap;
1485 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001486 u32 max_hw_sectors;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001487 u8 prev_apsta;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001488
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001489 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1490 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001491 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001492 return ret;
1493 }
1494
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001495 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1496 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001497 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001498 return ret;
1499 }
1500 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1501
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001502 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001503 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1504
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001505 ret = nvme_identify_ctrl(ctrl, &id);
1506 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001507 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001508 return -EIO;
1509 }
1510
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001511 if (!ctrl->identified) {
1512 /*
1513 * Check for quirks. Quirk can depend on firmware version,
1514 * so, in principle, the set of quirks present can change
1515 * across a reset. As a possible future enhancement, we
1516 * could re-scan for quirks every time we reinitialize
1517 * the device, but we'd have to make sure that the driver
1518 * behaves intelligently if the quirks change.
1519 */
1520
1521 int i;
1522
1523 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1524 if (quirk_matches(id, &core_quirks[i]))
1525 ctrl->quirks |= core_quirks[i].quirks;
1526 }
1527 }
1528
Scott Bauer8a9ae522017-02-17 13:59:40 +01001529 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001530 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001531 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001532 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001533 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001534 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001535 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1536 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1537 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1538 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001539 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001540 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001541 max_hw_sectors = UINT_MAX;
1542 ctrl->max_hw_sectors =
1543 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001544
Christoph Hellwigda358252016-03-02 18:07:11 +01001545 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001546 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001547 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001548
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001549 ctrl->npss = id->npss;
1550 prev_apsta = ctrl->apsta;
1551 ctrl->apsta = (ctrl->quirks & NVME_QUIRK_NO_APST) ? 0 : id->apsta;
1552 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1553
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001554 if (ctrl->ops->is_fabrics) {
1555 ctrl->icdoff = le16_to_cpu(id->icdoff);
1556 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1557 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1558 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1559
1560 /*
1561 * In fabrics we need to verify the cntlid matches the
1562 * admin connect
1563 */
1564 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1565 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001566
1567 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1568 dev_err(ctrl->dev,
1569 "keep-alive support is mandatory for fabrics\n");
1570 ret = -EINVAL;
1571 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001572 } else {
1573 ctrl->cntlid = le16_to_cpu(id->cntlid);
1574 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001575
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001576 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001577
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001578 if (ctrl->apsta && !prev_apsta)
1579 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1580 else if (!ctrl->apsta && prev_apsta)
1581 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1582
1583 nvme_configure_apst(ctrl);
1584
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001585 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001586
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001587 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001588}
Ming Lin576d55d2016-02-10 10:03:32 -08001589EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001590
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001591static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001592{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001593 struct nvme_ctrl *ctrl;
1594 int instance = iminor(inode);
1595 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001596
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001597 spin_lock(&dev_list_lock);
1598 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1599 if (ctrl->instance != instance)
1600 continue;
1601
1602 if (!ctrl->admin_q) {
1603 ret = -EWOULDBLOCK;
1604 break;
1605 }
1606 if (!kref_get_unless_zero(&ctrl->kref))
1607 break;
1608 file->private_data = ctrl;
1609 ret = 0;
1610 break;
1611 }
1612 spin_unlock(&dev_list_lock);
1613
1614 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001615}
1616
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001617static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001618{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001619 nvme_put_ctrl(file->private_data);
1620 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001621}
1622
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001623static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1624{
1625 struct nvme_ns *ns;
1626 int ret;
1627
1628 mutex_lock(&ctrl->namespaces_mutex);
1629 if (list_empty(&ctrl->namespaces)) {
1630 ret = -ENOTTY;
1631 goto out_unlock;
1632 }
1633
1634 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1635 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001636 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001637 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1638 ret = -EINVAL;
1639 goto out_unlock;
1640 }
1641
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001642 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001643 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1644 kref_get(&ns->kref);
1645 mutex_unlock(&ctrl->namespaces_mutex);
1646
1647 ret = nvme_user_cmd(ctrl, ns, argp);
1648 nvme_put_ns(ns);
1649 return ret;
1650
1651out_unlock:
1652 mutex_unlock(&ctrl->namespaces_mutex);
1653 return ret;
1654}
1655
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001656static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1657 unsigned long arg)
1658{
1659 struct nvme_ctrl *ctrl = file->private_data;
1660 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001661
1662 switch (cmd) {
1663 case NVME_IOCTL_ADMIN_CMD:
1664 return nvme_user_cmd(ctrl, NULL, argp);
1665 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001666 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001667 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001668 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001669 return ctrl->ops->reset_ctrl(ctrl);
1670 case NVME_IOCTL_SUBSYS_RESET:
1671 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001672 case NVME_IOCTL_RESCAN:
1673 nvme_queue_scan(ctrl);
1674 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001675 default:
1676 return -ENOTTY;
1677 }
1678}
1679
1680static const struct file_operations nvme_dev_fops = {
1681 .owner = THIS_MODULE,
1682 .open = nvme_dev_open,
1683 .release = nvme_dev_release,
1684 .unlocked_ioctl = nvme_dev_ioctl,
1685 .compat_ioctl = nvme_dev_ioctl,
1686};
1687
1688static ssize_t nvme_sysfs_reset(struct device *dev,
1689 struct device_attribute *attr, const char *buf,
1690 size_t count)
1691{
1692 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1693 int ret;
1694
1695 ret = ctrl->ops->reset_ctrl(ctrl);
1696 if (ret < 0)
1697 return ret;
1698 return count;
1699}
1700static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1701
Keith Busch9ec3bb22016-04-29 15:45:18 -06001702static ssize_t nvme_sysfs_rescan(struct device *dev,
1703 struct device_attribute *attr, const char *buf,
1704 size_t count)
1705{
1706 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1707
1708 nvme_queue_scan(ctrl);
1709 return count;
1710}
1711static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1712
Keith Busch118472a2016-02-18 09:57:48 -07001713static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1714 char *buf)
1715{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001716 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001717 struct nvme_ctrl *ctrl = ns->ctrl;
1718 int serial_len = sizeof(ctrl->serial);
1719 int model_len = sizeof(ctrl->model);
1720
1721 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1722 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1723
1724 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1725 return sprintf(buf, "eui.%8phN\n", ns->eui);
1726
1727 while (ctrl->serial[serial_len - 1] == ' ')
1728 serial_len--;
1729 while (ctrl->model[model_len - 1] == ' ')
1730 model_len--;
1731
1732 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1733 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1734}
1735static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1736
Keith Busch2b9b6e82015-12-22 10:10:45 -07001737static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1738 char *buf)
1739{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001740 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001741 return sprintf(buf, "%pU\n", ns->uuid);
1742}
1743static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1744
1745static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1746 char *buf)
1747{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001748 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001749 return sprintf(buf, "%8phd\n", ns->eui);
1750}
1751static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1752
1753static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1754 char *buf)
1755{
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 return sprintf(buf, "%d\n", ns->ns_id);
1758}
1759static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1760
1761static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001762 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001763 &dev_attr_uuid.attr,
1764 &dev_attr_eui.attr,
1765 &dev_attr_nsid.attr,
1766 NULL,
1767};
1768
Ming Lin1a353d82016-06-13 16:45:24 +02001769static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001770 struct attribute *a, int n)
1771{
1772 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001773 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001774
1775 if (a == &dev_attr_uuid.attr) {
1776 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1777 return 0;
1778 }
1779 if (a == &dev_attr_eui.attr) {
1780 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1781 return 0;
1782 }
1783 return a->mode;
1784}
1785
1786static const struct attribute_group nvme_ns_attr_group = {
1787 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001788 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001789};
1790
Ming Lin931e1c22016-02-26 13:24:19 -08001791#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001792static ssize_t field##_show(struct device *dev, \
1793 struct device_attribute *attr, char *buf) \
1794{ \
1795 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1796 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1797} \
1798static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1799
Ming Lin931e1c22016-02-26 13:24:19 -08001800#define nvme_show_int_function(field) \
1801static ssize_t field##_show(struct device *dev, \
1802 struct device_attribute *attr, char *buf) \
1803{ \
1804 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1805 return sprintf(buf, "%d\n", ctrl->field); \
1806} \
1807static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1808
1809nvme_show_str_function(model);
1810nvme_show_str_function(serial);
1811nvme_show_str_function(firmware_rev);
1812nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001813
Ming Lin1a353d82016-06-13 16:45:24 +02001814static ssize_t nvme_sysfs_delete(struct device *dev,
1815 struct device_attribute *attr, const char *buf,
1816 size_t count)
1817{
1818 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1819
1820 if (device_remove_file_self(dev, attr))
1821 ctrl->ops->delete_ctrl(ctrl);
1822 return count;
1823}
1824static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1825
1826static ssize_t nvme_sysfs_show_transport(struct device *dev,
1827 struct device_attribute *attr,
1828 char *buf)
1829{
1830 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1831
1832 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1833}
1834static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1835
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001836static ssize_t nvme_sysfs_show_state(struct device *dev,
1837 struct device_attribute *attr,
1838 char *buf)
1839{
1840 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1841 static const char *const state_name[] = {
1842 [NVME_CTRL_NEW] = "new",
1843 [NVME_CTRL_LIVE] = "live",
1844 [NVME_CTRL_RESETTING] = "resetting",
1845 [NVME_CTRL_RECONNECTING]= "reconnecting",
1846 [NVME_CTRL_DELETING] = "deleting",
1847 [NVME_CTRL_DEAD] = "dead",
1848 };
1849
1850 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1851 state_name[ctrl->state])
1852 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1853
1854 return sprintf(buf, "unknown state\n");
1855}
1856
1857static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
1858
Ming Lin1a353d82016-06-13 16:45:24 +02001859static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1862{
1863 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1864
1865 return snprintf(buf, PAGE_SIZE, "%s\n",
1866 ctrl->ops->get_subsysnqn(ctrl));
1867}
1868static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1869
1870static ssize_t nvme_sysfs_show_address(struct device *dev,
1871 struct device_attribute *attr,
1872 char *buf)
1873{
1874 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1875
1876 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1877}
1878static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1879
Keith Busch779ff7562016-01-12 15:09:31 -07001880static struct attribute *nvme_dev_attrs[] = {
1881 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001882 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001883 &dev_attr_model.attr,
1884 &dev_attr_serial.attr,
1885 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001886 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02001887 &dev_attr_delete_controller.attr,
1888 &dev_attr_transport.attr,
1889 &dev_attr_subsysnqn.attr,
1890 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001891 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001892 NULL
1893};
1894
Ming Lin1a353d82016-06-13 16:45:24 +02001895#define CHECK_ATTR(ctrl, a, name) \
1896 if ((a) == &dev_attr_##name.attr && \
1897 !(ctrl)->ops->get_##name) \
1898 return 0
1899
1900static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1901 struct attribute *a, int n)
1902{
1903 struct device *dev = container_of(kobj, struct device, kobj);
1904 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1905
1906 if (a == &dev_attr_delete_controller.attr) {
1907 if (!ctrl->ops->delete_ctrl)
1908 return 0;
1909 }
1910
1911 CHECK_ATTR(ctrl, a, subsysnqn);
1912 CHECK_ATTR(ctrl, a, address);
1913
1914 return a->mode;
1915}
1916
Keith Busch779ff7562016-01-12 15:09:31 -07001917static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02001918 .attrs = nvme_dev_attrs,
1919 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07001920};
1921
1922static const struct attribute_group *nvme_dev_attr_groups[] = {
1923 &nvme_dev_attrs_group,
1924 NULL,
1925};
1926
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001927static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1928{
1929 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1930 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1931
1932 return nsa->ns_id - nsb->ns_id;
1933}
1934
Keith Busch32f0c4a2016-07-13 11:45:02 -06001935static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001936{
Keith Busch32f0c4a2016-07-13 11:45:02 -06001937 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001938
Keith Busch32f0c4a2016-07-13 11:45:02 -06001939 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001940 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001941 if (ns->ns_id == nsid) {
1942 kref_get(&ns->kref);
1943 ret = ns;
1944 break;
1945 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001946 if (ns->ns_id > nsid)
1947 break;
1948 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001949 mutex_unlock(&ctrl->namespaces_mutex);
1950 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001951}
1952
1953static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1954{
1955 struct nvme_ns *ns;
1956 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001957 struct nvme_id_ns *id;
1958 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001959 int node = dev_to_node(ctrl->dev);
1960
1961 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1962 if (!ns)
1963 return;
1964
Keith Busch075790e2016-02-24 09:15:53 -07001965 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1966 if (ns->instance < 0)
1967 goto out_free_ns;
1968
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001969 ns->queue = blk_mq_init_queue(ctrl->tagset);
1970 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07001971 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001972 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1973 ns->queue->queuedata = ns;
1974 ns->ctrl = ctrl;
1975
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001976 kref_init(&ns->kref);
1977 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001978 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001979
1980 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01001981 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001982
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001983 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001984
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001985 if (nvme_revalidate_ns(ns, &id))
1986 goto out_free_queue;
1987
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001988 if (nvme_nvm_ns_supported(ns, id) &&
1989 nvme_nvm_register(ns, disk_name, node)) {
1990 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
1991 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001992 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001993
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001994 disk = alloc_disk_node(0, node);
1995 if (!disk)
1996 goto out_free_id;
1997
1998 disk->fops = &nvme_fops;
1999 disk->private_data = ns;
2000 disk->queue = ns->queue;
2001 disk->flags = GENHD_FL_EXT_DEVT;
2002 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2003 ns->disk = disk;
2004
2005 __nvme_revalidate_disk(disk, id);
2006
Keith Busch32f0c4a2016-07-13 11:45:02 -06002007 mutex_lock(&ctrl->namespaces_mutex);
2008 list_add_tail(&ns->list, &ctrl->namespaces);
2009 mutex_unlock(&ctrl->namespaces_mutex);
2010
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002011 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002012
2013 kfree(id);
2014
Dan Williams0d52c7562016-06-15 19:44:20 -07002015 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002016 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2017 &nvme_ns_attr_group))
2018 pr_warn("%s: failed to create sysfs group for identification\n",
2019 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002020 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2021 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2022 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002023 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002024 out_free_id:
2025 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002026 out_free_queue:
2027 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002028 out_release_instance:
2029 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002030 out_free_ns:
2031 kfree(ns);
2032}
2033
2034static void nvme_ns_remove(struct nvme_ns *ns)
2035{
Keith Busch646017a2016-02-24 09:15:54 -07002036 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2037 return;
2038
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002039 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002040 if (blk_get_integrity(ns->disk))
2041 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002042 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2043 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002044 if (ns->ndev)
2045 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002046 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002047 blk_mq_abort_requeue_list(ns->queue);
2048 blk_cleanup_queue(ns->queue);
2049 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002050
2051 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002052 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002053 mutex_unlock(&ns->ctrl->namespaces_mutex);
2054
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002055 nvme_put_ns(ns);
2056}
2057
Keith Busch540c8012015-10-22 15:45:06 -06002058static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2059{
2060 struct nvme_ns *ns;
2061
Keith Busch32f0c4a2016-07-13 11:45:02 -06002062 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002063 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002064 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002065 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002066 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002067 } else
2068 nvme_alloc_ns(ctrl, nsid);
2069}
2070
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302071static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2072 unsigned nsid)
2073{
2074 struct nvme_ns *ns, *next;
2075
2076 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2077 if (ns->ns_id > nsid)
2078 nvme_ns_remove(ns);
2079 }
2080}
2081
Keith Busch540c8012015-10-22 15:45:06 -06002082static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2083{
2084 struct nvme_ns *ns;
2085 __le32 *ns_list;
2086 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2087 int ret = 0;
2088
2089 ns_list = kzalloc(0x1000, GFP_KERNEL);
2090 if (!ns_list)
2091 return -ENOMEM;
2092
2093 for (i = 0; i < num_lists; i++) {
2094 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2095 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302096 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002097
2098 for (j = 0; j < min(nn, 1024U); j++) {
2099 nsid = le32_to_cpu(ns_list[j]);
2100 if (!nsid)
2101 goto out;
2102
2103 nvme_validate_ns(ctrl, nsid);
2104
2105 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002106 ns = nvme_find_get_ns(ctrl, prev);
2107 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002108 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002109 nvme_put_ns(ns);
2110 }
Keith Busch540c8012015-10-22 15:45:06 -06002111 }
2112 }
2113 nn -= j;
2114 }
2115 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302116 nvme_remove_invalid_namespaces(ctrl, prev);
2117 free:
Keith Busch540c8012015-10-22 15:45:06 -06002118 kfree(ns_list);
2119 return ret;
2120}
2121
Christoph Hellwig5955be22016-04-26 13:51:59 +02002122static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002123{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002124 unsigned i;
2125
Keith Busch540c8012015-10-22 15:45:06 -06002126 for (i = 1; i <= nn; i++)
2127 nvme_validate_ns(ctrl, i);
2128
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302129 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002130}
2131
Christoph Hellwig5955be22016-04-26 13:51:59 +02002132static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002133{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002134 struct nvme_ctrl *ctrl =
2135 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002136 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002137 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002138
Christoph Hellwig5955be22016-04-26 13:51:59 +02002139 if (ctrl->state != NVME_CTRL_LIVE)
2140 return;
2141
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002142 if (nvme_identify_ctrl(ctrl, &id))
2143 return;
Keith Busch540c8012015-10-22 15:45:06 -06002144
2145 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002146 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002147 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2148 if (!nvme_scan_ns_list(ctrl, nn))
2149 goto done;
2150 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002151 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002152 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002153 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002154 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002155 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002156 kfree(id);
2157}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002158
2159void nvme_queue_scan(struct nvme_ctrl *ctrl)
2160{
2161 /*
2162 * Do not queue new scan work when a controller is reset during
2163 * removal.
2164 */
2165 if (ctrl->state == NVME_CTRL_LIVE)
2166 schedule_work(&ctrl->scan_work);
2167}
2168EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002169
Keith Busch32f0c4a2016-07-13 11:45:02 -06002170/*
2171 * This function iterates the namespace list unlocked to allow recovery from
2172 * controller failure. It is up to the caller to ensure the namespace list is
2173 * not modified by scan work while this function is executing.
2174 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002175void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2176{
2177 struct nvme_ns *ns, *next;
2178
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002179 /*
2180 * The dead states indicates the controller was not gracefully
2181 * disconnected. In that case, we won't be able to flush any data while
2182 * removing the namespaces' disks; fail all the queues now to avoid
2183 * potentially having to clean up the failed sync later.
2184 */
2185 if (ctrl->state == NVME_CTRL_DEAD)
2186 nvme_kill_queues(ctrl);
2187
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002188 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2189 nvme_ns_remove(ns);
2190}
Ming Lin576d55d2016-02-10 10:03:32 -08002191EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002192
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002193static void nvme_async_event_work(struct work_struct *work)
2194{
2195 struct nvme_ctrl *ctrl =
2196 container_of(work, struct nvme_ctrl, async_event_work);
2197
2198 spin_lock_irq(&ctrl->lock);
2199 while (ctrl->event_limit > 0) {
2200 int aer_idx = --ctrl->event_limit;
2201
2202 spin_unlock_irq(&ctrl->lock);
2203 ctrl->ops->submit_async_event(ctrl, aer_idx);
2204 spin_lock_irq(&ctrl->lock);
2205 }
2206 spin_unlock_irq(&ctrl->lock);
2207}
2208
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002209void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2210 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002211{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002212 u32 result = le32_to_cpu(res->u32);
2213 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002214
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002215 switch (le16_to_cpu(status) >> 1) {
2216 case NVME_SC_SUCCESS:
2217 done = false;
2218 /*FALLTHRU*/
2219 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002220 ++ctrl->event_limit;
2221 schedule_work(&ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002222 break;
2223 default:
2224 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002225 }
2226
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002227 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002228 return;
2229
2230 switch (result & 0xff07) {
2231 case NVME_AER_NOTICE_NS_CHANGED:
2232 dev_info(ctrl->device, "rescanning\n");
2233 nvme_queue_scan(ctrl);
2234 break;
2235 default:
2236 dev_warn(ctrl->device, "async event result %08x\n", result);
2237 }
2238}
2239EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2240
2241void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2242{
2243 ctrl->event_limit = NVME_NR_AERS;
2244 schedule_work(&ctrl->async_event_work);
2245}
2246EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2247
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002248static DEFINE_IDA(nvme_instance_ida);
2249
2250static int nvme_set_instance(struct nvme_ctrl *ctrl)
2251{
2252 int instance, error;
2253
2254 do {
2255 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2256 return -ENODEV;
2257
2258 spin_lock(&dev_list_lock);
2259 error = ida_get_new(&nvme_instance_ida, &instance);
2260 spin_unlock(&dev_list_lock);
2261 } while (error == -EAGAIN);
2262
2263 if (error)
2264 return -ENODEV;
2265
2266 ctrl->instance = instance;
2267 return 0;
2268}
2269
2270static void nvme_release_instance(struct nvme_ctrl *ctrl)
2271{
2272 spin_lock(&dev_list_lock);
2273 ida_remove(&nvme_instance_ida, ctrl->instance);
2274 spin_unlock(&dev_list_lock);
2275}
2276
Keith Busch53029b02015-11-28 15:41:02 +01002277void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002278{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002279 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002280 flush_work(&ctrl->scan_work);
2281 nvme_remove_namespaces(ctrl);
2282
Keith Busch53029b02015-11-28 15:41:02 +01002283 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002284
2285 spin_lock(&dev_list_lock);
2286 list_del(&ctrl->node);
2287 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002288}
Ming Lin576d55d2016-02-10 10:03:32 -08002289EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002290
2291static void nvme_free_ctrl(struct kref *kref)
2292{
2293 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002294
2295 put_device(ctrl->device);
2296 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002297 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002298
2299 ctrl->ops->free_ctrl(ctrl);
2300}
2301
2302void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2303{
2304 kref_put(&ctrl->kref, nvme_free_ctrl);
2305}
Ming Lin576d55d2016-02-10 10:03:32 -08002306EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002307
2308/*
2309 * Initialize a NVMe controller structures. This needs to be called during
2310 * earliest initialization so that we have the initialized structured around
2311 * during probing.
2312 */
2313int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2314 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2315{
2316 int ret;
2317
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002318 ctrl->state = NVME_CTRL_NEW;
2319 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002320 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002321 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002322 kref_init(&ctrl->kref);
2323 ctrl->dev = dev;
2324 ctrl->ops = ops;
2325 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002326 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002327 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002328
2329 ret = nvme_set_instance(ctrl);
2330 if (ret)
2331 goto out;
2332
Keith Busch779ff7562016-01-12 15:09:31 -07002333 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002334 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002335 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002336 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002337 if (IS_ERR(ctrl->device)) {
2338 ret = PTR_ERR(ctrl->device);
2339 goto out_release_instance;
2340 }
2341 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002342 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002343
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002344 spin_lock(&dev_list_lock);
2345 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2346 spin_unlock(&dev_list_lock);
2347
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002348 /*
2349 * Initialize latency tolerance controls. The sysfs files won't
2350 * be visible to userspace unless the device actually supports APST.
2351 */
2352 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2353 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2354 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2355
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002356 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002357out_release_instance:
2358 nvme_release_instance(ctrl);
2359out:
2360 return ret;
2361}
Ming Lin576d55d2016-02-10 10:03:32 -08002362EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002363
Keith Busch69d9a992016-02-24 09:15:56 -07002364/**
2365 * nvme_kill_queues(): Ends all namespace queues
2366 * @ctrl: the dead controller that needs to end
2367 *
2368 * Call this function when the driver determines it is unable to get the
2369 * controller in a state capable of servicing IO.
2370 */
2371void nvme_kill_queues(struct nvme_ctrl *ctrl)
2372{
2373 struct nvme_ns *ns;
2374
Keith Busch32f0c4a2016-07-13 11:45:02 -06002375 mutex_lock(&ctrl->namespaces_mutex);
2376 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002377 /*
2378 * Revalidating a dead namespace sets capacity to 0. This will
2379 * end buffered writers dirtying pages that can't be synced.
2380 */
Keith Buschf33447b2017-02-10 18:15:51 -05002381 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2382 continue;
2383 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002384 blk_set_queue_dying(ns->queue);
2385 blk_mq_abort_requeue_list(ns->queue);
2386 blk_mq_start_stopped_hw_queues(ns->queue, true);
Keith Busch69d9a992016-02-24 09:15:56 -07002387 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002388 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002389}
Linus Torvalds237045f2016-03-18 17:13:31 -07002390EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002391
Keith Busch302ad8c2017-03-01 14:22:12 -05002392void nvme_unfreeze(struct nvme_ctrl *ctrl)
2393{
2394 struct nvme_ns *ns;
2395
2396 mutex_lock(&ctrl->namespaces_mutex);
2397 list_for_each_entry(ns, &ctrl->namespaces, list)
2398 blk_mq_unfreeze_queue(ns->queue);
2399 mutex_unlock(&ctrl->namespaces_mutex);
2400}
2401EXPORT_SYMBOL_GPL(nvme_unfreeze);
2402
2403void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2404{
2405 struct nvme_ns *ns;
2406
2407 mutex_lock(&ctrl->namespaces_mutex);
2408 list_for_each_entry(ns, &ctrl->namespaces, list) {
2409 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2410 if (timeout <= 0)
2411 break;
2412 }
2413 mutex_unlock(&ctrl->namespaces_mutex);
2414}
2415EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2416
2417void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2418{
2419 struct nvme_ns *ns;
2420
2421 mutex_lock(&ctrl->namespaces_mutex);
2422 list_for_each_entry(ns, &ctrl->namespaces, list)
2423 blk_mq_freeze_queue_wait(ns->queue);
2424 mutex_unlock(&ctrl->namespaces_mutex);
2425}
2426EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2427
2428void nvme_start_freeze(struct nvme_ctrl *ctrl)
2429{
2430 struct nvme_ns *ns;
2431
2432 mutex_lock(&ctrl->namespaces_mutex);
2433 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002434 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002435 mutex_unlock(&ctrl->namespaces_mutex);
2436}
2437EXPORT_SYMBOL_GPL(nvme_start_freeze);
2438
Keith Busch25646262016-01-04 09:10:57 -07002439void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002440{
2441 struct nvme_ns *ns;
2442
Keith Busch32f0c4a2016-07-13 11:45:02 -06002443 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002444 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002445 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002446 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002447}
Ming Lin576d55d2016-02-10 10:03:32 -08002448EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002449
Keith Busch25646262016-01-04 09:10:57 -07002450void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002451{
2452 struct nvme_ns *ns;
2453
Keith Busch32f0c4a2016-07-13 11:45:02 -06002454 mutex_lock(&ctrl->namespaces_mutex);
2455 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002456 blk_mq_start_stopped_hw_queues(ns->queue, true);
2457 blk_mq_kick_requeue_list(ns->queue);
2458 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002459 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002460}
Ming Lin576d55d2016-02-10 10:03:32 -08002461EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002462
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002463int __init nvme_core_init(void)
2464{
2465 int result;
2466
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002467 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2468 &nvme_dev_fops);
2469 if (result < 0)
NeilBrownb09dcf52016-07-13 11:03:58 -07002470 return result;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002471 else if (result > 0)
2472 nvme_char_major = result;
2473
2474 nvme_class = class_create(THIS_MODULE, "nvme");
2475 if (IS_ERR(nvme_class)) {
2476 result = PTR_ERR(nvme_class);
2477 goto unregister_chrdev;
2478 }
2479
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002480 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002481
2482 unregister_chrdev:
2483 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002484 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002485}
2486
2487void nvme_core_exit(void)
2488{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002489 class_destroy(nvme_class);
2490 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002491}
Ming Lin576d55d2016-02-10 10:03:32 -08002492
2493MODULE_LICENSE("GPL");
2494MODULE_VERSION("1.0");
2495module_init(nvme_core_init);
2496module_exit(nvme_core_exit);