blob: 1b48514fbe99131c27f238970caad56ce716a9ac [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>
29#include <scsi/sg.h>
30#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Ming Linba0ba7d2016-02-10 10:03:30 -080037unsigned char admin_timeout = 60;
38module_param(admin_timeout, byte, 0644);
39MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
42unsigned char nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
44MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
47unsigned char shutdown_timeout = 5;
48module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Keith Buschf80ec962016-07-12 16:20:31 -070051unsigned int nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, uint, 0644);
53MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
54EXPORT_SYMBOL_GPL(nvme_max_retries);
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
59static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080060static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010061
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010062static struct class *nvme_class;
63
Ming Linc55a2fd2016-05-18 14:05:02 -070064void nvme_cancel_request(struct request *req, void *data, bool reserved)
65{
66 int status;
67
68 if (!blk_mq_request_started(req))
69 return;
70
71 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
72 "Cancelling I/O %d", req->tag);
73
74 status = NVME_SC_ABORT_REQ;
75 if (blk_queue_dying(req->q))
76 status |= NVME_SC_DNR;
77 blk_mq_complete_request(req, status);
78}
79EXPORT_SYMBOL_GPL(nvme_cancel_request);
80
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020081bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
82 enum nvme_ctrl_state new_state)
83{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -030084 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020085 bool changed = false;
86
87 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -030088
89 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020090 switch (new_state) {
91 case NVME_CTRL_LIVE:
92 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +020093 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020094 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +090095 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020096 changed = true;
97 /* FALLTHRU */
98 default:
99 break;
100 }
101 break;
102 case NVME_CTRL_RESETTING:
103 switch (old_state) {
104 case NVME_CTRL_NEW:
105 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900106 case NVME_CTRL_RECONNECTING:
107 changed = true;
108 /* FALLTHRU */
109 default:
110 break;
111 }
112 break;
113 case NVME_CTRL_RECONNECTING:
114 switch (old_state) {
115 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200116 changed = true;
117 /* FALLTHRU */
118 default:
119 break;
120 }
121 break;
122 case NVME_CTRL_DELETING:
123 switch (old_state) {
124 case NVME_CTRL_LIVE:
125 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900126 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200127 changed = true;
128 /* FALLTHRU */
129 default:
130 break;
131 }
132 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600133 case NVME_CTRL_DEAD:
134 switch (old_state) {
135 case NVME_CTRL_DELETING:
136 changed = true;
137 /* FALLTHRU */
138 default:
139 break;
140 }
141 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200142 default:
143 break;
144 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200145
146 if (changed)
147 ctrl->state = new_state;
148
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300149 spin_unlock_irq(&ctrl->lock);
150
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200151 return changed;
152}
153EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
154
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100155static void nvme_free_ns(struct kref *kref)
156{
157 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
158
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200159 if (ns->ndev)
160 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100161
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200162 if (ns->disk) {
163 spin_lock(&dev_list_lock);
164 ns->disk->private_data = NULL;
165 spin_unlock(&dev_list_lock);
166 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100167
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100168 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700169 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
170 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100171 kfree(ns);
172}
173
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100174static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100175{
176 kref_put(&ns->kref, nvme_free_ns);
177}
178
179static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
180{
181 struct nvme_ns *ns;
182
183 spin_lock(&dev_list_lock);
184 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800185 if (ns) {
186 if (!kref_get_unless_zero(&ns->kref))
187 goto fail;
188 if (!try_module_get(ns->ctrl->ops->module))
189 goto fail_put_ns;
190 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100191 spin_unlock(&dev_list_lock);
192
193 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800194
195fail_put_ns:
196 kref_put(&ns->kref, nvme_free_ns);
197fail:
198 spin_unlock(&dev_list_lock);
199 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100200}
201
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100202void nvme_requeue_req(struct request *req)
203{
Bart Van Asschea6eaa882016-10-28 17:23:40 -0700204 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100205}
Ming Lin576d55d2016-02-10 10:03:32 -0800206EXPORT_SYMBOL_GPL(nvme_requeue_req);
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100207
Christoph Hellwig41609822015-11-20 09:00:02 +0100208struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200209 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100210{
Christoph Hellwig21d34712015-11-26 09:08:36 +0100211 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100212
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200213 if (qid == NVME_QID_ANY) {
214 req = blk_mq_alloc_request(q, nvme_is_write(cmd), flags);
215 } else {
216 req = blk_mq_alloc_request_hctx(q, nvme_is_write(cmd), flags,
217 qid ? qid - 1 : 0);
218 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100219 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100220 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100221
222 req->cmd_type = REQ_TYPE_DRV_PRIV;
223 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800224 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100225
Christoph Hellwig41609822015-11-20 09:00:02 +0100226 return req;
227}
Ming Lin576d55d2016-02-10 10:03:32 -0800228EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100229
Ming Lin8093f7c2016-04-12 13:10:14 -0600230static inline void nvme_setup_flush(struct nvme_ns *ns,
231 struct nvme_command *cmnd)
232{
233 memset(cmnd, 0, sizeof(*cmnd));
234 cmnd->common.opcode = nvme_cmd_flush;
235 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
236}
237
238static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
239 struct nvme_command *cmnd)
240{
241 struct nvme_dsm_range *range;
242 struct page *page;
243 int offset;
244 unsigned int nr_bytes = blk_rq_bytes(req);
245
246 range = kmalloc(sizeof(*range), GFP_ATOMIC);
247 if (!range)
248 return BLK_MQ_RQ_QUEUE_BUSY;
249
250 range->cattr = cpu_to_le32(0);
251 range->nlb = cpu_to_le32(nr_bytes >> ns->lba_shift);
252 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
253
254 memset(cmnd, 0, sizeof(*cmnd));
255 cmnd->dsm.opcode = nvme_cmd_dsm;
256 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
257 cmnd->dsm.nr = 0;
258 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
259
260 req->completion_data = range;
261 page = virt_to_page(range);
262 offset = offset_in_page(range);
263 blk_add_request_payload(req, page, offset, sizeof(*range));
264
265 /*
266 * we set __data_len back to the size of the area to be discarded
267 * on disk. This allows us to report completion on the full amount
268 * of blocks described by the request.
269 */
270 req->__data_len = nr_bytes;
271
Omar Sandovalbac00002016-11-15 11:11:58 -0800272 return BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600273}
274
Chaitanya Kulkarni6d31e3ba2016-11-30 12:29:01 -0800275static inline void nvme_setup_write_zeroes(struct nvme_ns *ns,
276 struct request *req, struct nvme_command *cmnd)
277{
278 struct nvme_write_zeroes_cmd *write_zeroes = &cmnd->write_zeroes;
279
280 memset(cmnd, 0, sizeof(*cmnd));
281 write_zeroes->opcode = nvme_cmd_write_zeroes;
282 write_zeroes->nsid = cpu_to_le32(ns->ns_id);
283 write_zeroes->slba =
284 cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
285 write_zeroes->length =
286 cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
287 write_zeroes->control = 0;
288}
289
Ming Lin8093f7c2016-04-12 13:10:14 -0600290static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
291 struct nvme_command *cmnd)
292{
293 u16 control = 0;
294 u32 dsmgmt = 0;
295
296 if (req->cmd_flags & REQ_FUA)
297 control |= NVME_RW_FUA;
298 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
299 control |= NVME_RW_LR;
300
301 if (req->cmd_flags & REQ_RAHEAD)
302 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
303
304 memset(cmnd, 0, sizeof(*cmnd));
305 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600306 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
307 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
308 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
309
310 if (ns->ms) {
311 switch (ns->pi_type) {
312 case NVME_NS_DPS_PI_TYPE3:
313 control |= NVME_RW_PRINFO_PRCHK_GUARD;
314 break;
315 case NVME_NS_DPS_PI_TYPE1:
316 case NVME_NS_DPS_PI_TYPE2:
317 control |= NVME_RW_PRINFO_PRCHK_GUARD |
318 NVME_RW_PRINFO_PRCHK_REF;
319 cmnd->rw.reftag = cpu_to_le32(
320 nvme_block_nr(ns, blk_rq_pos(req)));
321 break;
322 }
323 if (!blk_integrity_rq(req))
324 control |= NVME_RW_PRINFO_PRACT;
325 }
326
327 cmnd->rw.control = cpu_to_le16(control);
328 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
329}
330
331int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
332 struct nvme_command *cmd)
333{
Omar Sandovalbac00002016-11-15 11:11:58 -0800334 int ret = BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600335
336 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800337 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Mike Christie3a5e02c2016-06-05 14:32:23 -0500338 else if (req_op(req) == REQ_OP_FLUSH)
Ming Lin8093f7c2016-04-12 13:10:14 -0600339 nvme_setup_flush(ns, cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -0500340 else if (req_op(req) == REQ_OP_DISCARD)
Ming Lin8093f7c2016-04-12 13:10:14 -0600341 ret = nvme_setup_discard(ns, req, cmd);
Chaitanya Kulkarni6d31e3ba2016-11-30 12:29:01 -0800342 else if (req_op(req) == REQ_OP_WRITE_ZEROES)
343 nvme_setup_write_zeroes(ns, req, cmd);
Ming Lin8093f7c2016-04-12 13:10:14 -0600344 else
345 nvme_setup_rw(ns, req, cmd);
346
James Smart721b3912016-10-21 23:33:34 +0300347 cmd->common.command_id = req->tag;
348
Ming Lin8093f7c2016-04-12 13:10:14 -0600349 return ret;
350}
351EXPORT_SYMBOL_GPL(nvme_setup_cmd);
352
Christoph Hellwig41609822015-11-20 09:00:02 +0100353/*
354 * Returns 0 on success. If the result is negative, it's a Linux error code;
355 * if the result is positive, it's an NVM Express status code
356 */
357int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800358 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200359 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100360{
361 struct request *req;
362 int ret;
363
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200364 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100365 if (IS_ERR(req))
366 return PTR_ERR(req);
367
368 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
369
Christoph Hellwig21d34712015-11-26 09:08:36 +0100370 if (buffer && bufflen) {
371 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
372 if (ret)
373 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100374 }
375
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200376 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800377 if (result)
378 *result = nvme_req(req)->result;
Christoph Hellwig41609822015-11-20 09:00:02 +0100379 ret = req->errors;
380 out:
381 blk_mq_free_request(req);
382 return ret;
383}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200384EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100385
386int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
387 void *buffer, unsigned bufflen)
388{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200389 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
390 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100391}
Ming Lin576d55d2016-02-10 10:03:32 -0800392EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100393
Keith Busch0b7f1f22015-10-23 09:47:28 -0600394int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
395 void __user *ubuffer, unsigned bufflen,
396 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
397 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100398{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200399 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600400 struct nvme_ns *ns = q->queuedata;
401 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100402 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600403 struct bio *bio = NULL;
404 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100405 int ret;
406
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200407 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
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
413 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100414 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
415 GFP_KERNEL);
416 if (ret)
417 goto out;
418 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100419
Keith Busch0b7f1f22015-10-23 09:47:28 -0600420 if (!disk)
421 goto submit;
422 bio->bi_bdev = bdget_disk(disk, 0);
423 if (!bio->bi_bdev) {
424 ret = -ENODEV;
425 goto out_unmap;
426 }
427
Keith Busche9fc63d2016-02-24 09:15:58 -0700428 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600429 struct bio_integrity_payload *bip;
430
431 meta = kmalloc(meta_len, GFP_KERNEL);
432 if (!meta) {
433 ret = -ENOMEM;
434 goto out_unmap;
435 }
436
437 if (write) {
438 if (copy_from_user(meta, meta_buffer,
439 meta_len)) {
440 ret = -EFAULT;
441 goto out_free_meta;
442 }
443 }
444
445 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700446 if (IS_ERR(bip)) {
447 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600448 goto out_free_meta;
449 }
450
451 bip->bip_iter.bi_size = meta_len;
452 bip->bip_iter.bi_sector = meta_seed;
453
454 ret = bio_integrity_add_page(bio, virt_to_page(meta),
455 meta_len, offset_in_page(meta));
456 if (ret != meta_len) {
457 ret = -ENOMEM;
458 goto out_free_meta;
459 }
460 }
461 }
462 submit:
463 blk_execute_rq(req->q, disk, req, 0);
464 ret = req->errors;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100465 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800466 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600467 if (meta && !ret && !write) {
468 if (copy_to_user(meta_buffer, meta, meta_len))
469 ret = -EFAULT;
470 }
471 out_free_meta:
472 kfree(meta);
473 out_unmap:
474 if (bio) {
475 if (disk && bio->bi_bdev)
476 bdput(bio->bi_bdev);
477 blk_rq_unmap_user(bio);
478 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100479 out:
480 blk_mq_free_request(req);
481 return ret;
482}
483
Keith Busch0b7f1f22015-10-23 09:47:28 -0600484int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
485 void __user *ubuffer, unsigned bufflen, u32 *result,
486 unsigned timeout)
487{
488 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
489 result, timeout);
490}
491
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200492static void nvme_keep_alive_end_io(struct request *rq, int error)
493{
494 struct nvme_ctrl *ctrl = rq->end_io_data;
495
496 blk_mq_free_request(rq);
497
498 if (error) {
499 dev_err(ctrl->device,
500 "failed nvme_keep_alive_end_io error=%d\n", error);
501 return;
502 }
503
504 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
505}
506
507static int nvme_keep_alive(struct nvme_ctrl *ctrl)
508{
509 struct nvme_command c;
510 struct request *rq;
511
512 memset(&c, 0, sizeof(c));
513 c.common.opcode = nvme_admin_keep_alive;
514
515 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
516 NVME_QID_ANY);
517 if (IS_ERR(rq))
518 return PTR_ERR(rq);
519
520 rq->timeout = ctrl->kato * HZ;
521 rq->end_io_data = ctrl;
522
523 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
524
525 return 0;
526}
527
528static void nvme_keep_alive_work(struct work_struct *work)
529{
530 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
531 struct nvme_ctrl, ka_work);
532
533 if (nvme_keep_alive(ctrl)) {
534 /* allocation failure, reset the controller */
535 dev_err(ctrl->device, "keep-alive failed\n");
536 ctrl->ops->reset_ctrl(ctrl);
537 return;
538 }
539}
540
541void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
542{
543 if (unlikely(ctrl->kato == 0))
544 return;
545
546 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
547 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
548}
549EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
550
551void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
552{
553 if (unlikely(ctrl->kato == 0))
554 return;
555
556 cancel_delayed_work_sync(&ctrl->ka_work);
557}
558EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
559
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100560int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100561{
562 struct nvme_command c = { };
563 int error;
564
565 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
566 c.identify.opcode = nvme_admin_identify;
567 c.identify.cns = cpu_to_le32(1);
568
569 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
570 if (!*id)
571 return -ENOMEM;
572
573 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
574 sizeof(struct nvme_id_ctrl));
575 if (error)
576 kfree(*id);
577 return error;
578}
579
Keith Busch540c8012015-10-22 15:45:06 -0600580static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
581{
582 struct nvme_command c = { };
583
584 c.identify.opcode = nvme_admin_identify;
585 c.identify.cns = cpu_to_le32(2);
586 c.identify.nsid = cpu_to_le32(nsid);
587 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
588}
589
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100590int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100591 struct nvme_id_ns **id)
592{
593 struct nvme_command c = { };
594 int error;
595
596 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
597 c.identify.opcode = nvme_admin_identify,
598 c.identify.nsid = cpu_to_le32(nsid),
599
600 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
601 if (!*id)
602 return -ENOMEM;
603
604 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
605 sizeof(struct nvme_id_ns));
606 if (error)
607 kfree(*id);
608 return error;
609}
610
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100611int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700612 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100613{
614 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800615 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100616 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100617
618 memset(&c, 0, sizeof(c));
619 c.features.opcode = nvme_admin_get_features;
620 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100621 c.features.fid = cpu_to_le32(fid);
622
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800623 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200624 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700625 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800626 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100627 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100628}
629
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100630int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700631 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100632{
633 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800634 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100635 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100636
637 memset(&c, 0, sizeof(c));
638 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100639 c.features.fid = cpu_to_le32(fid);
640 c.features.dword11 = cpu_to_le32(dword11);
641
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800642 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700643 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700644 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800645 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100646 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100647}
648
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100649int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100650{
651 struct nvme_command c = { };
652 int error;
653
654 c.common.opcode = nvme_admin_get_log_page,
655 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
656 c.common.cdw10[0] = cpu_to_le32(
657 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
658 NVME_LOG_SMART),
659
660 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
661 if (!*log)
662 return -ENOMEM;
663
664 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
665 sizeof(struct nvme_smart_log));
666 if (error)
667 kfree(*log);
668 return error;
669}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100670
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100671int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
672{
673 u32 q_count = (*count - 1) | ((*count - 1) << 16);
674 u32 result;
675 int status, nr_io_queues;
676
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700677 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100678 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200679 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100680 return status;
681
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200682 /*
683 * Degraded controllers might return an error when setting the queue
684 * count. We still want to be able to bring them online and offer
685 * access to the admin queue, as that might be only way to fix them up.
686 */
687 if (status > 0) {
688 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
689 *count = 0;
690 } else {
691 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
692 *count = min(*count, nr_io_queues);
693 }
694
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100695 return 0;
696}
Ming Lin576d55d2016-02-10 10:03:32 -0800697EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100698
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100699static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
700{
701 struct nvme_user_io io;
702 struct nvme_command c;
703 unsigned length, meta_len;
704 void __user *metadata;
705
706 if (copy_from_user(&io, uio, sizeof(io)))
707 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700708 if (io.flags)
709 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100710
711 switch (io.opcode) {
712 case nvme_cmd_write:
713 case nvme_cmd_read:
714 case nvme_cmd_compare:
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 length = (io.nblocks + 1) << ns->lba_shift;
721 meta_len = (io.nblocks + 1) * ns->ms;
722 metadata = (void __user *)(uintptr_t)io.metadata;
723
724 if (ns->ext) {
725 length += meta_len;
726 meta_len = 0;
727 } else if (meta_len) {
728 if ((io.metadata & 3) || !io.metadata)
729 return -EINVAL;
730 }
731
732 memset(&c, 0, sizeof(c));
733 c.rw.opcode = io.opcode;
734 c.rw.flags = io.flags;
735 c.rw.nsid = cpu_to_le32(ns->ns_id);
736 c.rw.slba = cpu_to_le64(io.slba);
737 c.rw.length = cpu_to_le16(io.nblocks);
738 c.rw.control = cpu_to_le16(io.control);
739 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
740 c.rw.reftag = cpu_to_le32(io.reftag);
741 c.rw.apptag = cpu_to_le16(io.apptag);
742 c.rw.appmask = cpu_to_le16(io.appmask);
743
744 return __nvme_submit_user_cmd(ns->queue, &c,
745 (void __user *)(uintptr_t)io.addr, length,
746 metadata, meta_len, io.slba, NULL, 0);
747}
748
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100749static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100750 struct nvme_passthru_cmd __user *ucmd)
751{
752 struct nvme_passthru_cmd cmd;
753 struct nvme_command c;
754 unsigned timeout = 0;
755 int status;
756
757 if (!capable(CAP_SYS_ADMIN))
758 return -EACCES;
759 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
760 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700761 if (cmd.flags)
762 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100763
764 memset(&c, 0, sizeof(c));
765 c.common.opcode = cmd.opcode;
766 c.common.flags = cmd.flags;
767 c.common.nsid = cpu_to_le32(cmd.nsid);
768 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
769 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
770 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
771 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
772 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
773 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
774 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
775 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
776
777 if (cmd.timeout_ms)
778 timeout = msecs_to_jiffies(cmd.timeout_ms);
779
780 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100781 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100782 &cmd.result, timeout);
783 if (status >= 0) {
784 if (put_user(cmd.result, &ucmd->result))
785 return -EFAULT;
786 }
787
788 return status;
789}
790
791static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
792 unsigned int cmd, unsigned long arg)
793{
794 struct nvme_ns *ns = bdev->bd_disk->private_data;
795
796 switch (cmd) {
797 case NVME_IOCTL_ID:
798 force_successful_syscall_return();
799 return ns->ns_id;
800 case NVME_IOCTL_ADMIN_CMD:
801 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
802 case NVME_IOCTL_IO_CMD:
803 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
804 case NVME_IOCTL_SUBMIT_IO:
805 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100806#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100807 case SG_GET_VERSION_NUM:
808 return nvme_sg_get_version_num((void __user *)arg);
809 case SG_IO:
810 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100811#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100812 default:
813 return -ENOTTY;
814 }
815}
816
817#ifdef CONFIG_COMPAT
818static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
819 unsigned int cmd, unsigned long arg)
820{
821 switch (cmd) {
822 case SG_IO:
823 return -ENOIOCTLCMD;
824 }
825 return nvme_ioctl(bdev, mode, cmd, arg);
826}
827#else
828#define nvme_compat_ioctl NULL
829#endif
830
831static int nvme_open(struct block_device *bdev, fmode_t mode)
832{
833 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
834}
835
836static void nvme_release(struct gendisk *disk, fmode_t mode)
837{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800838 struct nvme_ns *ns = disk->private_data;
839
840 module_put(ns->ctrl->ops->module);
841 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100842}
843
844static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
845{
846 /* some standard values */
847 geo->heads = 1 << 6;
848 geo->sectors = 1 << 5;
849 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
850 return 0;
851}
852
853#ifdef CONFIG_BLK_DEV_INTEGRITY
854static void nvme_init_integrity(struct nvme_ns *ns)
855{
856 struct blk_integrity integrity;
857
Jay Freyenseefa9a89f2016-07-20 21:26:16 -0600858 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100859 switch (ns->pi_type) {
860 case NVME_NS_DPS_PI_TYPE3:
861 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000862 integrity.tag_size = sizeof(u16) + sizeof(u32);
863 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100864 break;
865 case NVME_NS_DPS_PI_TYPE1:
866 case NVME_NS_DPS_PI_TYPE2:
867 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000868 integrity.tag_size = sizeof(u16);
869 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100870 break;
871 default:
872 integrity.profile = NULL;
873 break;
874 }
875 integrity.tuple_size = ns->ms;
876 blk_integrity_register(ns->disk, &integrity);
877 blk_queue_max_integrity_segments(ns->queue, 1);
878}
879#else
880static void nvme_init_integrity(struct nvme_ns *ns)
881{
882}
883#endif /* CONFIG_BLK_DEV_INTEGRITY */
884
885static void nvme_config_discard(struct nvme_ns *ns)
886{
Keith Busch08095e72016-03-04 13:15:17 -0700887 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100888 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700889
890 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
891 ns->queue->limits.discard_zeroes_data = 1;
892 else
893 ns->queue->limits.discard_zeroes_data = 0;
894
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100895 ns->queue->limits.discard_alignment = logical_block_size;
896 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800897 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100898 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
899}
900
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200901static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100902{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200903 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200904 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100905 return -ENODEV;
906 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200907
908 if ((*id)->ncap == 0) {
909 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100910 return -ENODEV;
911 }
912
Keith Busch2b9b6e82015-12-22 10:10:45 -0700913 if (ns->ctrl->vs >= NVME_VS(1, 1))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200914 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Keith Busch2b9b6e82015-12-22 10:10:45 -0700915 if (ns->ctrl->vs >= NVME_VS(1, 2))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200916 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
917
918 return 0;
919}
920
921static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
922{
923 struct nvme_ns *ns = disk->private_data;
924 u8 lbaf, pi_type;
925 u16 old_ms;
926 unsigned short bs;
Keith Busch2b9b6e82015-12-22 10:10:45 -0700927
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100928 old_ms = ns->ms;
929 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
930 ns->lba_shift = id->lbaf[lbaf].ds;
931 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
932 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
933
934 /*
935 * If identify namespace failed, use default 512 byte block size so
936 * block layer can use before failing read/write for 0 capacity.
937 */
938 if (ns->lba_shift == 0)
939 ns->lba_shift = 9;
940 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100941 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
942 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
943 id->dps & NVME_NS_DPS_PI_MASK : 0;
944
945 blk_mq_freeze_queue(disk->queue);
946 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
947 ns->ms != old_ms ||
948 bs != queue_logical_block_size(disk->queue) ||
949 (ns->ms && ns->ext)))
950 blk_integrity_unregister(disk);
951
952 ns->pi_type = pi_type;
953 blk_queue_logical_block_size(ns->queue, bs);
954
Keith Busch4b9d5b12015-11-20 09:13:30 +0100955 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100956 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100957 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
958 set_capacity(disk, 0);
959 else
960 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
961
962 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
963 nvme_config_discard(ns);
Chaitanya Kulkarni6d31e3ba2016-11-30 12:29:01 -0800964 if (ns->ctrl->oncs & NVME_CTRL_ONCS_WRITE_ZEROES)
965 blk_queue_max_write_zeroes_sectors(ns->queue,
966 ((u32)(USHRT_MAX + 1) * bs) >> 9);
967
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100968 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200969}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100970
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200971static int nvme_revalidate_disk(struct gendisk *disk)
972{
973 struct nvme_ns *ns = disk->private_data;
974 struct nvme_id_ns *id = NULL;
975 int ret;
976
977 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
978 set_capacity(disk, 0);
979 return -ENODEV;
980 }
981
982 ret = nvme_revalidate_ns(ns, &id);
983 if (ret)
984 return ret;
985
986 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100987 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200988
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100989 return 0;
990}
991
992static char nvme_pr_type(enum pr_type type)
993{
994 switch (type) {
995 case PR_WRITE_EXCLUSIVE:
996 return 1;
997 case PR_EXCLUSIVE_ACCESS:
998 return 2;
999 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1000 return 3;
1001 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1002 return 4;
1003 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1004 return 5;
1005 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1006 return 6;
1007 default:
1008 return 0;
1009 }
1010};
1011
1012static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1013 u64 key, u64 sa_key, u8 op)
1014{
1015 struct nvme_ns *ns = bdev->bd_disk->private_data;
1016 struct nvme_command c;
1017 u8 data[16] = { 0, };
1018
1019 put_unaligned_le64(key, &data[0]);
1020 put_unaligned_le64(sa_key, &data[8]);
1021
1022 memset(&c, 0, sizeof(c));
1023 c.common.opcode = op;
1024 c.common.nsid = cpu_to_le32(ns->ns_id);
1025 c.common.cdw10[0] = cpu_to_le32(cdw10);
1026
1027 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1028}
1029
1030static int nvme_pr_register(struct block_device *bdev, u64 old,
1031 u64 new, unsigned flags)
1032{
1033 u32 cdw10;
1034
1035 if (flags & ~PR_FL_IGNORE_KEY)
1036 return -EOPNOTSUPP;
1037
1038 cdw10 = old ? 2 : 0;
1039 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1040 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1041 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1042}
1043
1044static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1045 enum pr_type type, unsigned flags)
1046{
1047 u32 cdw10;
1048
1049 if (flags & ~PR_FL_IGNORE_KEY)
1050 return -EOPNOTSUPP;
1051
1052 cdw10 = nvme_pr_type(type) << 8;
1053 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1054 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1055}
1056
1057static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1058 enum pr_type type, bool abort)
1059{
1060 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1061 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1062}
1063
1064static int nvme_pr_clear(struct block_device *bdev, u64 key)
1065{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001066 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001067 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1068}
1069
1070static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1071{
1072 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1073 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1074}
1075
1076static const struct pr_ops nvme_pr_ops = {
1077 .pr_register = nvme_pr_register,
1078 .pr_reserve = nvme_pr_reserve,
1079 .pr_release = nvme_pr_release,
1080 .pr_preempt = nvme_pr_preempt,
1081 .pr_clear = nvme_pr_clear,
1082};
1083
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001084static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001085 .owner = THIS_MODULE,
1086 .ioctl = nvme_ioctl,
1087 .compat_ioctl = nvme_compat_ioctl,
1088 .open = nvme_open,
1089 .release = nvme_release,
1090 .getgeo = nvme_getgeo,
1091 .revalidate_disk= nvme_revalidate_disk,
1092 .pr_ops = &nvme_pr_ops,
1093};
1094
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001095static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1096{
1097 unsigned long timeout =
1098 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1099 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1100 int ret;
1101
1102 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1103 if ((csts & NVME_CSTS_RDY) == bit)
1104 break;
1105
1106 msleep(100);
1107 if (fatal_signal_pending(current))
1108 return -EINTR;
1109 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001110 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001111 "Device not ready; aborting %s\n", enabled ?
1112 "initialisation" : "reset");
1113 return -ENODEV;
1114 }
1115 }
1116
1117 return ret;
1118}
1119
1120/*
1121 * If the device has been passed off to us in an enabled state, just clear
1122 * the enabled bit. The spec says we should set the 'shutdown notification
1123 * bits', but doing so may cause the device to complete commands to the
1124 * admin queue ... and we don't know what memory that might be pointing at!
1125 */
1126int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1127{
1128 int ret;
1129
1130 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1131 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1132
1133 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1134 if (ret)
1135 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001136
1137 /* Checking for ctrl->tagset is a trick to avoid sleeping on module
1138 * load, since we only need the quirk on reset_controller. Notice
1139 * that the HGST device needs this delay only in firmware activation
1140 * procedure; unfortunately we have no (easy) way to verify this.
1141 */
1142 if ((ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) && ctrl->tagset)
1143 msleep(NVME_QUIRK_DELAY_AMOUNT);
1144
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001145 return nvme_wait_ready(ctrl, cap, false);
1146}
Ming Lin576d55d2016-02-10 10:03:32 -08001147EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001148
1149int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1150{
1151 /*
1152 * Default to a 4K page size, with the intention to update this
1153 * path in the future to accomodate architectures with differing
1154 * kernel and IO page sizes.
1155 */
1156 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1157 int ret;
1158
1159 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001160 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001161 "Minimum device page size %u too large for host (%u)\n",
1162 1 << dev_page_min, 1 << page_shift);
1163 return -ENODEV;
1164 }
1165
1166 ctrl->page_size = 1 << page_shift;
1167
1168 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1169 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1170 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1171 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1172 ctrl->ctrl_config |= NVME_CC_ENABLE;
1173
1174 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1175 if (ret)
1176 return ret;
1177 return nvme_wait_ready(ctrl, cap, true);
1178}
Ming Lin576d55d2016-02-10 10:03:32 -08001179EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001180
1181int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1182{
1183 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1184 u32 csts;
1185 int ret;
1186
1187 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1188 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1189
1190 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1191 if (ret)
1192 return ret;
1193
1194 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1195 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1196 break;
1197
1198 msleep(100);
1199 if (fatal_signal_pending(current))
1200 return -EINTR;
1201 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001202 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001203 "Device shutdown incomplete; abort shutdown\n");
1204 return -ENODEV;
1205 }
1206 }
1207
1208 return ret;
1209}
Ming Lin576d55d2016-02-10 10:03:32 -08001210EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001211
Christoph Hellwigda358252016-03-02 18:07:11 +01001212static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1213 struct request_queue *q)
1214{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001215 bool vwc = false;
1216
Christoph Hellwigda358252016-03-02 18:07:11 +01001217 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001218 u32 max_segments =
1219 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1220
Christoph Hellwigda358252016-03-02 18:07:11 +01001221 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001222 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001223 }
1224 if (ctrl->stripe_size)
1225 blk_queue_chunk_sectors(q, ctrl->stripe_size >> 9);
Christoph Hellwigda358252016-03-02 18:07:11 +01001226 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001227 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1228 vwc = true;
1229 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001230}
1231
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001232/*
1233 * Initialize the cached copies of the Identify data and various controller
1234 * register in our nvme_ctrl structure. This should be called as soon as
1235 * the admin queue is fully up and running.
1236 */
1237int nvme_init_identify(struct nvme_ctrl *ctrl)
1238{
1239 struct nvme_id_ctrl *id;
1240 u64 cap;
1241 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001242 u32 max_hw_sectors;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001243
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001244 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1245 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001246 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001247 return ret;
1248 }
1249
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001250 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1251 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001252 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001253 return ret;
1254 }
1255 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1256
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001257 if (ctrl->vs >= NVME_VS(1, 1))
1258 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1259
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001260 ret = nvme_identify_ctrl(ctrl, &id);
1261 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001262 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001263 return -EIO;
1264 }
1265
Keith Busch118472a2016-02-18 09:57:48 -07001266 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001267 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001268 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001269 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001270 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001271 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1272 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1273 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1274 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001275 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001276 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001277 max_hw_sectors = UINT_MAX;
1278 ctrl->max_hw_sectors =
1279 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001280
1281 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
1282 unsigned int max_hw_sectors;
1283
1284 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
1285 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
1286 if (ctrl->max_hw_sectors) {
1287 ctrl->max_hw_sectors = min(max_hw_sectors,
1288 ctrl->max_hw_sectors);
1289 } else {
1290 ctrl->max_hw_sectors = max_hw_sectors;
1291 }
1292 }
1293
Christoph Hellwigda358252016-03-02 18:07:11 +01001294 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001295 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001296 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001297
1298 if (ctrl->ops->is_fabrics) {
1299 ctrl->icdoff = le16_to_cpu(id->icdoff);
1300 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1301 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1302 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1303
1304 /*
1305 * In fabrics we need to verify the cntlid matches the
1306 * admin connect
1307 */
1308 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1309 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001310
1311 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1312 dev_err(ctrl->dev,
1313 "keep-alive support is mandatory for fabrics\n");
1314 ret = -EINVAL;
1315 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001316 } else {
1317 ctrl->cntlid = le16_to_cpu(id->cntlid);
1318 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001319
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001320 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001321 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001322}
Ming Lin576d55d2016-02-10 10:03:32 -08001323EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001324
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001325static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001326{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001327 struct nvme_ctrl *ctrl;
1328 int instance = iminor(inode);
1329 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001330
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001331 spin_lock(&dev_list_lock);
1332 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1333 if (ctrl->instance != instance)
1334 continue;
1335
1336 if (!ctrl->admin_q) {
1337 ret = -EWOULDBLOCK;
1338 break;
1339 }
1340 if (!kref_get_unless_zero(&ctrl->kref))
1341 break;
1342 file->private_data = ctrl;
1343 ret = 0;
1344 break;
1345 }
1346 spin_unlock(&dev_list_lock);
1347
1348 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001349}
1350
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001351static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001352{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001353 nvme_put_ctrl(file->private_data);
1354 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001355}
1356
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001357static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1358{
1359 struct nvme_ns *ns;
1360 int ret;
1361
1362 mutex_lock(&ctrl->namespaces_mutex);
1363 if (list_empty(&ctrl->namespaces)) {
1364 ret = -ENOTTY;
1365 goto out_unlock;
1366 }
1367
1368 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1369 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001370 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001371 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1372 ret = -EINVAL;
1373 goto out_unlock;
1374 }
1375
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001376 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001377 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1378 kref_get(&ns->kref);
1379 mutex_unlock(&ctrl->namespaces_mutex);
1380
1381 ret = nvme_user_cmd(ctrl, ns, argp);
1382 nvme_put_ns(ns);
1383 return ret;
1384
1385out_unlock:
1386 mutex_unlock(&ctrl->namespaces_mutex);
1387 return ret;
1388}
1389
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001390static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1391 unsigned long arg)
1392{
1393 struct nvme_ctrl *ctrl = file->private_data;
1394 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001395
1396 switch (cmd) {
1397 case NVME_IOCTL_ADMIN_CMD:
1398 return nvme_user_cmd(ctrl, NULL, argp);
1399 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001400 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001401 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001402 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001403 return ctrl->ops->reset_ctrl(ctrl);
1404 case NVME_IOCTL_SUBSYS_RESET:
1405 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001406 case NVME_IOCTL_RESCAN:
1407 nvme_queue_scan(ctrl);
1408 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001409 default:
1410 return -ENOTTY;
1411 }
1412}
1413
1414static const struct file_operations nvme_dev_fops = {
1415 .owner = THIS_MODULE,
1416 .open = nvme_dev_open,
1417 .release = nvme_dev_release,
1418 .unlocked_ioctl = nvme_dev_ioctl,
1419 .compat_ioctl = nvme_dev_ioctl,
1420};
1421
1422static ssize_t nvme_sysfs_reset(struct device *dev,
1423 struct device_attribute *attr, const char *buf,
1424 size_t count)
1425{
1426 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1427 int ret;
1428
1429 ret = ctrl->ops->reset_ctrl(ctrl);
1430 if (ret < 0)
1431 return ret;
1432 return count;
1433}
1434static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1435
Keith Busch9ec3bb22016-04-29 15:45:18 -06001436static ssize_t nvme_sysfs_rescan(struct device *dev,
1437 struct device_attribute *attr, const char *buf,
1438 size_t count)
1439{
1440 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1441
1442 nvme_queue_scan(ctrl);
1443 return count;
1444}
1445static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1446
Keith Busch118472a2016-02-18 09:57:48 -07001447static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1448 char *buf)
1449{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001450 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001451 struct nvme_ctrl *ctrl = ns->ctrl;
1452 int serial_len = sizeof(ctrl->serial);
1453 int model_len = sizeof(ctrl->model);
1454
1455 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1456 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1457
1458 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1459 return sprintf(buf, "eui.%8phN\n", ns->eui);
1460
1461 while (ctrl->serial[serial_len - 1] == ' ')
1462 serial_len--;
1463 while (ctrl->model[model_len - 1] == ' ')
1464 model_len--;
1465
1466 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1467 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1468}
1469static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1470
Keith Busch2b9b6e82015-12-22 10:10:45 -07001471static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1472 char *buf)
1473{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001474 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001475 return sprintf(buf, "%pU\n", ns->uuid);
1476}
1477static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1478
1479static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1480 char *buf)
1481{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001482 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001483 return sprintf(buf, "%8phd\n", ns->eui);
1484}
1485static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1486
1487static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1488 char *buf)
1489{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001490 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001491 return sprintf(buf, "%d\n", ns->ns_id);
1492}
1493static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1494
1495static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001496 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001497 &dev_attr_uuid.attr,
1498 &dev_attr_eui.attr,
1499 &dev_attr_nsid.attr,
1500 NULL,
1501};
1502
Ming Lin1a353d82016-06-13 16:45:24 +02001503static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001504 struct attribute *a, int n)
1505{
1506 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001507 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001508
1509 if (a == &dev_attr_uuid.attr) {
1510 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1511 return 0;
1512 }
1513 if (a == &dev_attr_eui.attr) {
1514 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1515 return 0;
1516 }
1517 return a->mode;
1518}
1519
1520static const struct attribute_group nvme_ns_attr_group = {
1521 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001522 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001523};
1524
Ming Lin931e1c22016-02-26 13:24:19 -08001525#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001526static ssize_t field##_show(struct device *dev, \
1527 struct device_attribute *attr, char *buf) \
1528{ \
1529 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1530 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1531} \
1532static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1533
Ming Lin931e1c22016-02-26 13:24:19 -08001534#define nvme_show_int_function(field) \
1535static ssize_t field##_show(struct device *dev, \
1536 struct device_attribute *attr, char *buf) \
1537{ \
1538 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1539 return sprintf(buf, "%d\n", ctrl->field); \
1540} \
1541static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1542
1543nvme_show_str_function(model);
1544nvme_show_str_function(serial);
1545nvme_show_str_function(firmware_rev);
1546nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001547
Ming Lin1a353d82016-06-13 16:45:24 +02001548static ssize_t nvme_sysfs_delete(struct device *dev,
1549 struct device_attribute *attr, const char *buf,
1550 size_t count)
1551{
1552 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1553
1554 if (device_remove_file_self(dev, attr))
1555 ctrl->ops->delete_ctrl(ctrl);
1556 return count;
1557}
1558static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1559
1560static ssize_t nvme_sysfs_show_transport(struct device *dev,
1561 struct device_attribute *attr,
1562 char *buf)
1563{
1564 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1565
1566 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1567}
1568static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1569
1570static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1571 struct device_attribute *attr,
1572 char *buf)
1573{
1574 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1575
1576 return snprintf(buf, PAGE_SIZE, "%s\n",
1577 ctrl->ops->get_subsysnqn(ctrl));
1578}
1579static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1580
1581static ssize_t nvme_sysfs_show_address(struct device *dev,
1582 struct device_attribute *attr,
1583 char *buf)
1584{
1585 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1586
1587 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1588}
1589static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1590
Keith Busch779ff7562016-01-12 15:09:31 -07001591static struct attribute *nvme_dev_attrs[] = {
1592 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001593 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001594 &dev_attr_model.attr,
1595 &dev_attr_serial.attr,
1596 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001597 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02001598 &dev_attr_delete_controller.attr,
1599 &dev_attr_transport.attr,
1600 &dev_attr_subsysnqn.attr,
1601 &dev_attr_address.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001602 NULL
1603};
1604
Ming Lin1a353d82016-06-13 16:45:24 +02001605#define CHECK_ATTR(ctrl, a, name) \
1606 if ((a) == &dev_attr_##name.attr && \
1607 !(ctrl)->ops->get_##name) \
1608 return 0
1609
1610static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1611 struct attribute *a, int n)
1612{
1613 struct device *dev = container_of(kobj, struct device, kobj);
1614 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1615
1616 if (a == &dev_attr_delete_controller.attr) {
1617 if (!ctrl->ops->delete_ctrl)
1618 return 0;
1619 }
1620
1621 CHECK_ATTR(ctrl, a, subsysnqn);
1622 CHECK_ATTR(ctrl, a, address);
1623
1624 return a->mode;
1625}
1626
Keith Busch779ff7562016-01-12 15:09:31 -07001627static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02001628 .attrs = nvme_dev_attrs,
1629 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07001630};
1631
1632static const struct attribute_group *nvme_dev_attr_groups[] = {
1633 &nvme_dev_attrs_group,
1634 NULL,
1635};
1636
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001637static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1638{
1639 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1640 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1641
1642 return nsa->ns_id - nsb->ns_id;
1643}
1644
Keith Busch32f0c4a2016-07-13 11:45:02 -06001645static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001646{
Keith Busch32f0c4a2016-07-13 11:45:02 -06001647 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001648
Keith Busch32f0c4a2016-07-13 11:45:02 -06001649 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001650 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001651 if (ns->ns_id == nsid) {
1652 kref_get(&ns->kref);
1653 ret = ns;
1654 break;
1655 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001656 if (ns->ns_id > nsid)
1657 break;
1658 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001659 mutex_unlock(&ctrl->namespaces_mutex);
1660 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001661}
1662
1663static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1664{
1665 struct nvme_ns *ns;
1666 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001667 struct nvme_id_ns *id;
1668 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001669 int node = dev_to_node(ctrl->dev);
1670
1671 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1672 if (!ns)
1673 return;
1674
Keith Busch075790e2016-02-24 09:15:53 -07001675 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1676 if (ns->instance < 0)
1677 goto out_free_ns;
1678
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001679 ns->queue = blk_mq_init_queue(ctrl->tagset);
1680 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07001681 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001682 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1683 ns->queue->queuedata = ns;
1684 ns->ctrl = ctrl;
1685
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001686 kref_init(&ns->kref);
1687 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001688 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001689
1690 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01001691 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001692
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001693 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001694
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001695 if (nvme_revalidate_ns(ns, &id))
1696 goto out_free_queue;
1697
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001698 if (nvme_nvm_ns_supported(ns, id) &&
1699 nvme_nvm_register(ns, disk_name, node)) {
1700 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
1701 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001702 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001703
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001704 disk = alloc_disk_node(0, node);
1705 if (!disk)
1706 goto out_free_id;
1707
1708 disk->fops = &nvme_fops;
1709 disk->private_data = ns;
1710 disk->queue = ns->queue;
1711 disk->flags = GENHD_FL_EXT_DEVT;
1712 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
1713 ns->disk = disk;
1714
1715 __nvme_revalidate_disk(disk, id);
1716
Keith Busch32f0c4a2016-07-13 11:45:02 -06001717 mutex_lock(&ctrl->namespaces_mutex);
1718 list_add_tail(&ns->list, &ctrl->namespaces);
1719 mutex_unlock(&ctrl->namespaces_mutex);
1720
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001721 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001722
1723 kfree(id);
1724
Dan Williams0d52c7562016-06-15 19:44:20 -07001725 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001726 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1727 &nvme_ns_attr_group))
1728 pr_warn("%s: failed to create sysfs group for identification\n",
1729 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001730 if (ns->ndev && nvme_nvm_register_sysfs(ns))
1731 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
1732 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001733 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001734 out_free_id:
1735 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001736 out_free_queue:
1737 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07001738 out_release_instance:
1739 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001740 out_free_ns:
1741 kfree(ns);
1742}
1743
1744static void nvme_ns_remove(struct nvme_ns *ns)
1745{
Keith Busch646017a2016-02-24 09:15:54 -07001746 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
1747 return;
1748
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001749 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001750 if (blk_get_integrity(ns->disk))
1751 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001752 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1753 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001754 if (ns->ndev)
1755 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001756 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001757 blk_mq_abort_requeue_list(ns->queue);
1758 blk_cleanup_queue(ns->queue);
1759 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001760
1761 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001762 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001763 mutex_unlock(&ns->ctrl->namespaces_mutex);
1764
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001765 nvme_put_ns(ns);
1766}
1767
Keith Busch540c8012015-10-22 15:45:06 -06001768static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1769{
1770 struct nvme_ns *ns;
1771
Keith Busch32f0c4a2016-07-13 11:45:02 -06001772 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06001773 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001774 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06001775 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001776 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06001777 } else
1778 nvme_alloc_ns(ctrl, nsid);
1779}
1780
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301781static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
1782 unsigned nsid)
1783{
1784 struct nvme_ns *ns, *next;
1785
1786 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1787 if (ns->ns_id > nsid)
1788 nvme_ns_remove(ns);
1789 }
1790}
1791
Keith Busch540c8012015-10-22 15:45:06 -06001792static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1793{
1794 struct nvme_ns *ns;
1795 __le32 *ns_list;
1796 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1797 int ret = 0;
1798
1799 ns_list = kzalloc(0x1000, GFP_KERNEL);
1800 if (!ns_list)
1801 return -ENOMEM;
1802
1803 for (i = 0; i < num_lists; i++) {
1804 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1805 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301806 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06001807
1808 for (j = 0; j < min(nn, 1024U); j++) {
1809 nsid = le32_to_cpu(ns_list[j]);
1810 if (!nsid)
1811 goto out;
1812
1813 nvme_validate_ns(ctrl, nsid);
1814
1815 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001816 ns = nvme_find_get_ns(ctrl, prev);
1817 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06001818 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001819 nvme_put_ns(ns);
1820 }
Keith Busch540c8012015-10-22 15:45:06 -06001821 }
1822 }
1823 nn -= j;
1824 }
1825 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301826 nvme_remove_invalid_namespaces(ctrl, prev);
1827 free:
Keith Busch540c8012015-10-22 15:45:06 -06001828 kfree(ns_list);
1829 return ret;
1830}
1831
Christoph Hellwig5955be22016-04-26 13:51:59 +02001832static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001833{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001834 unsigned i;
1835
Keith Busch540c8012015-10-22 15:45:06 -06001836 for (i = 1; i <= nn; i++)
1837 nvme_validate_ns(ctrl, i);
1838
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301839 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001840}
1841
Christoph Hellwig5955be22016-04-26 13:51:59 +02001842static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001843{
Christoph Hellwig5955be22016-04-26 13:51:59 +02001844 struct nvme_ctrl *ctrl =
1845 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001846 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06001847 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001848
Christoph Hellwig5955be22016-04-26 13:51:59 +02001849 if (ctrl->state != NVME_CTRL_LIVE)
1850 return;
1851
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001852 if (nvme_identify_ctrl(ctrl, &id))
1853 return;
Keith Busch540c8012015-10-22 15:45:06 -06001854
1855 nn = le32_to_cpu(id->nn);
1856 if (ctrl->vs >= NVME_VS(1, 1) &&
1857 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1858 if (!nvme_scan_ns_list(ctrl, nn))
1859 goto done;
1860 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02001861 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06001862 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06001863 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06001864 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001865 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001866 kfree(id);
1867}
Christoph Hellwig5955be22016-04-26 13:51:59 +02001868
1869void nvme_queue_scan(struct nvme_ctrl *ctrl)
1870{
1871 /*
1872 * Do not queue new scan work when a controller is reset during
1873 * removal.
1874 */
1875 if (ctrl->state == NVME_CTRL_LIVE)
1876 schedule_work(&ctrl->scan_work);
1877}
1878EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001879
Keith Busch32f0c4a2016-07-13 11:45:02 -06001880/*
1881 * This function iterates the namespace list unlocked to allow recovery from
1882 * controller failure. It is up to the caller to ensure the namespace list is
1883 * not modified by scan work while this function is executing.
1884 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001885void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1886{
1887 struct nvme_ns *ns, *next;
1888
Keith Busch0ff9d4e2016-05-12 08:37:14 -06001889 /*
1890 * The dead states indicates the controller was not gracefully
1891 * disconnected. In that case, we won't be able to flush any data while
1892 * removing the namespaces' disks; fail all the queues now to avoid
1893 * potentially having to clean up the failed sync later.
1894 */
1895 if (ctrl->state == NVME_CTRL_DEAD)
1896 nvme_kill_queues(ctrl);
1897
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001898 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1899 nvme_ns_remove(ns);
1900}
Ming Lin576d55d2016-02-10 10:03:32 -08001901EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001902
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001903static void nvme_async_event_work(struct work_struct *work)
1904{
1905 struct nvme_ctrl *ctrl =
1906 container_of(work, struct nvme_ctrl, async_event_work);
1907
1908 spin_lock_irq(&ctrl->lock);
1909 while (ctrl->event_limit > 0) {
1910 int aer_idx = --ctrl->event_limit;
1911
1912 spin_unlock_irq(&ctrl->lock);
1913 ctrl->ops->submit_async_event(ctrl, aer_idx);
1914 spin_lock_irq(&ctrl->lock);
1915 }
1916 spin_unlock_irq(&ctrl->lock);
1917}
1918
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001919void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
1920 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001921{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001922 u32 result = le32_to_cpu(res->u32);
1923 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001924
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001925 switch (le16_to_cpu(status) >> 1) {
1926 case NVME_SC_SUCCESS:
1927 done = false;
1928 /*FALLTHRU*/
1929 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001930 ++ctrl->event_limit;
1931 schedule_work(&ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001932 break;
1933 default:
1934 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001935 }
1936
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001937 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001938 return;
1939
1940 switch (result & 0xff07) {
1941 case NVME_AER_NOTICE_NS_CHANGED:
1942 dev_info(ctrl->device, "rescanning\n");
1943 nvme_queue_scan(ctrl);
1944 break;
1945 default:
1946 dev_warn(ctrl->device, "async event result %08x\n", result);
1947 }
1948}
1949EXPORT_SYMBOL_GPL(nvme_complete_async_event);
1950
1951void nvme_queue_async_events(struct nvme_ctrl *ctrl)
1952{
1953 ctrl->event_limit = NVME_NR_AERS;
1954 schedule_work(&ctrl->async_event_work);
1955}
1956EXPORT_SYMBOL_GPL(nvme_queue_async_events);
1957
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001958static DEFINE_IDA(nvme_instance_ida);
1959
1960static int nvme_set_instance(struct nvme_ctrl *ctrl)
1961{
1962 int instance, error;
1963
1964 do {
1965 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1966 return -ENODEV;
1967
1968 spin_lock(&dev_list_lock);
1969 error = ida_get_new(&nvme_instance_ida, &instance);
1970 spin_unlock(&dev_list_lock);
1971 } while (error == -EAGAIN);
1972
1973 if (error)
1974 return -ENODEV;
1975
1976 ctrl->instance = instance;
1977 return 0;
1978}
1979
1980static void nvme_release_instance(struct nvme_ctrl *ctrl)
1981{
1982 spin_lock(&dev_list_lock);
1983 ida_remove(&nvme_instance_ida, ctrl->instance);
1984 spin_unlock(&dev_list_lock);
1985}
1986
Keith Busch53029b02015-11-28 15:41:02 +01001987void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08001988{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001989 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02001990 flush_work(&ctrl->scan_work);
1991 nvme_remove_namespaces(ctrl);
1992
Keith Busch53029b02015-11-28 15:41:02 +01001993 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001994
1995 spin_lock(&dev_list_lock);
1996 list_del(&ctrl->node);
1997 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01001998}
Ming Lin576d55d2016-02-10 10:03:32 -08001999EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002000
2001static void nvme_free_ctrl(struct kref *kref)
2002{
2003 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002004
2005 put_device(ctrl->device);
2006 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002007 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002008
2009 ctrl->ops->free_ctrl(ctrl);
2010}
2011
2012void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2013{
2014 kref_put(&ctrl->kref, nvme_free_ctrl);
2015}
Ming Lin576d55d2016-02-10 10:03:32 -08002016EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002017
2018/*
2019 * Initialize a NVMe controller structures. This needs to be called during
2020 * earliest initialization so that we have the initialized structured around
2021 * during probing.
2022 */
2023int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2024 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2025{
2026 int ret;
2027
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002028 ctrl->state = NVME_CTRL_NEW;
2029 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002030 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002031 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002032 kref_init(&ctrl->kref);
2033 ctrl->dev = dev;
2034 ctrl->ops = ops;
2035 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002036 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002037 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002038
2039 ret = nvme_set_instance(ctrl);
2040 if (ret)
2041 goto out;
2042
Keith Busch779ff7562016-01-12 15:09:31 -07002043 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002044 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002045 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002046 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002047 if (IS_ERR(ctrl->device)) {
2048 ret = PTR_ERR(ctrl->device);
2049 goto out_release_instance;
2050 }
2051 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002052 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002053
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002054 spin_lock(&dev_list_lock);
2055 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2056 spin_unlock(&dev_list_lock);
2057
2058 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002059out_release_instance:
2060 nvme_release_instance(ctrl);
2061out:
2062 return ret;
2063}
Ming Lin576d55d2016-02-10 10:03:32 -08002064EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002065
Keith Busch69d9a992016-02-24 09:15:56 -07002066/**
2067 * nvme_kill_queues(): Ends all namespace queues
2068 * @ctrl: the dead controller that needs to end
2069 *
2070 * Call this function when the driver determines it is unable to get the
2071 * controller in a state capable of servicing IO.
2072 */
2073void nvme_kill_queues(struct nvme_ctrl *ctrl)
2074{
2075 struct nvme_ns *ns;
2076
Keith Busch32f0c4a2016-07-13 11:45:02 -06002077 mutex_lock(&ctrl->namespaces_mutex);
2078 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002079 /*
2080 * Revalidating a dead namespace sets capacity to 0. This will
2081 * end buffered writers dirtying pages that can't be synced.
2082 */
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002083 if (ns->disk && !test_and_set_bit(NVME_NS_DEAD, &ns->flags))
Keith Busch69d9a992016-02-24 09:15:56 -07002084 revalidate_disk(ns->disk);
2085
2086 blk_set_queue_dying(ns->queue);
2087 blk_mq_abort_requeue_list(ns->queue);
2088 blk_mq_start_stopped_hw_queues(ns->queue, true);
Keith Busch69d9a992016-02-24 09:15:56 -07002089 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002090 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002091}
Linus Torvalds237045f2016-03-18 17:13:31 -07002092EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002093
Keith Busch25646262016-01-04 09:10:57 -07002094void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002095{
2096 struct nvme_ns *ns;
2097
Keith Busch32f0c4a2016-07-13 11:45:02 -06002098 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002099 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002100 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002101 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002102}
Ming Lin576d55d2016-02-10 10:03:32 -08002103EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002104
Keith Busch25646262016-01-04 09:10:57 -07002105void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002106{
2107 struct nvme_ns *ns;
2108
Keith Busch32f0c4a2016-07-13 11:45:02 -06002109 mutex_lock(&ctrl->namespaces_mutex);
2110 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002111 blk_mq_start_stopped_hw_queues(ns->queue, true);
2112 blk_mq_kick_requeue_list(ns->queue);
2113 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002114 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002115}
Ming Lin576d55d2016-02-10 10:03:32 -08002116EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002117
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002118int __init nvme_core_init(void)
2119{
2120 int result;
2121
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002122 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2123 &nvme_dev_fops);
2124 if (result < 0)
NeilBrownb09dcf52016-07-13 11:03:58 -07002125 return result;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002126 else if (result > 0)
2127 nvme_char_major = result;
2128
2129 nvme_class = class_create(THIS_MODULE, "nvme");
2130 if (IS_ERR(nvme_class)) {
2131 result = PTR_ERR(nvme_class);
2132 goto unregister_chrdev;
2133 }
2134
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002135 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002136
2137 unregister_chrdev:
2138 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002139 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002140}
2141
2142void nvme_core_exit(void)
2143{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002144 class_destroy(nvme_class);
2145 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002146}
Ming Lin576d55d2016-02-10 10:03:32 -08002147
2148MODULE_LICENSE("GPL");
2149MODULE_VERSION("1.0");
2150module_init(nvme_core_init);
2151module_exit(nvme_core_exit);