2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
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.
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
15 #include <linux/blkdev.h>
16 #include <linux/blk-mq.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/hdreg.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list_sort.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <linux/ptrace.h>
27 #include <linux/nvme_ioctl.h>
28 #include <linux/t10-pi.h>
30 #include <asm/unaligned.h>
34 #define NVME_MINORS (1U << MINORBITS)
36 static int nvme_major;
37 module_param(nvme_major, int, 0);
39 static int nvme_char_major;
40 module_param(nvme_char_major, int, 0);
42 static LIST_HEAD(nvme_ctrl_list);
43 DEFINE_SPINLOCK(dev_list_lock);
45 static struct class *nvme_class;
47 static void nvme_free_ns(struct kref *kref)
49 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
51 if (ns->type == NVME_NS_LIGHTNVM)
52 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
54 spin_lock(&dev_list_lock);
55 ns->disk->private_data = NULL;
56 spin_unlock(&dev_list_lock);
59 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
60 nvme_put_ctrl(ns->ctrl);
64 static void nvme_put_ns(struct nvme_ns *ns)
66 kref_put(&ns->kref, nvme_free_ns);
69 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
73 spin_lock(&dev_list_lock);
74 ns = disk->private_data;
75 if (ns && !kref_get_unless_zero(&ns->kref))
77 spin_unlock(&dev_list_lock);
82 void nvme_requeue_req(struct request *req)
86 blk_mq_requeue_request(req);
87 spin_lock_irqsave(req->q->queue_lock, flags);
88 if (!blk_queue_stopped(req->q))
89 blk_mq_kick_requeue_list(req->q);
90 spin_unlock_irqrestore(req->q->queue_lock, flags);
93 struct request *nvme_alloc_request(struct request_queue *q,
94 struct nvme_command *cmd, unsigned int flags)
96 bool write = cmd->common.opcode & 1;
99 req = blk_mq_alloc_request(q, write, flags);
103 req->cmd_type = REQ_TYPE_DRV_PRIV;
104 req->cmd_flags |= REQ_FAILFAST_DRIVER;
106 req->__sector = (sector_t) -1;
107 req->bio = req->biotail = NULL;
109 req->cmd = (unsigned char *)cmd;
110 req->cmd_len = sizeof(struct nvme_command);
111 req->special = (void *)0;
117 * Returns 0 on success. If the result is negative, it's a Linux error code;
118 * if the result is positive, it's an NVM Express status code
120 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
121 void *buffer, unsigned bufflen, u32 *result, unsigned timeout)
126 req = nvme_alloc_request(q, cmd, 0);
130 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
132 if (buffer && bufflen) {
133 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
138 blk_execute_rq(req->q, NULL, req, 0);
140 *result = (u32)(uintptr_t)req->special;
143 blk_mq_free_request(req);
147 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
148 void *buffer, unsigned bufflen)
150 return __nvme_submit_sync_cmd(q, cmd, buffer, bufflen, NULL, 0);
153 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
154 void __user *ubuffer, unsigned bufflen,
155 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
156 u32 *result, unsigned timeout)
158 bool write = cmd->common.opcode & 1;
159 struct nvme_ns *ns = q->queuedata;
160 struct gendisk *disk = ns ? ns->disk : NULL;
162 struct bio *bio = NULL;
166 req = nvme_alloc_request(q, cmd, 0);
170 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
172 if (ubuffer && bufflen) {
173 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
181 bio->bi_bdev = bdget_disk(disk, 0);
188 struct bio_integrity_payload *bip;
190 meta = kmalloc(meta_len, GFP_KERNEL);
197 if (copy_from_user(meta, meta_buffer,
204 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
210 bip->bip_iter.bi_size = meta_len;
211 bip->bip_iter.bi_sector = meta_seed;
213 ret = bio_integrity_add_page(bio, virt_to_page(meta),
214 meta_len, offset_in_page(meta));
215 if (ret != meta_len) {
222 blk_execute_rq(req->q, disk, req, 0);
225 *result = (u32)(uintptr_t)req->special;
226 if (meta && !ret && !write) {
227 if (copy_to_user(meta_buffer, meta, meta_len))
234 if (disk && bio->bi_bdev)
236 blk_rq_unmap_user(bio);
239 blk_mq_free_request(req);
243 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
244 void __user *ubuffer, unsigned bufflen, u32 *result,
247 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
251 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
253 struct nvme_command c = { };
256 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
257 c.identify.opcode = nvme_admin_identify;
258 c.identify.cns = cpu_to_le32(1);
260 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
264 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
265 sizeof(struct nvme_id_ctrl));
271 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
273 struct nvme_command c = { };
275 c.identify.opcode = nvme_admin_identify;
276 c.identify.cns = cpu_to_le32(2);
277 c.identify.nsid = cpu_to_le32(nsid);
278 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
281 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
282 struct nvme_id_ns **id)
284 struct nvme_command c = { };
287 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
288 c.identify.opcode = nvme_admin_identify,
289 c.identify.nsid = cpu_to_le32(nsid),
291 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
295 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
296 sizeof(struct nvme_id_ns));
302 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
303 dma_addr_t dma_addr, u32 *result)
305 struct nvme_command c;
307 memset(&c, 0, sizeof(c));
308 c.features.opcode = nvme_admin_get_features;
309 c.features.nsid = cpu_to_le32(nsid);
310 c.features.prp1 = cpu_to_le64(dma_addr);
311 c.features.fid = cpu_to_le32(fid);
313 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
316 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
317 dma_addr_t dma_addr, u32 *result)
319 struct nvme_command c;
321 memset(&c, 0, sizeof(c));
322 c.features.opcode = nvme_admin_set_features;
323 c.features.prp1 = cpu_to_le64(dma_addr);
324 c.features.fid = cpu_to_le32(fid);
325 c.features.dword11 = cpu_to_le32(dword11);
327 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
330 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
332 struct nvme_command c = { };
335 c.common.opcode = nvme_admin_get_log_page,
336 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
337 c.common.cdw10[0] = cpu_to_le32(
338 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
341 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
345 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
346 sizeof(struct nvme_smart_log));
352 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
354 u32 q_count = (*count - 1) | ((*count - 1) << 16);
356 int status, nr_io_queues;
358 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
363 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
364 *count = min(*count, nr_io_queues);
368 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
370 struct nvme_user_io io;
371 struct nvme_command c;
372 unsigned length, meta_len;
373 void __user *metadata;
375 if (copy_from_user(&io, uio, sizeof(io)))
383 case nvme_cmd_compare:
389 length = (io.nblocks + 1) << ns->lba_shift;
390 meta_len = (io.nblocks + 1) * ns->ms;
391 metadata = (void __user *)(uintptr_t)io.metadata;
396 } else if (meta_len) {
397 if ((io.metadata & 3) || !io.metadata)
401 memset(&c, 0, sizeof(c));
402 c.rw.opcode = io.opcode;
403 c.rw.flags = io.flags;
404 c.rw.nsid = cpu_to_le32(ns->ns_id);
405 c.rw.slba = cpu_to_le64(io.slba);
406 c.rw.length = cpu_to_le16(io.nblocks);
407 c.rw.control = cpu_to_le16(io.control);
408 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
409 c.rw.reftag = cpu_to_le32(io.reftag);
410 c.rw.apptag = cpu_to_le16(io.apptag);
411 c.rw.appmask = cpu_to_le16(io.appmask);
413 return __nvme_submit_user_cmd(ns->queue, &c,
414 (void __user *)(uintptr_t)io.addr, length,
415 metadata, meta_len, io.slba, NULL, 0);
418 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
419 struct nvme_passthru_cmd __user *ucmd)
421 struct nvme_passthru_cmd cmd;
422 struct nvme_command c;
423 unsigned timeout = 0;
426 if (!capable(CAP_SYS_ADMIN))
428 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
433 memset(&c, 0, sizeof(c));
434 c.common.opcode = cmd.opcode;
435 c.common.flags = cmd.flags;
436 c.common.nsid = cpu_to_le32(cmd.nsid);
437 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
438 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
439 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
440 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
441 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
442 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
443 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
444 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
447 timeout = msecs_to_jiffies(cmd.timeout_ms);
449 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
450 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
451 &cmd.result, timeout);
453 if (put_user(cmd.result, &ucmd->result))
460 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
461 unsigned int cmd, unsigned long arg)
463 struct nvme_ns *ns = bdev->bd_disk->private_data;
467 force_successful_syscall_return();
469 case NVME_IOCTL_ADMIN_CMD:
470 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
471 case NVME_IOCTL_IO_CMD:
472 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
473 case NVME_IOCTL_SUBMIT_IO:
474 return nvme_submit_io(ns, (void __user *)arg);
475 #ifdef CONFIG_BLK_DEV_NVME_SCSI
476 case SG_GET_VERSION_NUM:
477 return nvme_sg_get_version_num((void __user *)arg);
479 return nvme_sg_io(ns, (void __user *)arg);
487 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
488 unsigned int cmd, unsigned long arg)
494 return nvme_ioctl(bdev, mode, cmd, arg);
497 #define nvme_compat_ioctl NULL
500 static int nvme_open(struct block_device *bdev, fmode_t mode)
502 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
505 static void nvme_release(struct gendisk *disk, fmode_t mode)
507 nvme_put_ns(disk->private_data);
510 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
512 /* some standard values */
514 geo->sectors = 1 << 5;
515 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
519 #ifdef CONFIG_BLK_DEV_INTEGRITY
520 static void nvme_init_integrity(struct nvme_ns *ns)
522 struct blk_integrity integrity;
524 switch (ns->pi_type) {
525 case NVME_NS_DPS_PI_TYPE3:
526 integrity.profile = &t10_pi_type3_crc;
528 case NVME_NS_DPS_PI_TYPE1:
529 case NVME_NS_DPS_PI_TYPE2:
530 integrity.profile = &t10_pi_type1_crc;
533 integrity.profile = NULL;
536 integrity.tuple_size = ns->ms;
537 blk_integrity_register(ns->disk, &integrity);
538 blk_queue_max_integrity_segments(ns->queue, 1);
541 static void nvme_init_integrity(struct nvme_ns *ns)
544 #endif /* CONFIG_BLK_DEV_INTEGRITY */
546 static void nvme_config_discard(struct nvme_ns *ns)
548 u32 logical_block_size = queue_logical_block_size(ns->queue);
549 ns->queue->limits.discard_zeroes_data = 0;
550 ns->queue->limits.discard_alignment = logical_block_size;
551 ns->queue->limits.discard_granularity = logical_block_size;
552 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
553 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
556 static int nvme_revalidate_disk(struct gendisk *disk)
558 struct nvme_ns *ns = disk->private_data;
559 struct nvme_id_ns *id;
564 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
565 set_capacity(disk, 0);
568 if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
569 dev_warn(ns->ctrl->dev, "%s: Identify failure nvme%dn%d\n",
570 __func__, ns->ctrl->instance, ns->ns_id);
578 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
579 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
580 dev_warn(ns->ctrl->dev,
581 "%s: LightNVM init failure\n", __func__);
585 ns->type = NVME_NS_LIGHTNVM;
588 if (ns->ctrl->vs >= NVME_VS(1, 1))
589 memcpy(ns->eui, id->eui64, sizeof(ns->eui));
590 if (ns->ctrl->vs >= NVME_VS(1, 2))
591 memcpy(ns->uuid, id->nguid, sizeof(ns->uuid));
594 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
595 ns->lba_shift = id->lbaf[lbaf].ds;
596 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
597 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
600 * If identify namespace failed, use default 512 byte block size so
601 * block layer can use before failing read/write for 0 capacity.
603 if (ns->lba_shift == 0)
605 bs = 1 << ns->lba_shift;
606 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
607 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
608 id->dps & NVME_NS_DPS_PI_MASK : 0;
610 blk_mq_freeze_queue(disk->queue);
611 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
613 bs != queue_logical_block_size(disk->queue) ||
614 (ns->ms && ns->ext)))
615 blk_integrity_unregister(disk);
617 ns->pi_type = pi_type;
618 blk_queue_logical_block_size(ns->queue, bs);
620 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
621 nvme_init_integrity(ns);
622 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
623 set_capacity(disk, 0);
625 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
627 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
628 nvme_config_discard(ns);
629 blk_mq_unfreeze_queue(disk->queue);
635 static char nvme_pr_type(enum pr_type type)
638 case PR_WRITE_EXCLUSIVE:
640 case PR_EXCLUSIVE_ACCESS:
642 case PR_WRITE_EXCLUSIVE_REG_ONLY:
644 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
646 case PR_WRITE_EXCLUSIVE_ALL_REGS:
648 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
655 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
656 u64 key, u64 sa_key, u8 op)
658 struct nvme_ns *ns = bdev->bd_disk->private_data;
659 struct nvme_command c;
660 u8 data[16] = { 0, };
662 put_unaligned_le64(key, &data[0]);
663 put_unaligned_le64(sa_key, &data[8]);
665 memset(&c, 0, sizeof(c));
666 c.common.opcode = op;
667 c.common.nsid = cpu_to_le32(ns->ns_id);
668 c.common.cdw10[0] = cpu_to_le32(cdw10);
670 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
673 static int nvme_pr_register(struct block_device *bdev, u64 old,
674 u64 new, unsigned flags)
678 if (flags & ~PR_FL_IGNORE_KEY)
682 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
683 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
684 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
687 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
688 enum pr_type type, unsigned flags)
692 if (flags & ~PR_FL_IGNORE_KEY)
695 cdw10 = nvme_pr_type(type) << 8;
696 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
697 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
700 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
701 enum pr_type type, bool abort)
703 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
704 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
707 static int nvme_pr_clear(struct block_device *bdev, u64 key)
709 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
710 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
713 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
715 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
716 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
719 static const struct pr_ops nvme_pr_ops = {
720 .pr_register = nvme_pr_register,
721 .pr_reserve = nvme_pr_reserve,
722 .pr_release = nvme_pr_release,
723 .pr_preempt = nvme_pr_preempt,
724 .pr_clear = nvme_pr_clear,
727 static const struct block_device_operations nvme_fops = {
728 .owner = THIS_MODULE,
730 .compat_ioctl = nvme_compat_ioctl,
732 .release = nvme_release,
733 .getgeo = nvme_getgeo,
734 .revalidate_disk= nvme_revalidate_disk,
735 .pr_ops = &nvme_pr_ops,
738 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
740 unsigned long timeout =
741 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
742 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
745 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
746 if ((csts & NVME_CSTS_RDY) == bit)
750 if (fatal_signal_pending(current))
752 if (time_after(jiffies, timeout)) {
754 "Device not ready; aborting %s\n", enabled ?
755 "initialisation" : "reset");
764 * If the device has been passed off to us in an enabled state, just clear
765 * the enabled bit. The spec says we should set the 'shutdown notification
766 * bits', but doing so may cause the device to complete commands to the
767 * admin queue ... and we don't know what memory that might be pointing at!
769 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
773 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
774 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
776 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
779 return nvme_wait_ready(ctrl, cap, false);
782 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
785 * Default to a 4K page size, with the intention to update this
786 * path in the future to accomodate architectures with differing
787 * kernel and IO page sizes.
789 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
792 if (page_shift < dev_page_min) {
794 "Minimum device page size %u too large for host (%u)\n",
795 1 << dev_page_min, 1 << page_shift);
799 ctrl->page_size = 1 << page_shift;
801 ctrl->ctrl_config = NVME_CC_CSS_NVM;
802 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
803 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
804 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
805 ctrl->ctrl_config |= NVME_CC_ENABLE;
807 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
810 return nvme_wait_ready(ctrl, cap, true);
813 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
815 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
819 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
820 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
822 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
826 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
827 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
831 if (fatal_signal_pending(current))
833 if (time_after(jiffies, timeout)) {
835 "Device shutdown incomplete; abort shutdown\n");
844 * Initialize the cached copies of the Identify data and various controller
845 * register in our nvme_ctrl structure. This should be called as soon as
846 * the admin queue is fully up and running.
848 int nvme_init_identify(struct nvme_ctrl *ctrl)
850 struct nvme_id_ctrl *id;
854 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
856 dev_err(ctrl->dev, "Reading VS failed (%d)\n", ret);
860 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
862 dev_err(ctrl->dev, "Reading CAP failed (%d)\n", ret);
865 page_shift = NVME_CAP_MPSMIN(cap) + 12;
867 if (ctrl->vs >= NVME_VS(1, 1))
868 ctrl->subsystem = NVME_CAP_NSSRC(cap);
870 ret = nvme_identify_ctrl(ctrl, &id);
872 dev_err(ctrl->dev, "Identify Controller failed (%d)\n", ret);
876 ctrl->oncs = le16_to_cpup(&id->oncs);
877 atomic_set(&ctrl->abort_limit, id->acl + 1);
879 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
880 memcpy(ctrl->model, id->mn, sizeof(id->mn));
881 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
883 ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9);
885 ctrl->max_hw_sectors = UINT_MAX;
887 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
888 unsigned int max_hw_sectors;
890 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
891 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
892 if (ctrl->max_hw_sectors) {
893 ctrl->max_hw_sectors = min(max_hw_sectors,
894 ctrl->max_hw_sectors);
896 ctrl->max_hw_sectors = max_hw_sectors;
904 static int nvme_dev_open(struct inode *inode, struct file *file)
906 struct nvme_ctrl *ctrl;
907 int instance = iminor(inode);
910 spin_lock(&dev_list_lock);
911 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
912 if (ctrl->instance != instance)
915 if (!ctrl->admin_q) {
919 if (!kref_get_unless_zero(&ctrl->kref))
921 file->private_data = ctrl;
925 spin_unlock(&dev_list_lock);
930 static int nvme_dev_release(struct inode *inode, struct file *file)
932 nvme_put_ctrl(file->private_data);
936 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
941 mutex_lock(&ctrl->namespaces_mutex);
942 if (list_empty(&ctrl->namespaces)) {
947 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
948 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
950 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
956 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
958 mutex_unlock(&ctrl->namespaces_mutex);
960 ret = nvme_user_cmd(ctrl, ns, argp);
965 mutex_unlock(&ctrl->namespaces_mutex);
969 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
972 struct nvme_ctrl *ctrl = file->private_data;
973 void __user *argp = (void __user *)arg;
976 case NVME_IOCTL_ADMIN_CMD:
977 return nvme_user_cmd(ctrl, NULL, argp);
978 case NVME_IOCTL_IO_CMD:
979 return nvme_dev_user_cmd(ctrl, argp);
980 case NVME_IOCTL_RESET:
981 dev_warn(ctrl->dev, "resetting controller\n");
982 return ctrl->ops->reset_ctrl(ctrl);
983 case NVME_IOCTL_SUBSYS_RESET:
984 return nvme_reset_subsystem(ctrl);
990 static const struct file_operations nvme_dev_fops = {
991 .owner = THIS_MODULE,
992 .open = nvme_dev_open,
993 .release = nvme_dev_release,
994 .unlocked_ioctl = nvme_dev_ioctl,
995 .compat_ioctl = nvme_dev_ioctl,
998 static ssize_t nvme_sysfs_reset(struct device *dev,
999 struct device_attribute *attr, const char *buf,
1002 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1005 ret = ctrl->ops->reset_ctrl(ctrl);
1010 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1012 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1015 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1016 return sprintf(buf, "%pU\n", ns->uuid);
1018 static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1020 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1023 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1024 return sprintf(buf, "%8phd\n", ns->eui);
1026 static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1028 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1031 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1032 return sprintf(buf, "%d\n", ns->ns_id);
1034 static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1036 static struct attribute *nvme_ns_attrs[] = {
1037 &dev_attr_uuid.attr,
1039 &dev_attr_nsid.attr,
1043 static umode_t nvme_attrs_are_visible(struct kobject *kobj,
1044 struct attribute *a, int n)
1046 struct device *dev = container_of(kobj, struct device, kobj);
1047 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1049 if (a == &dev_attr_uuid.attr) {
1050 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1053 if (a == &dev_attr_eui.attr) {
1054 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1060 static const struct attribute_group nvme_ns_attr_group = {
1061 .attrs = nvme_ns_attrs,
1062 .is_visible = nvme_attrs_are_visible,
1065 #define nvme_show_function(field) \
1066 static ssize_t field##_show(struct device *dev, \
1067 struct device_attribute *attr, char *buf) \
1069 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1070 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1072 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1074 nvme_show_function(model);
1075 nvme_show_function(serial);
1076 nvme_show_function(firmware_rev);
1078 static struct attribute *nvme_dev_attrs[] = {
1079 &dev_attr_reset_controller.attr,
1080 &dev_attr_model.attr,
1081 &dev_attr_serial.attr,
1082 &dev_attr_firmware_rev.attr,
1086 static struct attribute_group nvme_dev_attrs_group = {
1087 .attrs = nvme_dev_attrs,
1090 static const struct attribute_group *nvme_dev_attr_groups[] = {
1091 &nvme_dev_attrs_group,
1095 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1097 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1098 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1100 return nsa->ns_id - nsb->ns_id;
1103 static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1107 lockdep_assert_held(&ctrl->namespaces_mutex);
1109 list_for_each_entry(ns, &ctrl->namespaces, list) {
1110 if (ns->ns_id == nsid)
1112 if (ns->ns_id > nsid)
1118 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1121 struct gendisk *disk;
1122 int node = dev_to_node(ctrl->dev);
1124 lockdep_assert_held(&ctrl->namespaces_mutex);
1126 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1130 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1131 if (ns->instance < 0)
1134 ns->queue = blk_mq_init_queue(ctrl->tagset);
1135 if (IS_ERR(ns->queue))
1136 goto out_release_instance;
1137 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1138 ns->queue->queuedata = ns;
1141 disk = alloc_disk_node(0, node);
1143 goto out_free_queue;
1145 kref_init(&ns->kref);
1148 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1150 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1151 if (ctrl->max_hw_sectors) {
1152 blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors);
1153 blk_queue_max_segments(ns->queue,
1154 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1);
1156 if (ctrl->stripe_size)
1157 blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9);
1158 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1159 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
1160 blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1);
1162 disk->major = nvme_major;
1163 disk->first_minor = 0;
1164 disk->fops = &nvme_fops;
1165 disk->private_data = ns;
1166 disk->queue = ns->queue;
1167 disk->driverfs_dev = ctrl->device;
1168 disk->flags = GENHD_FL_EXT_DEVT;
1169 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
1171 if (nvme_revalidate_disk(ns->disk))
1174 list_add_tail(&ns->list, &ctrl->namespaces);
1175 kref_get(&ctrl->kref);
1176 if (ns->type == NVME_NS_LIGHTNVM)
1180 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1181 &nvme_ns_attr_group))
1182 pr_warn("%s: failed to create sysfs group for identification\n",
1183 ns->disk->disk_name);
1188 blk_cleanup_queue(ns->queue);
1189 out_release_instance:
1190 ida_simple_remove(&ctrl->ns_ida, ns->instance);
1195 static void nvme_ns_remove(struct nvme_ns *ns)
1197 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
1200 if (ns->disk->flags & GENHD_FL_UP) {
1201 if (blk_get_integrity(ns->disk))
1202 blk_integrity_unregister(ns->disk);
1203 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1204 &nvme_ns_attr_group);
1205 del_gendisk(ns->disk);
1206 blk_mq_abort_requeue_list(ns->queue);
1207 blk_cleanup_queue(ns->queue);
1209 mutex_lock(&ns->ctrl->namespaces_mutex);
1210 list_del_init(&ns->list);
1211 mutex_unlock(&ns->ctrl->namespaces_mutex);
1215 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1219 ns = nvme_find_ns(ctrl, nsid);
1221 if (revalidate_disk(ns->disk))
1224 nvme_alloc_ns(ctrl, nsid);
1227 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1231 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1234 ns_list = kzalloc(0x1000, GFP_KERNEL);
1238 for (i = 0; i < num_lists; i++) {
1239 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1243 for (j = 0; j < min(nn, 1024U); j++) {
1244 nsid = le32_to_cpu(ns_list[j]);
1248 nvme_validate_ns(ctrl, nsid);
1250 while (++prev < nsid) {
1251 ns = nvme_find_ns(ctrl, prev);
1263 static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn)
1265 struct nvme_ns *ns, *next;
1268 lockdep_assert_held(&ctrl->namespaces_mutex);
1270 for (i = 1; i <= nn; i++)
1271 nvme_validate_ns(ctrl, i);
1273 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1279 void nvme_scan_namespaces(struct nvme_ctrl *ctrl)
1281 struct nvme_id_ctrl *id;
1284 if (nvme_identify_ctrl(ctrl, &id))
1287 mutex_lock(&ctrl->namespaces_mutex);
1288 nn = le32_to_cpu(id->nn);
1289 if (ctrl->vs >= NVME_VS(1, 1) &&
1290 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1291 if (!nvme_scan_ns_list(ctrl, nn))
1294 __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn));
1296 list_sort(NULL, &ctrl->namespaces, ns_cmp);
1297 mutex_unlock(&ctrl->namespaces_mutex);
1301 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1303 struct nvme_ns *ns, *next;
1305 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1309 static DEFINE_IDA(nvme_instance_ida);
1311 static int nvme_set_instance(struct nvme_ctrl *ctrl)
1313 int instance, error;
1316 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1319 spin_lock(&dev_list_lock);
1320 error = ida_get_new(&nvme_instance_ida, &instance);
1321 spin_unlock(&dev_list_lock);
1322 } while (error == -EAGAIN);
1327 ctrl->instance = instance;
1331 static void nvme_release_instance(struct nvme_ctrl *ctrl)
1333 spin_lock(&dev_list_lock);
1334 ida_remove(&nvme_instance_ida, ctrl->instance);
1335 spin_unlock(&dev_list_lock);
1338 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
1340 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
1342 spin_lock(&dev_list_lock);
1343 list_del(&ctrl->node);
1344 spin_unlock(&dev_list_lock);
1347 static void nvme_free_ctrl(struct kref *kref)
1349 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
1351 put_device(ctrl->device);
1352 nvme_release_instance(ctrl);
1353 ida_destroy(&ctrl->ns_ida);
1355 ctrl->ops->free_ctrl(ctrl);
1358 void nvme_put_ctrl(struct nvme_ctrl *ctrl)
1360 kref_put(&ctrl->kref, nvme_free_ctrl);
1364 * Initialize a NVMe controller structures. This needs to be called during
1365 * earliest initialization so that we have the initialized structured around
1368 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
1369 const struct nvme_ctrl_ops *ops, unsigned long quirks)
1373 INIT_LIST_HEAD(&ctrl->namespaces);
1374 mutex_init(&ctrl->namespaces_mutex);
1375 kref_init(&ctrl->kref);
1378 ctrl->quirks = quirks;
1380 ret = nvme_set_instance(ctrl);
1384 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
1385 MKDEV(nvme_char_major, ctrl->instance),
1386 dev, nvme_dev_attr_groups,
1387 "nvme%d", ctrl->instance);
1388 if (IS_ERR(ctrl->device)) {
1389 ret = PTR_ERR(ctrl->device);
1390 goto out_release_instance;
1392 get_device(ctrl->device);
1393 dev_set_drvdata(ctrl->device, ctrl);
1394 ida_init(&ctrl->ns_ida);
1396 spin_lock(&dev_list_lock);
1397 list_add_tail(&ctrl->node, &nvme_ctrl_list);
1398 spin_unlock(&dev_list_lock);
1401 out_release_instance:
1402 nvme_release_instance(ctrl);
1408 * nvme_kill_queues(): Ends all namespace queues
1409 * @ctrl: the dead controller that needs to end
1411 * Call this function when the driver determines it is unable to get the
1412 * controller in a state capable of servicing IO.
1414 void nvme_kill_queues(struct nvme_ctrl *ctrl)
1418 mutex_lock(&ctrl->namespaces_mutex);
1419 list_for_each_entry(ns, &ctrl->namespaces, list) {
1420 if (!kref_get_unless_zero(&ns->kref))
1424 * Revalidating a dead namespace sets capacity to 0. This will
1425 * end buffered writers dirtying pages that can't be synced.
1427 if (!test_and_set_bit(NVME_NS_DEAD, &ns->flags))
1428 revalidate_disk(ns->disk);
1430 blk_set_queue_dying(ns->queue);
1431 blk_mq_abort_requeue_list(ns->queue);
1432 blk_mq_start_stopped_hw_queues(ns->queue, true);
1436 mutex_unlock(&ctrl->namespaces_mutex);
1439 void nvme_stop_queues(struct nvme_ctrl *ctrl)
1443 mutex_lock(&ctrl->namespaces_mutex);
1444 list_for_each_entry(ns, &ctrl->namespaces, list) {
1445 spin_lock_irq(ns->queue->queue_lock);
1446 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
1447 spin_unlock_irq(ns->queue->queue_lock);
1449 blk_mq_cancel_requeue_work(ns->queue);
1450 blk_mq_stop_hw_queues(ns->queue);
1452 mutex_unlock(&ctrl->namespaces_mutex);
1455 void nvme_start_queues(struct nvme_ctrl *ctrl)
1459 mutex_lock(&ctrl->namespaces_mutex);
1460 list_for_each_entry(ns, &ctrl->namespaces, list) {
1461 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
1462 blk_mq_start_stopped_hw_queues(ns->queue, true);
1463 blk_mq_kick_requeue_list(ns->queue);
1465 mutex_unlock(&ctrl->namespaces_mutex);
1468 int __init nvme_core_init(void)
1472 result = register_blkdev(nvme_major, "nvme");
1475 else if (result > 0)
1476 nvme_major = result;
1478 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
1481 goto unregister_blkdev;
1482 else if (result > 0)
1483 nvme_char_major = result;
1485 nvme_class = class_create(THIS_MODULE, "nvme");
1486 if (IS_ERR(nvme_class)) {
1487 result = PTR_ERR(nvme_class);
1488 goto unregister_chrdev;
1494 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1496 unregister_blkdev(nvme_major, "nvme");
1500 void nvme_core_exit(void)
1502 unregister_blkdev(nvme_major, "nvme");
1503 class_destroy(nvme_class);
1504 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");