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 unsigned char admin_timeout = 60;
37 module_param(admin_timeout, byte, 0644);
38 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
39 EXPORT_SYMBOL_GPL(admin_timeout);
41 unsigned char nvme_io_timeout = 30;
42 module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
43 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
44 EXPORT_SYMBOL_GPL(nvme_io_timeout);
46 unsigned char shutdown_timeout = 5;
47 module_param(shutdown_timeout, byte, 0644);
48 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50 static int nvme_major;
51 module_param(nvme_major, int, 0);
53 static int nvme_char_major;
54 module_param(nvme_char_major, int, 0);
56 static LIST_HEAD(nvme_ctrl_list);
57 static DEFINE_SPINLOCK(dev_list_lock);
59 static struct class *nvme_class;
61 static void nvme_free_ns(struct kref *kref)
63 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
65 if (ns->type == NVME_NS_LIGHTNVM)
66 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
68 spin_lock(&dev_list_lock);
69 ns->disk->private_data = NULL;
70 spin_unlock(&dev_list_lock);
72 nvme_put_ctrl(ns->ctrl);
77 static void nvme_put_ns(struct nvme_ns *ns)
79 kref_put(&ns->kref, nvme_free_ns);
82 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
86 spin_lock(&dev_list_lock);
87 ns = disk->private_data;
89 if (!kref_get_unless_zero(&ns->kref))
91 if (!try_module_get(ns->ctrl->ops->module))
94 spin_unlock(&dev_list_lock);
99 kref_put(&ns->kref, nvme_free_ns);
101 spin_unlock(&dev_list_lock);
105 void nvme_requeue_req(struct request *req)
109 blk_mq_requeue_request(req);
110 spin_lock_irqsave(req->q->queue_lock, flags);
111 if (!blk_queue_stopped(req->q))
112 blk_mq_kick_requeue_list(req->q);
113 spin_unlock_irqrestore(req->q->queue_lock, flags);
115 EXPORT_SYMBOL_GPL(nvme_requeue_req);
117 struct request *nvme_alloc_request(struct request_queue *q,
118 struct nvme_command *cmd, unsigned int flags)
120 bool write = cmd->common.opcode & 1;
123 req = blk_mq_alloc_request(q, write, flags);
127 req->cmd_type = REQ_TYPE_DRV_PRIV;
128 req->cmd_flags |= REQ_FAILFAST_DRIVER;
130 req->__sector = (sector_t) -1;
131 req->bio = req->biotail = NULL;
133 req->cmd = (unsigned char *)cmd;
134 req->cmd_len = sizeof(struct nvme_command);
138 EXPORT_SYMBOL_GPL(nvme_alloc_request);
141 * Returns 0 on success. If the result is negative, it's a Linux error code;
142 * if the result is positive, it's an NVM Express status code
144 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
145 struct nvme_completion *cqe, void *buffer, unsigned bufflen,
151 req = nvme_alloc_request(q, cmd, 0);
155 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
158 if (buffer && bufflen) {
159 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
164 blk_execute_rq(req->q, NULL, req, 0);
167 blk_mq_free_request(req);
171 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
172 void *buffer, unsigned bufflen)
174 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0);
176 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
178 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
179 void __user *ubuffer, unsigned bufflen,
180 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
181 u32 *result, unsigned timeout)
183 bool write = cmd->common.opcode & 1;
184 struct nvme_completion cqe;
185 struct nvme_ns *ns = q->queuedata;
186 struct gendisk *disk = ns ? ns->disk : NULL;
188 struct bio *bio = NULL;
192 req = nvme_alloc_request(q, cmd, 0);
196 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
199 if (ubuffer && bufflen) {
200 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
208 bio->bi_bdev = bdget_disk(disk, 0);
215 struct bio_integrity_payload *bip;
217 meta = kmalloc(meta_len, GFP_KERNEL);
224 if (copy_from_user(meta, meta_buffer,
231 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
237 bip->bip_iter.bi_size = meta_len;
238 bip->bip_iter.bi_sector = meta_seed;
240 ret = bio_integrity_add_page(bio, virt_to_page(meta),
241 meta_len, offset_in_page(meta));
242 if (ret != meta_len) {
249 blk_execute_rq(req->q, disk, req, 0);
252 *result = le32_to_cpu(cqe.result);
253 if (meta && !ret && !write) {
254 if (copy_to_user(meta_buffer, meta, meta_len))
261 if (disk && bio->bi_bdev)
263 blk_rq_unmap_user(bio);
266 blk_mq_free_request(req);
270 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
271 void __user *ubuffer, unsigned bufflen, u32 *result,
274 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
278 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
280 struct nvme_command c = { };
283 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
284 c.identify.opcode = nvme_admin_identify;
285 c.identify.cns = cpu_to_le32(1);
287 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
291 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
292 sizeof(struct nvme_id_ctrl));
298 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
300 struct nvme_command c = { };
302 c.identify.opcode = nvme_admin_identify;
303 c.identify.cns = cpu_to_le32(2);
304 c.identify.nsid = cpu_to_le32(nsid);
305 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
308 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
309 struct nvme_id_ns **id)
311 struct nvme_command c = { };
314 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
315 c.identify.opcode = nvme_admin_identify,
316 c.identify.nsid = cpu_to_le32(nsid),
318 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
322 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
323 sizeof(struct nvme_id_ns));
329 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
330 dma_addr_t dma_addr, u32 *result)
332 struct nvme_command c;
333 struct nvme_completion cqe;
336 memset(&c, 0, sizeof(c));
337 c.features.opcode = nvme_admin_get_features;
338 c.features.nsid = cpu_to_le32(nsid);
339 c.features.prp1 = cpu_to_le64(dma_addr);
340 c.features.fid = cpu_to_le32(fid);
342 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
344 *result = le32_to_cpu(cqe.result);
348 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
349 dma_addr_t dma_addr, u32 *result)
351 struct nvme_command c;
352 struct nvme_completion cqe;
355 memset(&c, 0, sizeof(c));
356 c.features.opcode = nvme_admin_set_features;
357 c.features.prp1 = cpu_to_le64(dma_addr);
358 c.features.fid = cpu_to_le32(fid);
359 c.features.dword11 = cpu_to_le32(dword11);
361 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
363 *result = le32_to_cpu(cqe.result);
367 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
369 struct nvme_command c = { };
372 c.common.opcode = nvme_admin_get_log_page,
373 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
374 c.common.cdw10[0] = cpu_to_le32(
375 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
378 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
382 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
383 sizeof(struct nvme_smart_log));
389 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
391 u32 q_count = (*count - 1) | ((*count - 1) << 16);
393 int status, nr_io_queues;
395 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
400 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
401 *count = min(*count, nr_io_queues);
404 EXPORT_SYMBOL_GPL(nvme_set_queue_count);
406 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
408 struct nvme_user_io io;
409 struct nvme_command c;
410 unsigned length, meta_len;
411 void __user *metadata;
413 if (copy_from_user(&io, uio, sizeof(io)))
419 case nvme_cmd_compare:
425 length = (io.nblocks + 1) << ns->lba_shift;
426 meta_len = (io.nblocks + 1) * ns->ms;
427 metadata = (void __user *)(uintptr_t)io.metadata;
432 } else if (meta_len) {
433 if ((io.metadata & 3) || !io.metadata)
437 memset(&c, 0, sizeof(c));
438 c.rw.opcode = io.opcode;
439 c.rw.flags = io.flags;
440 c.rw.nsid = cpu_to_le32(ns->ns_id);
441 c.rw.slba = cpu_to_le64(io.slba);
442 c.rw.length = cpu_to_le16(io.nblocks);
443 c.rw.control = cpu_to_le16(io.control);
444 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
445 c.rw.reftag = cpu_to_le32(io.reftag);
446 c.rw.apptag = cpu_to_le16(io.apptag);
447 c.rw.appmask = cpu_to_le16(io.appmask);
449 return __nvme_submit_user_cmd(ns->queue, &c,
450 (void __user *)(uintptr_t)io.addr, length,
451 metadata, meta_len, io.slba, NULL, 0);
454 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
455 struct nvme_passthru_cmd __user *ucmd)
457 struct nvme_passthru_cmd cmd;
458 struct nvme_command c;
459 unsigned timeout = 0;
462 if (!capable(CAP_SYS_ADMIN))
464 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
467 memset(&c, 0, sizeof(c));
468 c.common.opcode = cmd.opcode;
469 c.common.flags = cmd.flags;
470 c.common.nsid = cpu_to_le32(cmd.nsid);
471 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
472 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
473 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
474 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
475 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
476 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
477 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
478 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
481 timeout = msecs_to_jiffies(cmd.timeout_ms);
483 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
484 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
485 &cmd.result, timeout);
487 if (put_user(cmd.result, &ucmd->result))
494 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
495 unsigned int cmd, unsigned long arg)
497 struct nvme_ns *ns = bdev->bd_disk->private_data;
501 force_successful_syscall_return();
503 case NVME_IOCTL_ADMIN_CMD:
504 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
505 case NVME_IOCTL_IO_CMD:
506 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
507 case NVME_IOCTL_SUBMIT_IO:
508 return nvme_submit_io(ns, (void __user *)arg);
509 #ifdef CONFIG_BLK_DEV_NVME_SCSI
510 case SG_GET_VERSION_NUM:
511 return nvme_sg_get_version_num((void __user *)arg);
513 return nvme_sg_io(ns, (void __user *)arg);
521 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
522 unsigned int cmd, unsigned long arg)
528 return nvme_ioctl(bdev, mode, cmd, arg);
531 #define nvme_compat_ioctl NULL
534 static int nvme_open(struct block_device *bdev, fmode_t mode)
536 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
539 static void nvme_release(struct gendisk *disk, fmode_t mode)
541 struct nvme_ns *ns = disk->private_data;
543 module_put(ns->ctrl->ops->module);
547 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
549 /* some standard values */
551 geo->sectors = 1 << 5;
552 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
556 #ifdef CONFIG_BLK_DEV_INTEGRITY
557 static void nvme_init_integrity(struct nvme_ns *ns)
559 struct blk_integrity integrity;
561 switch (ns->pi_type) {
562 case NVME_NS_DPS_PI_TYPE3:
563 integrity.profile = &t10_pi_type3_crc;
565 case NVME_NS_DPS_PI_TYPE1:
566 case NVME_NS_DPS_PI_TYPE2:
567 integrity.profile = &t10_pi_type1_crc;
570 integrity.profile = NULL;
573 integrity.tuple_size = ns->ms;
574 blk_integrity_register(ns->disk, &integrity);
575 blk_queue_max_integrity_segments(ns->queue, 1);
578 static void nvme_init_integrity(struct nvme_ns *ns)
581 #endif /* CONFIG_BLK_DEV_INTEGRITY */
583 static void nvme_config_discard(struct nvme_ns *ns)
585 struct nvme_ctrl *ctrl = ns->ctrl;
586 u32 logical_block_size = queue_logical_block_size(ns->queue);
588 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
589 ns->queue->limits.discard_zeroes_data = 1;
591 ns->queue->limits.discard_zeroes_data = 0;
593 ns->queue->limits.discard_alignment = logical_block_size;
594 ns->queue->limits.discard_granularity = logical_block_size;
595 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
596 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
599 static int nvme_revalidate_disk(struct gendisk *disk)
601 struct nvme_ns *ns = disk->private_data;
602 struct nvme_id_ns *id;
607 if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
608 dev_warn(disk_to_dev(ns->disk), "%s: Identify failure\n",
617 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
618 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
619 dev_warn(disk_to_dev(ns->disk),
620 "%s: LightNVM init failure\n", __func__);
624 ns->type = NVME_NS_LIGHTNVM;
627 if (ns->ctrl->vs >= NVME_VS(1, 1))
628 memcpy(ns->eui, id->eui64, sizeof(ns->eui));
629 if (ns->ctrl->vs >= NVME_VS(1, 2))
630 memcpy(ns->uuid, id->nguid, sizeof(ns->uuid));
633 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
634 ns->lba_shift = id->lbaf[lbaf].ds;
635 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
636 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
639 * If identify namespace failed, use default 512 byte block size so
640 * block layer can use before failing read/write for 0 capacity.
642 if (ns->lba_shift == 0)
644 bs = 1 << ns->lba_shift;
645 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
646 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
647 id->dps & NVME_NS_DPS_PI_MASK : 0;
649 blk_mq_freeze_queue(disk->queue);
650 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
652 bs != queue_logical_block_size(disk->queue) ||
653 (ns->ms && ns->ext)))
654 blk_integrity_unregister(disk);
656 ns->pi_type = pi_type;
657 blk_queue_logical_block_size(ns->queue, bs);
659 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
660 nvme_init_integrity(ns);
661 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
662 set_capacity(disk, 0);
664 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
666 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
667 nvme_config_discard(ns);
668 blk_mq_unfreeze_queue(disk->queue);
674 static char nvme_pr_type(enum pr_type type)
677 case PR_WRITE_EXCLUSIVE:
679 case PR_EXCLUSIVE_ACCESS:
681 case PR_WRITE_EXCLUSIVE_REG_ONLY:
683 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
685 case PR_WRITE_EXCLUSIVE_ALL_REGS:
687 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
694 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
695 u64 key, u64 sa_key, u8 op)
697 struct nvme_ns *ns = bdev->bd_disk->private_data;
698 struct nvme_command c;
699 u8 data[16] = { 0, };
701 put_unaligned_le64(key, &data[0]);
702 put_unaligned_le64(sa_key, &data[8]);
704 memset(&c, 0, sizeof(c));
705 c.common.opcode = op;
706 c.common.nsid = cpu_to_le32(ns->ns_id);
707 c.common.cdw10[0] = cpu_to_le32(cdw10);
709 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
712 static int nvme_pr_register(struct block_device *bdev, u64 old,
713 u64 new, unsigned flags)
717 if (flags & ~PR_FL_IGNORE_KEY)
721 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
722 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
723 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
726 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
727 enum pr_type type, unsigned flags)
731 if (flags & ~PR_FL_IGNORE_KEY)
734 cdw10 = nvme_pr_type(type) << 8;
735 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
736 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
739 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
740 enum pr_type type, bool abort)
742 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
743 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
746 static int nvme_pr_clear(struct block_device *bdev, u64 key)
748 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
749 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
752 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
754 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
755 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
758 static const struct pr_ops nvme_pr_ops = {
759 .pr_register = nvme_pr_register,
760 .pr_reserve = nvme_pr_reserve,
761 .pr_release = nvme_pr_release,
762 .pr_preempt = nvme_pr_preempt,
763 .pr_clear = nvme_pr_clear,
766 static const struct block_device_operations nvme_fops = {
767 .owner = THIS_MODULE,
769 .compat_ioctl = nvme_compat_ioctl,
771 .release = nvme_release,
772 .getgeo = nvme_getgeo,
773 .revalidate_disk= nvme_revalidate_disk,
774 .pr_ops = &nvme_pr_ops,
777 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
779 unsigned long timeout =
780 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
781 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
784 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
785 if ((csts & NVME_CSTS_RDY) == bit)
789 if (fatal_signal_pending(current))
791 if (time_after(jiffies, timeout)) {
792 dev_err(ctrl->device,
793 "Device not ready; aborting %s\n", enabled ?
794 "initialisation" : "reset");
803 * If the device has been passed off to us in an enabled state, just clear
804 * the enabled bit. The spec says we should set the 'shutdown notification
805 * bits', but doing so may cause the device to complete commands to the
806 * admin queue ... and we don't know what memory that might be pointing at!
808 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
812 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
813 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
815 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
818 return nvme_wait_ready(ctrl, cap, false);
820 EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
822 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
825 * Default to a 4K page size, with the intention to update this
826 * path in the future to accomodate architectures with differing
827 * kernel and IO page sizes.
829 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
832 if (page_shift < dev_page_min) {
833 dev_err(ctrl->device,
834 "Minimum device page size %u too large for host (%u)\n",
835 1 << dev_page_min, 1 << page_shift);
839 ctrl->page_size = 1 << page_shift;
841 ctrl->ctrl_config = NVME_CC_CSS_NVM;
842 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
843 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
844 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
845 ctrl->ctrl_config |= NVME_CC_ENABLE;
847 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
850 return nvme_wait_ready(ctrl, cap, true);
852 EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
854 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
856 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
860 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
861 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
863 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
867 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
868 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
872 if (fatal_signal_pending(current))
874 if (time_after(jiffies, timeout)) {
875 dev_err(ctrl->device,
876 "Device shutdown incomplete; abort shutdown\n");
883 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
886 * Initialize the cached copies of the Identify data and various controller
887 * register in our nvme_ctrl structure. This should be called as soon as
888 * the admin queue is fully up and running.
890 int nvme_init_identify(struct nvme_ctrl *ctrl)
892 struct nvme_id_ctrl *id;
896 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
898 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
902 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
904 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
907 page_shift = NVME_CAP_MPSMIN(cap) + 12;
909 if (ctrl->vs >= NVME_VS(1, 1))
910 ctrl->subsystem = NVME_CAP_NSSRC(cap);
912 ret = nvme_identify_ctrl(ctrl, &id);
914 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
918 ctrl->vid = le16_to_cpu(id->vid);
919 ctrl->oncs = le16_to_cpup(&id->oncs);
920 atomic_set(&ctrl->abort_limit, id->acl + 1);
922 ctrl->cntlid = le16_to_cpup(&id->cntlid);
923 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
924 memcpy(ctrl->model, id->mn, sizeof(id->mn));
925 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
927 ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9);
929 ctrl->max_hw_sectors = UINT_MAX;
931 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
932 unsigned int max_hw_sectors;
934 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
935 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
936 if (ctrl->max_hw_sectors) {
937 ctrl->max_hw_sectors = min(max_hw_sectors,
938 ctrl->max_hw_sectors);
940 ctrl->max_hw_sectors = max_hw_sectors;
947 EXPORT_SYMBOL_GPL(nvme_init_identify);
949 static int nvme_dev_open(struct inode *inode, struct file *file)
951 struct nvme_ctrl *ctrl;
952 int instance = iminor(inode);
955 spin_lock(&dev_list_lock);
956 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
957 if (ctrl->instance != instance)
960 if (!ctrl->admin_q) {
964 if (!kref_get_unless_zero(&ctrl->kref))
966 file->private_data = ctrl;
970 spin_unlock(&dev_list_lock);
975 static int nvme_dev_release(struct inode *inode, struct file *file)
977 nvme_put_ctrl(file->private_data);
981 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
986 mutex_lock(&ctrl->namespaces_mutex);
987 if (list_empty(&ctrl->namespaces)) {
992 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
993 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
994 dev_warn(ctrl->device,
995 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1000 dev_warn(ctrl->device,
1001 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1002 kref_get(&ns->kref);
1003 mutex_unlock(&ctrl->namespaces_mutex);
1005 ret = nvme_user_cmd(ctrl, ns, argp);
1010 mutex_unlock(&ctrl->namespaces_mutex);
1014 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1017 struct nvme_ctrl *ctrl = file->private_data;
1018 void __user *argp = (void __user *)arg;
1021 case NVME_IOCTL_ADMIN_CMD:
1022 return nvme_user_cmd(ctrl, NULL, argp);
1023 case NVME_IOCTL_IO_CMD:
1024 return nvme_dev_user_cmd(ctrl, argp);
1025 case NVME_IOCTL_RESET:
1026 dev_warn(ctrl->device, "resetting controller\n");
1027 return ctrl->ops->reset_ctrl(ctrl);
1028 case NVME_IOCTL_SUBSYS_RESET:
1029 return nvme_reset_subsystem(ctrl);
1035 static const struct file_operations nvme_dev_fops = {
1036 .owner = THIS_MODULE,
1037 .open = nvme_dev_open,
1038 .release = nvme_dev_release,
1039 .unlocked_ioctl = nvme_dev_ioctl,
1040 .compat_ioctl = nvme_dev_ioctl,
1043 static ssize_t nvme_sysfs_reset(struct device *dev,
1044 struct device_attribute *attr, const char *buf,
1047 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1050 ret = ctrl->ops->reset_ctrl(ctrl);
1055 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1057 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1060 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1061 struct nvme_ctrl *ctrl = ns->ctrl;
1062 int serial_len = sizeof(ctrl->serial);
1063 int model_len = sizeof(ctrl->model);
1065 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1066 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1068 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1069 return sprintf(buf, "eui.%8phN\n", ns->eui);
1071 while (ctrl->serial[serial_len - 1] == ' ')
1073 while (ctrl->model[model_len - 1] == ' ')
1076 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1077 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1079 static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1081 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1084 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1085 return sprintf(buf, "%pU\n", ns->uuid);
1087 static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1089 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1092 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1093 return sprintf(buf, "%8phd\n", ns->eui);
1095 static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1097 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1100 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1101 return sprintf(buf, "%d\n", ns->ns_id);
1103 static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1105 static struct attribute *nvme_ns_attrs[] = {
1106 &dev_attr_wwid.attr,
1107 &dev_attr_uuid.attr,
1109 &dev_attr_nsid.attr,
1113 static umode_t nvme_attrs_are_visible(struct kobject *kobj,
1114 struct attribute *a, int n)
1116 struct device *dev = container_of(kobj, struct device, kobj);
1117 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1119 if (a == &dev_attr_uuid.attr) {
1120 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1123 if (a == &dev_attr_eui.attr) {
1124 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1130 static const struct attribute_group nvme_ns_attr_group = {
1131 .attrs = nvme_ns_attrs,
1132 .is_visible = nvme_attrs_are_visible,
1135 #define nvme_show_str_function(field) \
1136 static ssize_t field##_show(struct device *dev, \
1137 struct device_attribute *attr, char *buf) \
1139 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1140 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1142 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1144 #define nvme_show_int_function(field) \
1145 static ssize_t field##_show(struct device *dev, \
1146 struct device_attribute *attr, char *buf) \
1148 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1149 return sprintf(buf, "%d\n", ctrl->field); \
1151 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1153 nvme_show_str_function(model);
1154 nvme_show_str_function(serial);
1155 nvme_show_str_function(firmware_rev);
1156 nvme_show_int_function(cntlid);
1158 static struct attribute *nvme_dev_attrs[] = {
1159 &dev_attr_reset_controller.attr,
1160 &dev_attr_model.attr,
1161 &dev_attr_serial.attr,
1162 &dev_attr_firmware_rev.attr,
1163 &dev_attr_cntlid.attr,
1167 static struct attribute_group nvme_dev_attrs_group = {
1168 .attrs = nvme_dev_attrs,
1171 static const struct attribute_group *nvme_dev_attr_groups[] = {
1172 &nvme_dev_attrs_group,
1176 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1178 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1179 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1181 return nsa->ns_id - nsb->ns_id;
1184 static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1188 lockdep_assert_held(&ctrl->namespaces_mutex);
1190 list_for_each_entry(ns, &ctrl->namespaces, list) {
1191 if (ns->ns_id == nsid)
1193 if (ns->ns_id > nsid)
1199 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1202 struct gendisk *disk;
1203 int node = dev_to_node(ctrl->dev);
1205 lockdep_assert_held(&ctrl->namespaces_mutex);
1207 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1211 ns->queue = blk_mq_init_queue(ctrl->tagset);
1212 if (IS_ERR(ns->queue))
1214 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1215 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1216 ns->queue->queuedata = ns;
1219 disk = alloc_disk_node(0, node);
1221 goto out_free_queue;
1223 kref_init(&ns->kref);
1226 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1228 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1229 if (ctrl->max_hw_sectors) {
1230 blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors);
1231 blk_queue_max_segments(ns->queue,
1232 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1);
1234 if (ctrl->stripe_size)
1235 blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9);
1236 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1237 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
1238 blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1);
1240 disk->major = nvme_major;
1241 disk->first_minor = 0;
1242 disk->fops = &nvme_fops;
1243 disk->private_data = ns;
1244 disk->queue = ns->queue;
1245 disk->driverfs_dev = ctrl->device;
1246 disk->flags = GENHD_FL_EXT_DEVT;
1247 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, nsid);
1249 if (nvme_revalidate_disk(ns->disk))
1252 list_add_tail(&ns->list, &ctrl->namespaces);
1253 kref_get(&ctrl->kref);
1254 if (ns->type == NVME_NS_LIGHTNVM)
1258 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1259 &nvme_ns_attr_group))
1260 pr_warn("%s: failed to create sysfs group for identification\n",
1261 ns->disk->disk_name);
1266 blk_cleanup_queue(ns->queue);
1271 static void nvme_ns_remove(struct nvme_ns *ns)
1273 bool kill = nvme_io_incapable(ns->ctrl) &&
1274 !blk_queue_dying(ns->queue);
1276 lockdep_assert_held(&ns->ctrl->namespaces_mutex);
1279 blk_set_queue_dying(ns->queue);
1282 * The controller was shutdown first if we got here through
1283 * device removal. The shutdown may requeue outstanding
1284 * requests. These need to be aborted immediately so
1285 * del_gendisk doesn't block indefinitely for their completion.
1287 blk_mq_abort_requeue_list(ns->queue);
1289 if (ns->disk->flags & GENHD_FL_UP) {
1290 if (blk_get_integrity(ns->disk))
1291 blk_integrity_unregister(ns->disk);
1292 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1293 &nvme_ns_attr_group);
1294 del_gendisk(ns->disk);
1296 if (kill || !blk_queue_dying(ns->queue)) {
1297 blk_mq_abort_requeue_list(ns->queue);
1298 blk_cleanup_queue(ns->queue);
1300 list_del_init(&ns->list);
1304 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1308 ns = nvme_find_ns(ctrl, nsid);
1310 if (revalidate_disk(ns->disk))
1313 nvme_alloc_ns(ctrl, nsid);
1316 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1320 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1323 ns_list = kzalloc(0x1000, GFP_KERNEL);
1327 for (i = 0; i < num_lists; i++) {
1328 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1332 for (j = 0; j < min(nn, 1024U); j++) {
1333 nsid = le32_to_cpu(ns_list[j]);
1337 nvme_validate_ns(ctrl, nsid);
1339 while (++prev < nsid) {
1340 ns = nvme_find_ns(ctrl, prev);
1352 static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn)
1354 struct nvme_ns *ns, *next;
1357 lockdep_assert_held(&ctrl->namespaces_mutex);
1359 for (i = 1; i <= nn; i++)
1360 nvme_validate_ns(ctrl, i);
1362 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1368 void nvme_scan_namespaces(struct nvme_ctrl *ctrl)
1370 struct nvme_id_ctrl *id;
1373 if (nvme_identify_ctrl(ctrl, &id))
1376 mutex_lock(&ctrl->namespaces_mutex);
1377 nn = le32_to_cpu(id->nn);
1378 if (ctrl->vs >= NVME_VS(1, 1) &&
1379 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1380 if (!nvme_scan_ns_list(ctrl, nn))
1383 __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn));
1385 list_sort(NULL, &ctrl->namespaces, ns_cmp);
1386 mutex_unlock(&ctrl->namespaces_mutex);
1389 EXPORT_SYMBOL_GPL(nvme_scan_namespaces);
1391 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1393 struct nvme_ns *ns, *next;
1395 mutex_lock(&ctrl->namespaces_mutex);
1396 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1398 mutex_unlock(&ctrl->namespaces_mutex);
1400 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
1402 static DEFINE_IDA(nvme_instance_ida);
1404 static int nvme_set_instance(struct nvme_ctrl *ctrl)
1406 int instance, error;
1409 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1412 spin_lock(&dev_list_lock);
1413 error = ida_get_new(&nvme_instance_ida, &instance);
1414 spin_unlock(&dev_list_lock);
1415 } while (error == -EAGAIN);
1420 ctrl->instance = instance;
1424 static void nvme_release_instance(struct nvme_ctrl *ctrl)
1426 spin_lock(&dev_list_lock);
1427 ida_remove(&nvme_instance_ida, ctrl->instance);
1428 spin_unlock(&dev_list_lock);
1431 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
1433 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
1435 spin_lock(&dev_list_lock);
1436 list_del(&ctrl->node);
1437 spin_unlock(&dev_list_lock);
1439 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
1441 static void nvme_free_ctrl(struct kref *kref)
1443 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
1445 put_device(ctrl->device);
1446 nvme_release_instance(ctrl);
1448 ctrl->ops->free_ctrl(ctrl);
1451 void nvme_put_ctrl(struct nvme_ctrl *ctrl)
1453 kref_put(&ctrl->kref, nvme_free_ctrl);
1455 EXPORT_SYMBOL_GPL(nvme_put_ctrl);
1458 * Initialize a NVMe controller structures. This needs to be called during
1459 * earliest initialization so that we have the initialized structured around
1462 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
1463 const struct nvme_ctrl_ops *ops, unsigned long quirks)
1467 INIT_LIST_HEAD(&ctrl->namespaces);
1468 mutex_init(&ctrl->namespaces_mutex);
1469 kref_init(&ctrl->kref);
1472 ctrl->quirks = quirks;
1474 ret = nvme_set_instance(ctrl);
1478 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
1479 MKDEV(nvme_char_major, ctrl->instance),
1480 ctrl, nvme_dev_attr_groups,
1481 "nvme%d", ctrl->instance);
1482 if (IS_ERR(ctrl->device)) {
1483 ret = PTR_ERR(ctrl->device);
1484 goto out_release_instance;
1486 get_device(ctrl->device);
1488 spin_lock(&dev_list_lock);
1489 list_add_tail(&ctrl->node, &nvme_ctrl_list);
1490 spin_unlock(&dev_list_lock);
1493 out_release_instance:
1494 nvme_release_instance(ctrl);
1498 EXPORT_SYMBOL_GPL(nvme_init_ctrl);
1500 void nvme_stop_queues(struct nvme_ctrl *ctrl)
1504 mutex_lock(&ctrl->namespaces_mutex);
1505 list_for_each_entry(ns, &ctrl->namespaces, list) {
1506 spin_lock_irq(ns->queue->queue_lock);
1507 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
1508 spin_unlock_irq(ns->queue->queue_lock);
1510 blk_mq_cancel_requeue_work(ns->queue);
1511 blk_mq_stop_hw_queues(ns->queue);
1513 mutex_unlock(&ctrl->namespaces_mutex);
1515 EXPORT_SYMBOL_GPL(nvme_stop_queues);
1517 void nvme_start_queues(struct nvme_ctrl *ctrl)
1521 mutex_lock(&ctrl->namespaces_mutex);
1522 list_for_each_entry(ns, &ctrl->namespaces, list) {
1523 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
1524 blk_mq_start_stopped_hw_queues(ns->queue, true);
1525 blk_mq_kick_requeue_list(ns->queue);
1527 mutex_unlock(&ctrl->namespaces_mutex);
1529 EXPORT_SYMBOL_GPL(nvme_start_queues);
1531 int __init nvme_core_init(void)
1535 result = register_blkdev(nvme_major, "nvme");
1538 else if (result > 0)
1539 nvme_major = result;
1541 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
1544 goto unregister_blkdev;
1545 else if (result > 0)
1546 nvme_char_major = result;
1548 nvme_class = class_create(THIS_MODULE, "nvme");
1549 if (IS_ERR(nvme_class)) {
1550 result = PTR_ERR(nvme_class);
1551 goto unregister_chrdev;
1557 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1559 unregister_blkdev(nvme_major, "nvme");
1563 void nvme_core_exit(void)
1565 unregister_blkdev(nvme_major, "nvme");
1566 class_destroy(nvme_class);
1567 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1570 MODULE_LICENSE("GPL");
1571 MODULE_VERSION("1.0");
1572 module_init(nvme_core_init);
1573 module_exit(nvme_core_exit);