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)))
381 case nvme_cmd_compare:
387 length = (io.nblocks + 1) << ns->lba_shift;
388 meta_len = (io.nblocks + 1) * ns->ms;
389 metadata = (void __user *)(uintptr_t)io.metadata;
394 } else if (meta_len) {
395 if ((io.metadata & 3) || !io.metadata)
399 memset(&c, 0, sizeof(c));
400 c.rw.opcode = io.opcode;
401 c.rw.flags = io.flags;
402 c.rw.nsid = cpu_to_le32(ns->ns_id);
403 c.rw.slba = cpu_to_le64(io.slba);
404 c.rw.length = cpu_to_le16(io.nblocks);
405 c.rw.control = cpu_to_le16(io.control);
406 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
407 c.rw.reftag = cpu_to_le32(io.reftag);
408 c.rw.apptag = cpu_to_le16(io.apptag);
409 c.rw.appmask = cpu_to_le16(io.appmask);
411 return __nvme_submit_user_cmd(ns->queue, &c,
412 (void __user *)(uintptr_t)io.addr, length,
413 metadata, meta_len, io.slba, NULL, 0);
416 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
417 struct nvme_passthru_cmd __user *ucmd)
419 struct nvme_passthru_cmd cmd;
420 struct nvme_command c;
421 unsigned timeout = 0;
424 if (!capable(CAP_SYS_ADMIN))
426 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
429 memset(&c, 0, sizeof(c));
430 c.common.opcode = cmd.opcode;
431 c.common.flags = cmd.flags;
432 c.common.nsid = cpu_to_le32(cmd.nsid);
433 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
434 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
435 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
436 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
437 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
438 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
439 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
440 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
443 timeout = msecs_to_jiffies(cmd.timeout_ms);
445 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
446 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
447 &cmd.result, timeout);
449 if (put_user(cmd.result, &ucmd->result))
456 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
457 unsigned int cmd, unsigned long arg)
459 struct nvme_ns *ns = bdev->bd_disk->private_data;
463 force_successful_syscall_return();
465 case NVME_IOCTL_ADMIN_CMD:
466 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
467 case NVME_IOCTL_IO_CMD:
468 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
469 case NVME_IOCTL_SUBMIT_IO:
470 return nvme_submit_io(ns, (void __user *)arg);
471 #ifdef CONFIG_BLK_DEV_NVME_SCSI
472 case SG_GET_VERSION_NUM:
473 return nvme_sg_get_version_num((void __user *)arg);
475 return nvme_sg_io(ns, (void __user *)arg);
483 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
484 unsigned int cmd, unsigned long arg)
490 return nvme_ioctl(bdev, mode, cmd, arg);
493 #define nvme_compat_ioctl NULL
496 static int nvme_open(struct block_device *bdev, fmode_t mode)
498 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
501 static void nvme_release(struct gendisk *disk, fmode_t mode)
503 nvme_put_ns(disk->private_data);
506 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
508 /* some standard values */
510 geo->sectors = 1 << 5;
511 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
515 #ifdef CONFIG_BLK_DEV_INTEGRITY
516 static void nvme_init_integrity(struct nvme_ns *ns)
518 struct blk_integrity integrity;
520 switch (ns->pi_type) {
521 case NVME_NS_DPS_PI_TYPE3:
522 integrity.profile = &t10_pi_type3_crc;
524 case NVME_NS_DPS_PI_TYPE1:
525 case NVME_NS_DPS_PI_TYPE2:
526 integrity.profile = &t10_pi_type1_crc;
529 integrity.profile = NULL;
532 integrity.tuple_size = ns->ms;
533 blk_integrity_register(ns->disk, &integrity);
534 blk_queue_max_integrity_segments(ns->queue, 1);
537 static void nvme_init_integrity(struct nvme_ns *ns)
540 #endif /* CONFIG_BLK_DEV_INTEGRITY */
542 static void nvme_config_discard(struct nvme_ns *ns)
544 u32 logical_block_size = queue_logical_block_size(ns->queue);
545 ns->queue->limits.discard_zeroes_data = 0;
546 ns->queue->limits.discard_alignment = logical_block_size;
547 ns->queue->limits.discard_granularity = logical_block_size;
548 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
549 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
552 static int nvme_revalidate_disk(struct gendisk *disk)
554 struct nvme_ns *ns = disk->private_data;
555 struct nvme_id_ns *id;
560 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
561 set_capacity(disk, 0);
564 if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
565 dev_warn(ns->ctrl->dev, "%s: Identify failure nvme%dn%d\n",
566 __func__, ns->ctrl->instance, ns->ns_id);
574 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
575 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
576 dev_warn(ns->ctrl->dev,
577 "%s: LightNVM init failure\n", __func__);
581 ns->type = NVME_NS_LIGHTNVM;
584 if (ns->ctrl->vs >= NVME_VS(1, 1))
585 memcpy(ns->eui, id->eui64, sizeof(ns->eui));
586 if (ns->ctrl->vs >= NVME_VS(1, 2))
587 memcpy(ns->uuid, id->nguid, sizeof(ns->uuid));
590 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
591 ns->lba_shift = id->lbaf[lbaf].ds;
592 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
593 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
596 * If identify namespace failed, use default 512 byte block size so
597 * block layer can use before failing read/write for 0 capacity.
599 if (ns->lba_shift == 0)
601 bs = 1 << ns->lba_shift;
602 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
603 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
604 id->dps & NVME_NS_DPS_PI_MASK : 0;
606 blk_mq_freeze_queue(disk->queue);
607 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
609 bs != queue_logical_block_size(disk->queue) ||
610 (ns->ms && ns->ext)))
611 blk_integrity_unregister(disk);
613 ns->pi_type = pi_type;
614 blk_queue_logical_block_size(ns->queue, bs);
616 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
617 nvme_init_integrity(ns);
618 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
619 set_capacity(disk, 0);
621 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
623 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
624 nvme_config_discard(ns);
625 blk_mq_unfreeze_queue(disk->queue);
631 static char nvme_pr_type(enum pr_type type)
634 case PR_WRITE_EXCLUSIVE:
636 case PR_EXCLUSIVE_ACCESS:
638 case PR_WRITE_EXCLUSIVE_REG_ONLY:
640 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
642 case PR_WRITE_EXCLUSIVE_ALL_REGS:
644 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
651 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
652 u64 key, u64 sa_key, u8 op)
654 struct nvme_ns *ns = bdev->bd_disk->private_data;
655 struct nvme_command c;
656 u8 data[16] = { 0, };
658 put_unaligned_le64(key, &data[0]);
659 put_unaligned_le64(sa_key, &data[8]);
661 memset(&c, 0, sizeof(c));
662 c.common.opcode = op;
663 c.common.nsid = cpu_to_le32(ns->ns_id);
664 c.common.cdw10[0] = cpu_to_le32(cdw10);
666 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
669 static int nvme_pr_register(struct block_device *bdev, u64 old,
670 u64 new, unsigned flags)
674 if (flags & ~PR_FL_IGNORE_KEY)
678 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
679 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
680 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
683 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
684 enum pr_type type, unsigned flags)
688 if (flags & ~PR_FL_IGNORE_KEY)
691 cdw10 = nvme_pr_type(type) << 8;
692 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
693 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
696 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
697 enum pr_type type, bool abort)
699 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
700 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
703 static int nvme_pr_clear(struct block_device *bdev, u64 key)
705 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
706 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
709 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
711 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
712 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
715 static const struct pr_ops nvme_pr_ops = {
716 .pr_register = nvme_pr_register,
717 .pr_reserve = nvme_pr_reserve,
718 .pr_release = nvme_pr_release,
719 .pr_preempt = nvme_pr_preempt,
720 .pr_clear = nvme_pr_clear,
723 static const struct block_device_operations nvme_fops = {
724 .owner = THIS_MODULE,
726 .compat_ioctl = nvme_compat_ioctl,
728 .release = nvme_release,
729 .getgeo = nvme_getgeo,
730 .revalidate_disk= nvme_revalidate_disk,
731 .pr_ops = &nvme_pr_ops,
734 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
736 unsigned long timeout =
737 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
738 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
741 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
742 if ((csts & NVME_CSTS_RDY) == bit)
746 if (fatal_signal_pending(current))
748 if (time_after(jiffies, timeout)) {
750 "Device not ready; aborting %s\n", enabled ?
751 "initialisation" : "reset");
760 * If the device has been passed off to us in an enabled state, just clear
761 * the enabled bit. The spec says we should set the 'shutdown notification
762 * bits', but doing so may cause the device to complete commands to the
763 * admin queue ... and we don't know what memory that might be pointing at!
765 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
769 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
770 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
772 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
775 return nvme_wait_ready(ctrl, cap, false);
778 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
781 * Default to a 4K page size, with the intention to update this
782 * path in the future to accomodate architectures with differing
783 * kernel and IO page sizes.
785 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
788 if (page_shift < dev_page_min) {
790 "Minimum device page size %u too large for host (%u)\n",
791 1 << dev_page_min, 1 << page_shift);
795 ctrl->page_size = 1 << page_shift;
797 ctrl->ctrl_config = NVME_CC_CSS_NVM;
798 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
799 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
800 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
801 ctrl->ctrl_config |= NVME_CC_ENABLE;
803 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
806 return nvme_wait_ready(ctrl, cap, true);
809 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
811 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
815 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
816 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
818 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
822 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
823 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
827 if (fatal_signal_pending(current))
829 if (time_after(jiffies, timeout)) {
831 "Device shutdown incomplete; abort shutdown\n");
840 * Initialize the cached copies of the Identify data and various controller
841 * register in our nvme_ctrl structure. This should be called as soon as
842 * the admin queue is fully up and running.
844 int nvme_init_identify(struct nvme_ctrl *ctrl)
846 struct nvme_id_ctrl *id;
850 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
852 dev_err(ctrl->dev, "Reading VS failed (%d)\n", ret);
856 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
858 dev_err(ctrl->dev, "Reading CAP failed (%d)\n", ret);
861 page_shift = NVME_CAP_MPSMIN(cap) + 12;
863 if (ctrl->vs >= NVME_VS(1, 1))
864 ctrl->subsystem = NVME_CAP_NSSRC(cap);
866 ret = nvme_identify_ctrl(ctrl, &id);
868 dev_err(ctrl->dev, "Identify Controller failed (%d)\n", ret);
872 ctrl->oncs = le16_to_cpup(&id->oncs);
873 atomic_set(&ctrl->abort_limit, id->acl + 1);
875 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
876 memcpy(ctrl->model, id->mn, sizeof(id->mn));
877 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
879 ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9);
881 ctrl->max_hw_sectors = UINT_MAX;
883 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
884 unsigned int max_hw_sectors;
886 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
887 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
888 if (ctrl->max_hw_sectors) {
889 ctrl->max_hw_sectors = min(max_hw_sectors,
890 ctrl->max_hw_sectors);
892 ctrl->max_hw_sectors = max_hw_sectors;
900 static int nvme_dev_open(struct inode *inode, struct file *file)
902 struct nvme_ctrl *ctrl;
903 int instance = iminor(inode);
906 spin_lock(&dev_list_lock);
907 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
908 if (ctrl->instance != instance)
911 if (!ctrl->admin_q) {
915 if (!kref_get_unless_zero(&ctrl->kref))
917 file->private_data = ctrl;
921 spin_unlock(&dev_list_lock);
926 static int nvme_dev_release(struct inode *inode, struct file *file)
928 nvme_put_ctrl(file->private_data);
932 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
937 mutex_lock(&ctrl->namespaces_mutex);
938 if (list_empty(&ctrl->namespaces)) {
943 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
944 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
946 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
952 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
954 mutex_unlock(&ctrl->namespaces_mutex);
956 ret = nvme_user_cmd(ctrl, ns, argp);
961 mutex_unlock(&ctrl->namespaces_mutex);
965 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
968 struct nvme_ctrl *ctrl = file->private_data;
969 void __user *argp = (void __user *)arg;
972 case NVME_IOCTL_ADMIN_CMD:
973 return nvme_user_cmd(ctrl, NULL, argp);
974 case NVME_IOCTL_IO_CMD:
975 return nvme_dev_user_cmd(ctrl, argp);
976 case NVME_IOCTL_RESET:
977 dev_warn(ctrl->dev, "resetting controller\n");
978 return ctrl->ops->reset_ctrl(ctrl);
979 case NVME_IOCTL_SUBSYS_RESET:
980 return nvme_reset_subsystem(ctrl);
986 static const struct file_operations nvme_dev_fops = {
987 .owner = THIS_MODULE,
988 .open = nvme_dev_open,
989 .release = nvme_dev_release,
990 .unlocked_ioctl = nvme_dev_ioctl,
991 .compat_ioctl = nvme_dev_ioctl,
994 static ssize_t nvme_sysfs_reset(struct device *dev,
995 struct device_attribute *attr, const char *buf,
998 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1001 ret = ctrl->ops->reset_ctrl(ctrl);
1006 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1008 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1011 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1012 return sprintf(buf, "%pU\n", ns->uuid);
1014 static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1016 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1019 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1020 return sprintf(buf, "%8phd\n", ns->eui);
1022 static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1024 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1027 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1028 return sprintf(buf, "%d\n", ns->ns_id);
1030 static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1032 static struct attribute *nvme_ns_attrs[] = {
1033 &dev_attr_uuid.attr,
1035 &dev_attr_nsid.attr,
1039 static umode_t nvme_attrs_are_visible(struct kobject *kobj,
1040 struct attribute *a, int n)
1042 struct device *dev = container_of(kobj, struct device, kobj);
1043 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1045 if (a == &dev_attr_uuid.attr) {
1046 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1049 if (a == &dev_attr_eui.attr) {
1050 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1056 static const struct attribute_group nvme_ns_attr_group = {
1057 .attrs = nvme_ns_attrs,
1058 .is_visible = nvme_attrs_are_visible,
1061 #define nvme_show_function(field) \
1062 static ssize_t field##_show(struct device *dev, \
1063 struct device_attribute *attr, char *buf) \
1065 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1066 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1068 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1070 nvme_show_function(model);
1071 nvme_show_function(serial);
1072 nvme_show_function(firmware_rev);
1074 static struct attribute *nvme_dev_attrs[] = {
1075 &dev_attr_reset_controller.attr,
1076 &dev_attr_model.attr,
1077 &dev_attr_serial.attr,
1078 &dev_attr_firmware_rev.attr,
1082 static struct attribute_group nvme_dev_attrs_group = {
1083 .attrs = nvme_dev_attrs,
1086 static const struct attribute_group *nvme_dev_attr_groups[] = {
1087 &nvme_dev_attrs_group,
1091 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1093 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1094 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1096 return nsa->ns_id - nsb->ns_id;
1099 static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1103 lockdep_assert_held(&ctrl->namespaces_mutex);
1105 list_for_each_entry(ns, &ctrl->namespaces, list) {
1106 if (ns->ns_id == nsid)
1108 if (ns->ns_id > nsid)
1114 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1117 struct gendisk *disk;
1118 int node = dev_to_node(ctrl->dev);
1120 lockdep_assert_held(&ctrl->namespaces_mutex);
1122 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1126 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1127 if (ns->instance < 0)
1130 ns->queue = blk_mq_init_queue(ctrl->tagset);
1131 if (IS_ERR(ns->queue))
1132 goto out_release_instance;
1133 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1134 ns->queue->queuedata = ns;
1137 disk = alloc_disk_node(0, node);
1139 goto out_free_queue;
1141 kref_init(&ns->kref);
1144 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1146 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1147 if (ctrl->max_hw_sectors) {
1148 blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors);
1149 blk_queue_max_segments(ns->queue,
1150 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1);
1152 if (ctrl->stripe_size)
1153 blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9);
1154 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1155 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
1156 blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1);
1158 disk->major = nvme_major;
1159 disk->first_minor = 0;
1160 disk->fops = &nvme_fops;
1161 disk->private_data = ns;
1162 disk->queue = ns->queue;
1163 disk->driverfs_dev = ctrl->device;
1164 disk->flags = GENHD_FL_EXT_DEVT;
1165 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
1167 if (nvme_revalidate_disk(ns->disk))
1170 list_add_tail(&ns->list, &ctrl->namespaces);
1171 kref_get(&ctrl->kref);
1172 if (ns->type == NVME_NS_LIGHTNVM)
1176 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1177 &nvme_ns_attr_group))
1178 pr_warn("%s: failed to create sysfs group for identification\n",
1179 ns->disk->disk_name);
1184 blk_cleanup_queue(ns->queue);
1185 out_release_instance:
1186 ida_simple_remove(&ctrl->ns_ida, ns->instance);
1191 static void nvme_ns_remove(struct nvme_ns *ns)
1193 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
1196 if (ns->disk->flags & GENHD_FL_UP) {
1197 if (blk_get_integrity(ns->disk))
1198 blk_integrity_unregister(ns->disk);
1199 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1200 &nvme_ns_attr_group);
1201 del_gendisk(ns->disk);
1202 blk_mq_abort_requeue_list(ns->queue);
1203 blk_cleanup_queue(ns->queue);
1205 mutex_lock(&ns->ctrl->namespaces_mutex);
1206 list_del_init(&ns->list);
1207 mutex_unlock(&ns->ctrl->namespaces_mutex);
1211 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1215 ns = nvme_find_ns(ctrl, nsid);
1217 if (revalidate_disk(ns->disk))
1220 nvme_alloc_ns(ctrl, nsid);
1223 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1227 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1230 ns_list = kzalloc(0x1000, GFP_KERNEL);
1234 for (i = 0; i < num_lists; i++) {
1235 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1239 for (j = 0; j < min(nn, 1024U); j++) {
1240 nsid = le32_to_cpu(ns_list[j]);
1244 nvme_validate_ns(ctrl, nsid);
1246 while (++prev < nsid) {
1247 ns = nvme_find_ns(ctrl, prev);
1259 static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn)
1261 struct nvme_ns *ns, *next;
1264 lockdep_assert_held(&ctrl->namespaces_mutex);
1266 for (i = 1; i <= nn; i++)
1267 nvme_validate_ns(ctrl, i);
1269 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1275 void nvme_scan_namespaces(struct nvme_ctrl *ctrl)
1277 struct nvme_id_ctrl *id;
1280 if (nvme_identify_ctrl(ctrl, &id))
1283 mutex_lock(&ctrl->namespaces_mutex);
1284 nn = le32_to_cpu(id->nn);
1285 if (ctrl->vs >= NVME_VS(1, 1) &&
1286 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1287 if (!nvme_scan_ns_list(ctrl, nn))
1290 __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn));
1292 list_sort(NULL, &ctrl->namespaces, ns_cmp);
1293 mutex_unlock(&ctrl->namespaces_mutex);
1297 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1299 struct nvme_ns *ns, *next;
1301 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1305 static DEFINE_IDA(nvme_instance_ida);
1307 static int nvme_set_instance(struct nvme_ctrl *ctrl)
1309 int instance, error;
1312 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1315 spin_lock(&dev_list_lock);
1316 error = ida_get_new(&nvme_instance_ida, &instance);
1317 spin_unlock(&dev_list_lock);
1318 } while (error == -EAGAIN);
1323 ctrl->instance = instance;
1327 static void nvme_release_instance(struct nvme_ctrl *ctrl)
1329 spin_lock(&dev_list_lock);
1330 ida_remove(&nvme_instance_ida, ctrl->instance);
1331 spin_unlock(&dev_list_lock);
1334 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
1336 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
1338 spin_lock(&dev_list_lock);
1339 list_del(&ctrl->node);
1340 spin_unlock(&dev_list_lock);
1343 static void nvme_free_ctrl(struct kref *kref)
1345 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
1347 put_device(ctrl->device);
1348 nvme_release_instance(ctrl);
1349 ida_destroy(&ctrl->ns_ida);
1351 ctrl->ops->free_ctrl(ctrl);
1354 void nvme_put_ctrl(struct nvme_ctrl *ctrl)
1356 kref_put(&ctrl->kref, nvme_free_ctrl);
1360 * Initialize a NVMe controller structures. This needs to be called during
1361 * earliest initialization so that we have the initialized structured around
1364 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
1365 const struct nvme_ctrl_ops *ops, unsigned long quirks)
1369 INIT_LIST_HEAD(&ctrl->namespaces);
1370 mutex_init(&ctrl->namespaces_mutex);
1371 kref_init(&ctrl->kref);
1374 ctrl->quirks = quirks;
1376 ret = nvme_set_instance(ctrl);
1380 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
1381 MKDEV(nvme_char_major, ctrl->instance),
1382 dev, nvme_dev_attr_groups,
1383 "nvme%d", ctrl->instance);
1384 if (IS_ERR(ctrl->device)) {
1385 ret = PTR_ERR(ctrl->device);
1386 goto out_release_instance;
1388 get_device(ctrl->device);
1389 dev_set_drvdata(ctrl->device, ctrl);
1390 ida_init(&ctrl->ns_ida);
1392 spin_lock(&dev_list_lock);
1393 list_add_tail(&ctrl->node, &nvme_ctrl_list);
1394 spin_unlock(&dev_list_lock);
1397 out_release_instance:
1398 nvme_release_instance(ctrl);
1404 * nvme_kill_queues(): Ends all namespace queues
1405 * @ctrl: the dead controller that needs to end
1407 * Call this function when the driver determines it is unable to get the
1408 * controller in a state capable of servicing IO.
1410 void nvme_kill_queues(struct nvme_ctrl *ctrl)
1414 mutex_lock(&ctrl->namespaces_mutex);
1415 list_for_each_entry(ns, &ctrl->namespaces, list) {
1416 if (!kref_get_unless_zero(&ns->kref))
1420 * Revalidating a dead namespace sets capacity to 0. This will
1421 * end buffered writers dirtying pages that can't be synced.
1423 if (!test_and_set_bit(NVME_NS_DEAD, &ns->flags))
1424 revalidate_disk(ns->disk);
1426 blk_set_queue_dying(ns->queue);
1427 blk_mq_abort_requeue_list(ns->queue);
1428 blk_mq_start_stopped_hw_queues(ns->queue, true);
1432 mutex_unlock(&ctrl->namespaces_mutex);
1435 void nvme_stop_queues(struct nvme_ctrl *ctrl)
1439 mutex_lock(&ctrl->namespaces_mutex);
1440 list_for_each_entry(ns, &ctrl->namespaces, list) {
1441 spin_lock_irq(ns->queue->queue_lock);
1442 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
1443 spin_unlock_irq(ns->queue->queue_lock);
1445 blk_mq_cancel_requeue_work(ns->queue);
1446 blk_mq_stop_hw_queues(ns->queue);
1448 mutex_unlock(&ctrl->namespaces_mutex);
1451 void nvme_start_queues(struct nvme_ctrl *ctrl)
1455 mutex_lock(&ctrl->namespaces_mutex);
1456 list_for_each_entry(ns, &ctrl->namespaces, list) {
1457 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
1458 blk_mq_start_stopped_hw_queues(ns->queue, true);
1459 blk_mq_kick_requeue_list(ns->queue);
1461 mutex_unlock(&ctrl->namespaces_mutex);
1464 int __init nvme_core_init(void)
1468 result = register_blkdev(nvme_major, "nvme");
1471 else if (result > 0)
1472 nvme_major = result;
1474 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
1477 goto unregister_blkdev;
1478 else if (result > 0)
1479 nvme_char_major = result;
1481 nvme_class = class_create(THIS_MODULE, "nvme");
1482 if (IS_ERR(nvme_class)) {
1483 result = PTR_ERR(nvme_class);
1484 goto unregister_chrdev;
1490 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1492 unregister_blkdev(nvme_major, "nvme");
1496 void nvme_core_exit(void)
1498 unregister_blkdev(nvme_major, "nvme");
1499 class_destroy(nvme_class);
1500 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");