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>
29 #include <linux/pm_qos.h>
31 #include <asm/unaligned.h>
36 #define NVME_MINORS (1U << MINORBITS)
38 unsigned char admin_timeout = 60;
39 module_param(admin_timeout, byte, 0644);
40 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
41 EXPORT_SYMBOL_GPL(admin_timeout);
43 unsigned char nvme_io_timeout = 30;
44 module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
45 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
46 EXPORT_SYMBOL_GPL(nvme_io_timeout);
48 unsigned char shutdown_timeout = 5;
49 module_param(shutdown_timeout, byte, 0644);
50 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
52 static u8 nvme_max_retries = 5;
53 module_param_named(max_retries, nvme_max_retries, byte, 0644);
54 MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
56 static int nvme_char_major;
57 module_param(nvme_char_major, int, 0);
59 static unsigned long default_ps_max_latency_us = 25000;
60 module_param(default_ps_max_latency_us, ulong, 0644);
61 MODULE_PARM_DESC(default_ps_max_latency_us,
62 "max power saving latency for new devices; use PM QOS to change per device");
64 static LIST_HEAD(nvme_ctrl_list);
65 static DEFINE_SPINLOCK(dev_list_lock);
67 static struct class *nvme_class;
69 int nvme_error_status(struct request *req)
71 switch (nvme_req(req)->status & 0x7ff) {
74 case NVME_SC_CAP_EXCEEDED:
80 EXPORT_SYMBOL_GPL(nvme_error_status);
82 static inline bool nvme_req_needs_retry(struct request *req)
84 if (blk_noretry_request(req))
86 if (nvme_req(req)->status & NVME_SC_DNR)
88 if (jiffies - req->start_time >= req->timeout)
90 if (nvme_req(req)->retries >= nvme_max_retries)
95 void nvme_complete_rq(struct request *req)
97 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
98 nvme_req(req)->retries++;
99 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
103 blk_mq_end_request(req, nvme_error_status(req));
105 EXPORT_SYMBOL_GPL(nvme_complete_rq);
107 void nvme_cancel_request(struct request *req, void *data, bool reserved)
111 if (!blk_mq_request_started(req))
114 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
115 "Cancelling I/O %d", req->tag);
117 status = NVME_SC_ABORT_REQ;
118 if (blk_queue_dying(req->q))
119 status |= NVME_SC_DNR;
120 nvme_req(req)->status = status;
121 blk_mq_complete_request(req, 0);
124 EXPORT_SYMBOL_GPL(nvme_cancel_request);
126 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
127 enum nvme_ctrl_state new_state)
129 enum nvme_ctrl_state old_state;
130 bool changed = false;
132 spin_lock_irq(&ctrl->lock);
134 old_state = ctrl->state;
139 case NVME_CTRL_RESETTING:
140 case NVME_CTRL_RECONNECTING:
147 case NVME_CTRL_RESETTING:
151 case NVME_CTRL_RECONNECTING:
158 case NVME_CTRL_RECONNECTING:
167 case NVME_CTRL_DELETING:
170 case NVME_CTRL_RESETTING:
171 case NVME_CTRL_RECONNECTING:
180 case NVME_CTRL_DELETING:
192 ctrl->state = new_state;
194 spin_unlock_irq(&ctrl->lock);
198 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
200 static void nvme_free_ns(struct kref *kref)
202 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
205 nvme_nvm_unregister(ns);
208 spin_lock(&dev_list_lock);
209 ns->disk->private_data = NULL;
210 spin_unlock(&dev_list_lock);
214 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
215 nvme_put_ctrl(ns->ctrl);
219 static void nvme_put_ns(struct nvme_ns *ns)
221 kref_put(&ns->kref, nvme_free_ns);
224 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
228 spin_lock(&dev_list_lock);
229 ns = disk->private_data;
231 if (!kref_get_unless_zero(&ns->kref))
233 if (!try_module_get(ns->ctrl->ops->module))
236 spin_unlock(&dev_list_lock);
241 kref_put(&ns->kref, nvme_free_ns);
243 spin_unlock(&dev_list_lock);
247 struct request *nvme_alloc_request(struct request_queue *q,
248 struct nvme_command *cmd, unsigned int flags, int qid)
250 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
253 if (qid == NVME_QID_ANY) {
254 req = blk_mq_alloc_request(q, op, flags);
256 req = blk_mq_alloc_request_hctx(q, op, flags,
262 req->cmd_flags |= REQ_FAILFAST_DRIVER;
263 nvme_req(req)->cmd = cmd;
267 EXPORT_SYMBOL_GPL(nvme_alloc_request);
269 static inline void nvme_setup_flush(struct nvme_ns *ns,
270 struct nvme_command *cmnd)
272 memset(cmnd, 0, sizeof(*cmnd));
273 cmnd->common.opcode = nvme_cmd_flush;
274 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
277 static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
278 struct nvme_command *cmnd)
280 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
281 struct nvme_dsm_range *range;
284 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
286 return BLK_MQ_RQ_QUEUE_BUSY;
288 __rq_for_each_bio(bio, req) {
289 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
290 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
292 range[n].cattr = cpu_to_le32(0);
293 range[n].nlb = cpu_to_le32(nlb);
294 range[n].slba = cpu_to_le64(slba);
298 if (WARN_ON_ONCE(n != segments)) {
300 return BLK_MQ_RQ_QUEUE_ERROR;
303 memset(cmnd, 0, sizeof(*cmnd));
304 cmnd->dsm.opcode = nvme_cmd_dsm;
305 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
306 cmnd->dsm.nr = cpu_to_le32(segments - 1);
307 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
309 req->special_vec.bv_page = virt_to_page(range);
310 req->special_vec.bv_offset = offset_in_page(range);
311 req->special_vec.bv_len = sizeof(*range) * segments;
312 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
314 return BLK_MQ_RQ_QUEUE_OK;
317 static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
318 struct nvme_command *cmnd)
323 if (req->cmd_flags & REQ_FUA)
324 control |= NVME_RW_FUA;
325 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
326 control |= NVME_RW_LR;
328 if (req->cmd_flags & REQ_RAHEAD)
329 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
331 memset(cmnd, 0, sizeof(*cmnd));
332 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
333 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
334 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
335 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
338 switch (ns->pi_type) {
339 case NVME_NS_DPS_PI_TYPE3:
340 control |= NVME_RW_PRINFO_PRCHK_GUARD;
342 case NVME_NS_DPS_PI_TYPE1:
343 case NVME_NS_DPS_PI_TYPE2:
344 control |= NVME_RW_PRINFO_PRCHK_GUARD |
345 NVME_RW_PRINFO_PRCHK_REF;
346 cmnd->rw.reftag = cpu_to_le32(
347 nvme_block_nr(ns, blk_rq_pos(req)));
350 if (!blk_integrity_rq(req))
351 control |= NVME_RW_PRINFO_PRACT;
354 cmnd->rw.control = cpu_to_le16(control);
355 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
358 int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
359 struct nvme_command *cmd)
361 int ret = BLK_MQ_RQ_QUEUE_OK;
363 if (!(req->rq_flags & RQF_DONTPREP)) {
364 nvme_req(req)->retries = 0;
365 nvme_req(req)->flags = 0;
366 req->rq_flags |= RQF_DONTPREP;
369 switch (req_op(req)) {
372 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
375 nvme_setup_flush(ns, cmd);
377 case REQ_OP_WRITE_ZEROES:
378 /* currently only aliased to deallocate for a few ctrls: */
380 ret = nvme_setup_discard(ns, req, cmd);
384 nvme_setup_rw(ns, req, cmd);
388 return BLK_MQ_RQ_QUEUE_ERROR;
391 cmd->common.command_id = req->tag;
394 EXPORT_SYMBOL_GPL(nvme_setup_cmd);
397 * Returns 0 on success. If the result is negative, it's a Linux error code;
398 * if the result is positive, it's an NVM Express status code
400 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
401 union nvme_result *result, void *buffer, unsigned bufflen,
402 unsigned timeout, int qid, int at_head, int flags)
407 req = nvme_alloc_request(q, cmd, flags, qid);
411 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
413 if (buffer && bufflen) {
414 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
419 blk_execute_rq(req->q, NULL, req, at_head);
421 *result = nvme_req(req)->result;
422 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
425 ret = nvme_req(req)->status;
427 blk_mq_free_request(req);
430 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
432 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
433 void *buffer, unsigned bufflen)
435 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
438 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
440 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
441 void __user *ubuffer, unsigned bufflen,
442 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
443 u32 *result, unsigned timeout)
445 bool write = nvme_is_write(cmd);
446 struct nvme_ns *ns = q->queuedata;
447 struct gendisk *disk = ns ? ns->disk : NULL;
449 struct bio *bio = NULL;
453 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
457 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
459 if (ubuffer && bufflen) {
460 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
468 bio->bi_bdev = bdget_disk(disk, 0);
474 if (meta_buffer && meta_len) {
475 struct bio_integrity_payload *bip;
477 meta = kmalloc(meta_len, GFP_KERNEL);
484 if (copy_from_user(meta, meta_buffer,
491 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
497 bip->bip_iter.bi_size = meta_len;
498 bip->bip_iter.bi_sector = meta_seed;
500 ret = bio_integrity_add_page(bio, virt_to_page(meta),
501 meta_len, offset_in_page(meta));
502 if (ret != meta_len) {
509 blk_execute_rq(req->q, disk, req, 0);
510 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
513 ret = nvme_req(req)->status;
515 *result = le32_to_cpu(nvme_req(req)->result.u32);
516 if (meta && !ret && !write) {
517 if (copy_to_user(meta_buffer, meta, meta_len))
524 if (disk && bio->bi_bdev)
526 blk_rq_unmap_user(bio);
529 blk_mq_free_request(req);
533 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
534 void __user *ubuffer, unsigned bufflen, u32 *result,
537 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
541 static void nvme_keep_alive_end_io(struct request *rq, int error)
543 struct nvme_ctrl *ctrl = rq->end_io_data;
545 blk_mq_free_request(rq);
548 dev_err(ctrl->device,
549 "failed nvme_keep_alive_end_io error=%d\n", error);
553 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
556 static int nvme_keep_alive(struct nvme_ctrl *ctrl)
558 struct nvme_command c;
561 memset(&c, 0, sizeof(c));
562 c.common.opcode = nvme_admin_keep_alive;
564 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
569 rq->timeout = ctrl->kato * HZ;
570 rq->end_io_data = ctrl;
572 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
577 static void nvme_keep_alive_work(struct work_struct *work)
579 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
580 struct nvme_ctrl, ka_work);
582 if (nvme_keep_alive(ctrl)) {
583 /* allocation failure, reset the controller */
584 dev_err(ctrl->device, "keep-alive failed\n");
585 ctrl->ops->reset_ctrl(ctrl);
590 void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
592 if (unlikely(ctrl->kato == 0))
595 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
596 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
598 EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
600 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
602 if (unlikely(ctrl->kato == 0))
605 cancel_delayed_work_sync(&ctrl->ka_work);
607 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
609 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
611 struct nvme_command c = { };
614 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
615 c.identify.opcode = nvme_admin_identify;
616 c.identify.cns = NVME_ID_CNS_CTRL;
618 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
622 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
623 sizeof(struct nvme_id_ctrl));
629 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
631 struct nvme_command c = { };
633 c.identify.opcode = nvme_admin_identify;
634 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
635 c.identify.nsid = cpu_to_le32(nsid);
636 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
639 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
640 struct nvme_id_ns **id)
642 struct nvme_command c = { };
645 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
646 c.identify.opcode = nvme_admin_identify;
647 c.identify.nsid = cpu_to_le32(nsid);
648 c.identify.cns = NVME_ID_CNS_NS;
650 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
654 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
655 sizeof(struct nvme_id_ns));
661 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
662 void *buffer, size_t buflen, u32 *result)
664 struct nvme_command c;
665 union nvme_result res;
668 memset(&c, 0, sizeof(c));
669 c.features.opcode = nvme_admin_get_features;
670 c.features.nsid = cpu_to_le32(nsid);
671 c.features.fid = cpu_to_le32(fid);
673 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
675 if (ret >= 0 && result)
676 *result = le32_to_cpu(res.u32);
680 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
681 void *buffer, size_t buflen, u32 *result)
683 struct nvme_command c;
684 union nvme_result res;
687 memset(&c, 0, sizeof(c));
688 c.features.opcode = nvme_admin_set_features;
689 c.features.fid = cpu_to_le32(fid);
690 c.features.dword11 = cpu_to_le32(dword11);
692 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
693 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
694 if (ret >= 0 && result)
695 *result = le32_to_cpu(res.u32);
699 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
701 struct nvme_command c = { };
704 c.common.opcode = nvme_admin_get_log_page,
705 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
706 c.common.cdw10[0] = cpu_to_le32(
707 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
710 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
714 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
715 sizeof(struct nvme_smart_log));
721 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
723 u32 q_count = (*count - 1) | ((*count - 1) << 16);
725 int status, nr_io_queues;
727 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
733 * Degraded controllers might return an error when setting the queue
734 * count. We still want to be able to bring them online and offer
735 * access to the admin queue, as that might be only way to fix them up.
738 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
741 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
742 *count = min(*count, nr_io_queues);
747 EXPORT_SYMBOL_GPL(nvme_set_queue_count);
749 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
751 struct nvme_user_io io;
752 struct nvme_command c;
753 unsigned length, meta_len;
754 void __user *metadata;
756 if (copy_from_user(&io, uio, sizeof(io)))
764 case nvme_cmd_compare:
770 length = (io.nblocks + 1) << ns->lba_shift;
771 meta_len = (io.nblocks + 1) * ns->ms;
772 metadata = (void __user *)(uintptr_t)io.metadata;
777 } else if (meta_len) {
778 if ((io.metadata & 3) || !io.metadata)
782 memset(&c, 0, sizeof(c));
783 c.rw.opcode = io.opcode;
784 c.rw.flags = io.flags;
785 c.rw.nsid = cpu_to_le32(ns->ns_id);
786 c.rw.slba = cpu_to_le64(io.slba);
787 c.rw.length = cpu_to_le16(io.nblocks);
788 c.rw.control = cpu_to_le16(io.control);
789 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
790 c.rw.reftag = cpu_to_le32(io.reftag);
791 c.rw.apptag = cpu_to_le16(io.apptag);
792 c.rw.appmask = cpu_to_le16(io.appmask);
794 return __nvme_submit_user_cmd(ns->queue, &c,
795 (void __user *)(uintptr_t)io.addr, length,
796 metadata, meta_len, io.slba, NULL, 0);
799 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
800 struct nvme_passthru_cmd __user *ucmd)
802 struct nvme_passthru_cmd cmd;
803 struct nvme_command c;
804 unsigned timeout = 0;
807 if (!capable(CAP_SYS_ADMIN))
809 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
814 memset(&c, 0, sizeof(c));
815 c.common.opcode = cmd.opcode;
816 c.common.flags = cmd.flags;
817 c.common.nsid = cpu_to_le32(cmd.nsid);
818 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
819 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
820 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
821 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
822 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
823 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
824 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
825 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
828 timeout = msecs_to_jiffies(cmd.timeout_ms);
830 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
831 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
832 &cmd.result, timeout);
834 if (put_user(cmd.result, &ucmd->result))
841 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
842 unsigned int cmd, unsigned long arg)
844 struct nvme_ns *ns = bdev->bd_disk->private_data;
848 force_successful_syscall_return();
850 case NVME_IOCTL_ADMIN_CMD:
851 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
852 case NVME_IOCTL_IO_CMD:
853 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
854 case NVME_IOCTL_SUBMIT_IO:
855 return nvme_submit_io(ns, (void __user *)arg);
856 #ifdef CONFIG_BLK_DEV_NVME_SCSI
857 case SG_GET_VERSION_NUM:
858 return nvme_sg_get_version_num((void __user *)arg);
860 return nvme_sg_io(ns, (void __user *)arg);
865 return nvme_nvm_ioctl(ns, cmd, arg);
867 if (is_sed_ioctl(cmd))
868 return sed_ioctl(ns->ctrl->opal_dev, cmd,
869 (void __user *) arg);
875 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
876 unsigned int cmd, unsigned long arg)
882 return nvme_ioctl(bdev, mode, cmd, arg);
885 #define nvme_compat_ioctl NULL
888 static int nvme_open(struct block_device *bdev, fmode_t mode)
890 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
893 static void nvme_release(struct gendisk *disk, fmode_t mode)
895 struct nvme_ns *ns = disk->private_data;
897 module_put(ns->ctrl->ops->module);
901 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
903 /* some standard values */
905 geo->sectors = 1 << 5;
906 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
910 #ifdef CONFIG_BLK_DEV_INTEGRITY
911 static void nvme_init_integrity(struct nvme_ns *ns)
913 struct blk_integrity integrity;
915 memset(&integrity, 0, sizeof(integrity));
916 switch (ns->pi_type) {
917 case NVME_NS_DPS_PI_TYPE3:
918 integrity.profile = &t10_pi_type3_crc;
919 integrity.tag_size = sizeof(u16) + sizeof(u32);
920 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
922 case NVME_NS_DPS_PI_TYPE1:
923 case NVME_NS_DPS_PI_TYPE2:
924 integrity.profile = &t10_pi_type1_crc;
925 integrity.tag_size = sizeof(u16);
926 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
929 integrity.profile = NULL;
932 integrity.tuple_size = ns->ms;
933 blk_integrity_register(ns->disk, &integrity);
934 blk_queue_max_integrity_segments(ns->queue, 1);
937 static void nvme_init_integrity(struct nvme_ns *ns)
940 #endif /* CONFIG_BLK_DEV_INTEGRITY */
942 static void nvme_config_discard(struct nvme_ns *ns)
944 struct nvme_ctrl *ctrl = ns->ctrl;
945 u32 logical_block_size = queue_logical_block_size(ns->queue);
947 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
948 NVME_DSM_MAX_RANGES);
950 ns->queue->limits.discard_alignment = logical_block_size;
951 ns->queue->limits.discard_granularity = logical_block_size;
952 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
953 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
954 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
956 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
957 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
960 static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
962 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
963 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
967 if ((*id)->ncap == 0) {
972 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
973 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
974 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
975 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
980 static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
982 struct nvme_ns *ns = disk->private_data;
988 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
989 ns->lba_shift = id->lbaf[lbaf].ds;
990 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
991 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
994 * If identify namespace failed, use default 512 byte block size so
995 * block layer can use before failing read/write for 0 capacity.
997 if (ns->lba_shift == 0)
999 bs = 1 << ns->lba_shift;
1000 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
1001 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
1002 id->dps & NVME_NS_DPS_PI_MASK : 0;
1004 blk_mq_freeze_queue(disk->queue);
1005 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
1007 bs != queue_logical_block_size(disk->queue) ||
1008 (ns->ms && ns->ext)))
1009 blk_integrity_unregister(disk);
1011 ns->pi_type = pi_type;
1012 blk_queue_logical_block_size(ns->queue, bs);
1014 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
1015 nvme_init_integrity(ns);
1016 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1017 set_capacity(disk, 0);
1019 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1021 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1022 nvme_config_discard(ns);
1023 blk_mq_unfreeze_queue(disk->queue);
1026 static int nvme_revalidate_disk(struct gendisk *disk)
1028 struct nvme_ns *ns = disk->private_data;
1029 struct nvme_id_ns *id = NULL;
1032 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1033 set_capacity(disk, 0);
1037 ret = nvme_revalidate_ns(ns, &id);
1041 __nvme_revalidate_disk(disk, id);
1047 static char nvme_pr_type(enum pr_type type)
1050 case PR_WRITE_EXCLUSIVE:
1052 case PR_EXCLUSIVE_ACCESS:
1054 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1056 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1058 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1060 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1067 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1068 u64 key, u64 sa_key, u8 op)
1070 struct nvme_ns *ns = bdev->bd_disk->private_data;
1071 struct nvme_command c;
1072 u8 data[16] = { 0, };
1074 put_unaligned_le64(key, &data[0]);
1075 put_unaligned_le64(sa_key, &data[8]);
1077 memset(&c, 0, sizeof(c));
1078 c.common.opcode = op;
1079 c.common.nsid = cpu_to_le32(ns->ns_id);
1080 c.common.cdw10[0] = cpu_to_le32(cdw10);
1082 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1085 static int nvme_pr_register(struct block_device *bdev, u64 old,
1086 u64 new, unsigned flags)
1090 if (flags & ~PR_FL_IGNORE_KEY)
1093 cdw10 = old ? 2 : 0;
1094 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1095 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1096 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1099 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1100 enum pr_type type, unsigned flags)
1104 if (flags & ~PR_FL_IGNORE_KEY)
1107 cdw10 = nvme_pr_type(type) << 8;
1108 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1109 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1112 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1113 enum pr_type type, bool abort)
1115 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1116 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1119 static int nvme_pr_clear(struct block_device *bdev, u64 key)
1121 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
1122 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1125 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1127 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1128 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1131 static const struct pr_ops nvme_pr_ops = {
1132 .pr_register = nvme_pr_register,
1133 .pr_reserve = nvme_pr_reserve,
1134 .pr_release = nvme_pr_release,
1135 .pr_preempt = nvme_pr_preempt,
1136 .pr_clear = nvme_pr_clear,
1139 #ifdef CONFIG_BLK_SED_OPAL
1140 int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1143 struct nvme_ctrl *ctrl = data;
1144 struct nvme_command cmd;
1146 memset(&cmd, 0, sizeof(cmd));
1148 cmd.common.opcode = nvme_admin_security_send;
1150 cmd.common.opcode = nvme_admin_security_recv;
1151 cmd.common.nsid = 0;
1152 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1153 cmd.common.cdw10[1] = cpu_to_le32(len);
1155 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1156 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1158 EXPORT_SYMBOL_GPL(nvme_sec_submit);
1159 #endif /* CONFIG_BLK_SED_OPAL */
1161 static const struct block_device_operations nvme_fops = {
1162 .owner = THIS_MODULE,
1163 .ioctl = nvme_ioctl,
1164 .compat_ioctl = nvme_compat_ioctl,
1166 .release = nvme_release,
1167 .getgeo = nvme_getgeo,
1168 .revalidate_disk= nvme_revalidate_disk,
1169 .pr_ops = &nvme_pr_ops,
1172 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1174 unsigned long timeout =
1175 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1176 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1179 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1182 if ((csts & NVME_CSTS_RDY) == bit)
1186 if (fatal_signal_pending(current))
1188 if (time_after(jiffies, timeout)) {
1189 dev_err(ctrl->device,
1190 "Device not ready; aborting %s\n", enabled ?
1191 "initialisation" : "reset");
1200 * If the device has been passed off to us in an enabled state, just clear
1201 * the enabled bit. The spec says we should set the 'shutdown notification
1202 * bits', but doing so may cause the device to complete commands to the
1203 * admin queue ... and we don't know what memory that might be pointing at!
1205 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1209 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1210 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1212 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1216 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
1217 msleep(NVME_QUIRK_DELAY_AMOUNT);
1219 return nvme_wait_ready(ctrl, cap, false);
1221 EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
1223 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1226 * Default to a 4K page size, with the intention to update this
1227 * path in the future to accomodate architectures with differing
1228 * kernel and IO page sizes.
1230 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1233 if (page_shift < dev_page_min) {
1234 dev_err(ctrl->device,
1235 "Minimum device page size %u too large for host (%u)\n",
1236 1 << dev_page_min, 1 << page_shift);
1240 ctrl->page_size = 1 << page_shift;
1242 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1243 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1244 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1245 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1246 ctrl->ctrl_config |= NVME_CC_ENABLE;
1248 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1251 return nvme_wait_ready(ctrl, cap, true);
1253 EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
1255 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1257 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1261 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1262 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1264 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1268 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1269 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1273 if (fatal_signal_pending(current))
1275 if (time_after(jiffies, timeout)) {
1276 dev_err(ctrl->device,
1277 "Device shutdown incomplete; abort shutdown\n");
1284 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
1286 static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1287 struct request_queue *q)
1291 if (ctrl->max_hw_sectors) {
1293 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1295 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
1296 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
1298 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1299 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
1300 blk_queue_virt_boundary(q, ctrl->page_size - 1);
1301 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1303 blk_queue_write_cache(q, vwc, vwc);
1306 static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1309 * APST (Autonomous Power State Transition) lets us program a
1310 * table of power state transitions that the controller will
1311 * perform automatically. We configure it with a simple
1312 * heuristic: we are willing to spend at most 2% of the time
1313 * transitioning between power states. Therefore, when running
1314 * in any given state, we will enter the next lower-power
1315 * non-operational state after waiting 100 * (enlat + exlat)
1316 * microseconds, as long as that state's total latency is under
1317 * the requested maximum latency.
1319 * We will not autonomously enter any non-operational state for
1320 * which the total latency exceeds ps_max_latency_us. Users
1321 * can set ps_max_latency_us to zero to turn off APST.
1325 struct nvme_feat_auto_pst *table;
1329 * If APST isn't supported or if we haven't been initialized yet,
1330 * then don't do anything.
1335 if (ctrl->npss > 31) {
1336 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1340 table = kzalloc(sizeof(*table), GFP_KERNEL);
1344 if (ctrl->ps_max_latency_us == 0) {
1345 /* Turn off APST. */
1348 __le64 target = cpu_to_le64(0);
1352 * Walk through all states from lowest- to highest-power.
1353 * According to the spec, lower-numbered states use more
1354 * power. NPSS, despite the name, is the index of the
1355 * lowest-power state, not the number of states.
1357 for (state = (int)ctrl->npss; state >= 0; state--) {
1358 u64 total_latency_us, transition_ms;
1361 table->entries[state] = target;
1364 * Is this state a useful non-operational state for
1365 * higher-power states to autonomously transition to?
1367 if (!(ctrl->psd[state].flags &
1368 NVME_PS_FLAGS_NON_OP_STATE))
1372 (u64)le32_to_cpu(ctrl->psd[state].entry_lat) +
1373 + le32_to_cpu(ctrl->psd[state].exit_lat);
1374 if (total_latency_us > ctrl->ps_max_latency_us)
1378 * This state is good. Use it as the APST idle
1379 * target for higher power states.
1381 transition_ms = total_latency_us + 19;
1382 do_div(transition_ms, 20);
1383 if (transition_ms > (1 << 24) - 1)
1384 transition_ms = (1 << 24) - 1;
1386 target = cpu_to_le64((state << 3) |
1387 (transition_ms << 8));
1393 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1394 table, sizeof(*table), NULL);
1396 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1401 static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1403 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1407 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1408 case PM_QOS_LATENCY_ANY:
1416 if (ctrl->ps_max_latency_us != latency) {
1417 ctrl->ps_max_latency_us = latency;
1418 nvme_configure_apst(ctrl);
1422 struct nvme_core_quirk_entry {
1424 * NVMe model and firmware strings are padded with spaces. For
1425 * simplicity, strings in the quirk table are padded with NULLs
1431 unsigned long quirks;
1434 static const struct nvme_core_quirk_entry core_quirks[] = {
1436 * Seen on a Samsung "SM951 NVMe SAMSUNG 256GB": using APST causes
1437 * the controller to go out to lunch. It dies when the watchdog
1438 * timer reads CSTS and gets 0xffffffff.
1443 .quirks = NVME_QUIRK_NO_APST,
1447 /* match is null-terminated but idstr is space-padded. */
1448 static bool string_matches(const char *idstr, const char *match, size_t len)
1455 matchlen = strlen(match);
1456 WARN_ON_ONCE(matchlen > len);
1458 if (memcmp(idstr, match, matchlen))
1461 for (; matchlen < len; matchlen++)
1462 if (idstr[matchlen] != ' ')
1468 static bool quirk_matches(const struct nvme_id_ctrl *id,
1469 const struct nvme_core_quirk_entry *q)
1471 return q->vid == le16_to_cpu(id->vid) &&
1472 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1473 string_matches(id->fr, q->fr, sizeof(id->fr));
1477 * Initialize the cached copies of the Identify data and various controller
1478 * register in our nvme_ctrl structure. This should be called as soon as
1479 * the admin queue is fully up and running.
1481 int nvme_init_identify(struct nvme_ctrl *ctrl)
1483 struct nvme_id_ctrl *id;
1485 int ret, page_shift;
1489 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1491 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
1495 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1497 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
1500 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1502 if (ctrl->vs >= NVME_VS(1, 1, 0))
1503 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1505 ret = nvme_identify_ctrl(ctrl, &id);
1507 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
1511 if (!ctrl->identified) {
1513 * Check for quirks. Quirk can depend on firmware version,
1514 * so, in principle, the set of quirks present can change
1515 * across a reset. As a possible future enhancement, we
1516 * could re-scan for quirks every time we reinitialize
1517 * the device, but we'd have to make sure that the driver
1518 * behaves intelligently if the quirks change.
1523 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1524 if (quirk_matches(id, &core_quirks[i]))
1525 ctrl->quirks |= core_quirks[i].quirks;
1529 ctrl->oacs = le16_to_cpu(id->oacs);
1530 ctrl->vid = le16_to_cpu(id->vid);
1531 ctrl->oncs = le16_to_cpup(&id->oncs);
1532 atomic_set(&ctrl->abort_limit, id->acl + 1);
1533 ctrl->vwc = id->vwc;
1534 ctrl->cntlid = le16_to_cpup(&id->cntlid);
1535 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1536 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1537 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1539 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
1541 max_hw_sectors = UINT_MAX;
1542 ctrl->max_hw_sectors =
1543 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
1545 nvme_set_queue_limits(ctrl, ctrl->admin_q);
1546 ctrl->sgls = le32_to_cpu(id->sgls);
1547 ctrl->kas = le16_to_cpu(id->kas);
1549 ctrl->npss = id->npss;
1550 prev_apsta = ctrl->apsta;
1551 ctrl->apsta = (ctrl->quirks & NVME_QUIRK_NO_APST) ? 0 : id->apsta;
1552 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1554 if (ctrl->ops->is_fabrics) {
1555 ctrl->icdoff = le16_to_cpu(id->icdoff);
1556 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1557 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1558 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1561 * In fabrics we need to verify the cntlid matches the
1564 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1567 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1569 "keep-alive support is mandatory for fabrics\n");
1573 ctrl->cntlid = le16_to_cpu(id->cntlid);
1578 if (ctrl->apsta && !prev_apsta)
1579 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1580 else if (!ctrl->apsta && prev_apsta)
1581 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1583 nvme_configure_apst(ctrl);
1585 ctrl->identified = true;
1589 EXPORT_SYMBOL_GPL(nvme_init_identify);
1591 static int nvme_dev_open(struct inode *inode, struct file *file)
1593 struct nvme_ctrl *ctrl;
1594 int instance = iminor(inode);
1597 spin_lock(&dev_list_lock);
1598 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1599 if (ctrl->instance != instance)
1602 if (!ctrl->admin_q) {
1606 if (!kref_get_unless_zero(&ctrl->kref))
1608 file->private_data = ctrl;
1612 spin_unlock(&dev_list_lock);
1617 static int nvme_dev_release(struct inode *inode, struct file *file)
1619 nvme_put_ctrl(file->private_data);
1623 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1628 mutex_lock(&ctrl->namespaces_mutex);
1629 if (list_empty(&ctrl->namespaces)) {
1634 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1635 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
1636 dev_warn(ctrl->device,
1637 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1642 dev_warn(ctrl->device,
1643 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1644 kref_get(&ns->kref);
1645 mutex_unlock(&ctrl->namespaces_mutex);
1647 ret = nvme_user_cmd(ctrl, ns, argp);
1652 mutex_unlock(&ctrl->namespaces_mutex);
1656 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1659 struct nvme_ctrl *ctrl = file->private_data;
1660 void __user *argp = (void __user *)arg;
1663 case NVME_IOCTL_ADMIN_CMD:
1664 return nvme_user_cmd(ctrl, NULL, argp);
1665 case NVME_IOCTL_IO_CMD:
1666 return nvme_dev_user_cmd(ctrl, argp);
1667 case NVME_IOCTL_RESET:
1668 dev_warn(ctrl->device, "resetting controller\n");
1669 return ctrl->ops->reset_ctrl(ctrl);
1670 case NVME_IOCTL_SUBSYS_RESET:
1671 return nvme_reset_subsystem(ctrl);
1672 case NVME_IOCTL_RESCAN:
1673 nvme_queue_scan(ctrl);
1680 static const struct file_operations nvme_dev_fops = {
1681 .owner = THIS_MODULE,
1682 .open = nvme_dev_open,
1683 .release = nvme_dev_release,
1684 .unlocked_ioctl = nvme_dev_ioctl,
1685 .compat_ioctl = nvme_dev_ioctl,
1688 static ssize_t nvme_sysfs_reset(struct device *dev,
1689 struct device_attribute *attr, const char *buf,
1692 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1695 ret = ctrl->ops->reset_ctrl(ctrl);
1700 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1702 static ssize_t nvme_sysfs_rescan(struct device *dev,
1703 struct device_attribute *attr, const char *buf,
1706 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1708 nvme_queue_scan(ctrl);
1711 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1713 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1716 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1717 struct nvme_ctrl *ctrl = ns->ctrl;
1718 int serial_len = sizeof(ctrl->serial);
1719 int model_len = sizeof(ctrl->model);
1721 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1722 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1724 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1725 return sprintf(buf, "eui.%8phN\n", ns->eui);
1727 while (ctrl->serial[serial_len - 1] == ' ')
1729 while (ctrl->model[model_len - 1] == ' ')
1732 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1733 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1735 static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1737 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1740 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1741 return sprintf(buf, "%pU\n", ns->uuid);
1743 static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1745 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1748 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1749 return sprintf(buf, "%8phd\n", ns->eui);
1751 static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1753 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1756 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1757 return sprintf(buf, "%d\n", ns->ns_id);
1759 static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1761 static struct attribute *nvme_ns_attrs[] = {
1762 &dev_attr_wwid.attr,
1763 &dev_attr_uuid.attr,
1765 &dev_attr_nsid.attr,
1769 static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
1770 struct attribute *a, int n)
1772 struct device *dev = container_of(kobj, struct device, kobj);
1773 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
1775 if (a == &dev_attr_uuid.attr) {
1776 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1779 if (a == &dev_attr_eui.attr) {
1780 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1786 static const struct attribute_group nvme_ns_attr_group = {
1787 .attrs = nvme_ns_attrs,
1788 .is_visible = nvme_ns_attrs_are_visible,
1791 #define nvme_show_str_function(field) \
1792 static ssize_t field##_show(struct device *dev, \
1793 struct device_attribute *attr, char *buf) \
1795 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1796 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1798 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1800 #define nvme_show_int_function(field) \
1801 static ssize_t field##_show(struct device *dev, \
1802 struct device_attribute *attr, char *buf) \
1804 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1805 return sprintf(buf, "%d\n", ctrl->field); \
1807 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1809 nvme_show_str_function(model);
1810 nvme_show_str_function(serial);
1811 nvme_show_str_function(firmware_rev);
1812 nvme_show_int_function(cntlid);
1814 static ssize_t nvme_sysfs_delete(struct device *dev,
1815 struct device_attribute *attr, const char *buf,
1818 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1820 if (device_remove_file_self(dev, attr))
1821 ctrl->ops->delete_ctrl(ctrl);
1824 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1826 static ssize_t nvme_sysfs_show_transport(struct device *dev,
1827 struct device_attribute *attr,
1830 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1832 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1834 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1836 static ssize_t nvme_sysfs_show_state(struct device *dev,
1837 struct device_attribute *attr,
1840 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1841 static const char *const state_name[] = {
1842 [NVME_CTRL_NEW] = "new",
1843 [NVME_CTRL_LIVE] = "live",
1844 [NVME_CTRL_RESETTING] = "resetting",
1845 [NVME_CTRL_RECONNECTING]= "reconnecting",
1846 [NVME_CTRL_DELETING] = "deleting",
1847 [NVME_CTRL_DEAD] = "dead",
1850 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1851 state_name[ctrl->state])
1852 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1854 return sprintf(buf, "unknown state\n");
1857 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
1859 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1860 struct device_attribute *attr,
1863 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1865 return snprintf(buf, PAGE_SIZE, "%s\n",
1866 ctrl->ops->get_subsysnqn(ctrl));
1868 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1870 static ssize_t nvme_sysfs_show_address(struct device *dev,
1871 struct device_attribute *attr,
1874 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1876 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1878 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1880 static struct attribute *nvme_dev_attrs[] = {
1881 &dev_attr_reset_controller.attr,
1882 &dev_attr_rescan_controller.attr,
1883 &dev_attr_model.attr,
1884 &dev_attr_serial.attr,
1885 &dev_attr_firmware_rev.attr,
1886 &dev_attr_cntlid.attr,
1887 &dev_attr_delete_controller.attr,
1888 &dev_attr_transport.attr,
1889 &dev_attr_subsysnqn.attr,
1890 &dev_attr_address.attr,
1891 &dev_attr_state.attr,
1895 #define CHECK_ATTR(ctrl, a, name) \
1896 if ((a) == &dev_attr_##name.attr && \
1897 !(ctrl)->ops->get_##name) \
1900 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1901 struct attribute *a, int n)
1903 struct device *dev = container_of(kobj, struct device, kobj);
1904 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1906 if (a == &dev_attr_delete_controller.attr) {
1907 if (!ctrl->ops->delete_ctrl)
1911 CHECK_ATTR(ctrl, a, subsysnqn);
1912 CHECK_ATTR(ctrl, a, address);
1917 static struct attribute_group nvme_dev_attrs_group = {
1918 .attrs = nvme_dev_attrs,
1919 .is_visible = nvme_dev_attrs_are_visible,
1922 static const struct attribute_group *nvme_dev_attr_groups[] = {
1923 &nvme_dev_attrs_group,
1927 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1929 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1930 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1932 return nsa->ns_id - nsb->ns_id;
1935 static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1937 struct nvme_ns *ns, *ret = NULL;
1939 mutex_lock(&ctrl->namespaces_mutex);
1940 list_for_each_entry(ns, &ctrl->namespaces, list) {
1941 if (ns->ns_id == nsid) {
1942 kref_get(&ns->kref);
1946 if (ns->ns_id > nsid)
1949 mutex_unlock(&ctrl->namespaces_mutex);
1953 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1956 struct gendisk *disk;
1957 struct nvme_id_ns *id;
1958 char disk_name[DISK_NAME_LEN];
1959 int node = dev_to_node(ctrl->dev);
1961 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1965 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1966 if (ns->instance < 0)
1969 ns->queue = blk_mq_init_queue(ctrl->tagset);
1970 if (IS_ERR(ns->queue))
1971 goto out_release_instance;
1972 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1973 ns->queue->queuedata = ns;
1976 kref_init(&ns->kref);
1978 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1980 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1981 nvme_set_queue_limits(ctrl, ns->queue);
1983 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
1985 if (nvme_revalidate_ns(ns, &id))
1986 goto out_free_queue;
1988 if (nvme_nvm_ns_supported(ns, id) &&
1989 nvme_nvm_register(ns, disk_name, node)) {
1990 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
1994 disk = alloc_disk_node(0, node);
1998 disk->fops = &nvme_fops;
1999 disk->private_data = ns;
2000 disk->queue = ns->queue;
2001 disk->flags = GENHD_FL_EXT_DEVT;
2002 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2005 __nvme_revalidate_disk(disk, id);
2007 mutex_lock(&ctrl->namespaces_mutex);
2008 list_add_tail(&ns->list, &ctrl->namespaces);
2009 mutex_unlock(&ctrl->namespaces_mutex);
2011 kref_get(&ctrl->kref);
2015 device_add_disk(ctrl->device, ns->disk);
2016 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2017 &nvme_ns_attr_group))
2018 pr_warn("%s: failed to create sysfs group for identification\n",
2019 ns->disk->disk_name);
2020 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2021 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2022 ns->disk->disk_name);
2027 blk_cleanup_queue(ns->queue);
2028 out_release_instance:
2029 ida_simple_remove(&ctrl->ns_ida, ns->instance);
2034 static void nvme_ns_remove(struct nvme_ns *ns)
2036 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2039 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
2040 if (blk_get_integrity(ns->disk))
2041 blk_integrity_unregister(ns->disk);
2042 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2043 &nvme_ns_attr_group);
2045 nvme_nvm_unregister_sysfs(ns);
2046 del_gendisk(ns->disk);
2047 blk_mq_abort_requeue_list(ns->queue);
2048 blk_cleanup_queue(ns->queue);
2051 mutex_lock(&ns->ctrl->namespaces_mutex);
2052 list_del_init(&ns->list);
2053 mutex_unlock(&ns->ctrl->namespaces_mutex);
2058 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2062 ns = nvme_find_get_ns(ctrl, nsid);
2064 if (ns->disk && revalidate_disk(ns->disk))
2068 nvme_alloc_ns(ctrl, nsid);
2071 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2074 struct nvme_ns *ns, *next;
2076 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2077 if (ns->ns_id > nsid)
2082 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2086 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2089 ns_list = kzalloc(0x1000, GFP_KERNEL);
2093 for (i = 0; i < num_lists; i++) {
2094 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2098 for (j = 0; j < min(nn, 1024U); j++) {
2099 nsid = le32_to_cpu(ns_list[j]);
2103 nvme_validate_ns(ctrl, nsid);
2105 while (++prev < nsid) {
2106 ns = nvme_find_get_ns(ctrl, prev);
2116 nvme_remove_invalid_namespaces(ctrl, prev);
2122 static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
2126 for (i = 1; i <= nn; i++)
2127 nvme_validate_ns(ctrl, i);
2129 nvme_remove_invalid_namespaces(ctrl, nn);
2132 static void nvme_scan_work(struct work_struct *work)
2134 struct nvme_ctrl *ctrl =
2135 container_of(work, struct nvme_ctrl, scan_work);
2136 struct nvme_id_ctrl *id;
2139 if (ctrl->state != NVME_CTRL_LIVE)
2142 if (nvme_identify_ctrl(ctrl, &id))
2145 nn = le32_to_cpu(id->nn);
2146 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
2147 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2148 if (!nvme_scan_ns_list(ctrl, nn))
2151 nvme_scan_ns_sequential(ctrl, nn);
2153 mutex_lock(&ctrl->namespaces_mutex);
2154 list_sort(NULL, &ctrl->namespaces, ns_cmp);
2155 mutex_unlock(&ctrl->namespaces_mutex);
2159 void nvme_queue_scan(struct nvme_ctrl *ctrl)
2162 * Do not queue new scan work when a controller is reset during
2165 if (ctrl->state == NVME_CTRL_LIVE)
2166 schedule_work(&ctrl->scan_work);
2168 EXPORT_SYMBOL_GPL(nvme_queue_scan);
2171 * This function iterates the namespace list unlocked to allow recovery from
2172 * controller failure. It is up to the caller to ensure the namespace list is
2173 * not modified by scan work while this function is executing.
2175 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2177 struct nvme_ns *ns, *next;
2180 * The dead states indicates the controller was not gracefully
2181 * disconnected. In that case, we won't be able to flush any data while
2182 * removing the namespaces' disks; fail all the queues now to avoid
2183 * potentially having to clean up the failed sync later.
2185 if (ctrl->state == NVME_CTRL_DEAD)
2186 nvme_kill_queues(ctrl);
2188 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2191 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
2193 static void nvme_async_event_work(struct work_struct *work)
2195 struct nvme_ctrl *ctrl =
2196 container_of(work, struct nvme_ctrl, async_event_work);
2198 spin_lock_irq(&ctrl->lock);
2199 while (ctrl->event_limit > 0) {
2200 int aer_idx = --ctrl->event_limit;
2202 spin_unlock_irq(&ctrl->lock);
2203 ctrl->ops->submit_async_event(ctrl, aer_idx);
2204 spin_lock_irq(&ctrl->lock);
2206 spin_unlock_irq(&ctrl->lock);
2209 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2210 union nvme_result *res)
2212 u32 result = le32_to_cpu(res->u32);
2215 switch (le16_to_cpu(status) >> 1) {
2216 case NVME_SC_SUCCESS:
2219 case NVME_SC_ABORT_REQ:
2220 ++ctrl->event_limit;
2221 schedule_work(&ctrl->async_event_work);
2230 switch (result & 0xff07) {
2231 case NVME_AER_NOTICE_NS_CHANGED:
2232 dev_info(ctrl->device, "rescanning\n");
2233 nvme_queue_scan(ctrl);
2236 dev_warn(ctrl->device, "async event result %08x\n", result);
2239 EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2241 void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2243 ctrl->event_limit = NVME_NR_AERS;
2244 schedule_work(&ctrl->async_event_work);
2246 EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2248 static DEFINE_IDA(nvme_instance_ida);
2250 static int nvme_set_instance(struct nvme_ctrl *ctrl)
2252 int instance, error;
2255 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2258 spin_lock(&dev_list_lock);
2259 error = ida_get_new(&nvme_instance_ida, &instance);
2260 spin_unlock(&dev_list_lock);
2261 } while (error == -EAGAIN);
2266 ctrl->instance = instance;
2270 static void nvme_release_instance(struct nvme_ctrl *ctrl)
2272 spin_lock(&dev_list_lock);
2273 ida_remove(&nvme_instance_ida, ctrl->instance);
2274 spin_unlock(&dev_list_lock);
2277 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2279 flush_work(&ctrl->async_event_work);
2280 flush_work(&ctrl->scan_work);
2281 nvme_remove_namespaces(ctrl);
2283 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
2285 spin_lock(&dev_list_lock);
2286 list_del(&ctrl->node);
2287 spin_unlock(&dev_list_lock);
2289 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
2291 static void nvme_free_ctrl(struct kref *kref)
2293 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
2295 put_device(ctrl->device);
2296 nvme_release_instance(ctrl);
2297 ida_destroy(&ctrl->ns_ida);
2299 ctrl->ops->free_ctrl(ctrl);
2302 void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2304 kref_put(&ctrl->kref, nvme_free_ctrl);
2306 EXPORT_SYMBOL_GPL(nvme_put_ctrl);
2309 * Initialize a NVMe controller structures. This needs to be called during
2310 * earliest initialization so that we have the initialized structured around
2313 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2314 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2318 ctrl->state = NVME_CTRL_NEW;
2319 spin_lock_init(&ctrl->lock);
2320 INIT_LIST_HEAD(&ctrl->namespaces);
2321 mutex_init(&ctrl->namespaces_mutex);
2322 kref_init(&ctrl->kref);
2325 ctrl->quirks = quirks;
2326 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
2327 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
2329 ret = nvme_set_instance(ctrl);
2333 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
2334 MKDEV(nvme_char_major, ctrl->instance),
2335 ctrl, nvme_dev_attr_groups,
2336 "nvme%d", ctrl->instance);
2337 if (IS_ERR(ctrl->device)) {
2338 ret = PTR_ERR(ctrl->device);
2339 goto out_release_instance;
2341 get_device(ctrl->device);
2342 ida_init(&ctrl->ns_ida);
2344 spin_lock(&dev_list_lock);
2345 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2346 spin_unlock(&dev_list_lock);
2349 * Initialize latency tolerance controls. The sysfs files won't
2350 * be visible to userspace unless the device actually supports APST.
2352 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2353 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2354 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2357 out_release_instance:
2358 nvme_release_instance(ctrl);
2362 EXPORT_SYMBOL_GPL(nvme_init_ctrl);
2365 * nvme_kill_queues(): Ends all namespace queues
2366 * @ctrl: the dead controller that needs to end
2368 * Call this function when the driver determines it is unable to get the
2369 * controller in a state capable of servicing IO.
2371 void nvme_kill_queues(struct nvme_ctrl *ctrl)
2375 mutex_lock(&ctrl->namespaces_mutex);
2376 list_for_each_entry(ns, &ctrl->namespaces, list) {
2378 * Revalidating a dead namespace sets capacity to 0. This will
2379 * end buffered writers dirtying pages that can't be synced.
2381 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2383 revalidate_disk(ns->disk);
2384 blk_set_queue_dying(ns->queue);
2385 blk_mq_abort_requeue_list(ns->queue);
2386 blk_mq_start_stopped_hw_queues(ns->queue, true);
2388 mutex_unlock(&ctrl->namespaces_mutex);
2390 EXPORT_SYMBOL_GPL(nvme_kill_queues);
2392 void nvme_unfreeze(struct nvme_ctrl *ctrl)
2396 mutex_lock(&ctrl->namespaces_mutex);
2397 list_for_each_entry(ns, &ctrl->namespaces, list)
2398 blk_mq_unfreeze_queue(ns->queue);
2399 mutex_unlock(&ctrl->namespaces_mutex);
2401 EXPORT_SYMBOL_GPL(nvme_unfreeze);
2403 void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2407 mutex_lock(&ctrl->namespaces_mutex);
2408 list_for_each_entry(ns, &ctrl->namespaces, list) {
2409 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2413 mutex_unlock(&ctrl->namespaces_mutex);
2415 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2417 void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2421 mutex_lock(&ctrl->namespaces_mutex);
2422 list_for_each_entry(ns, &ctrl->namespaces, list)
2423 blk_mq_freeze_queue_wait(ns->queue);
2424 mutex_unlock(&ctrl->namespaces_mutex);
2426 EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2428 void nvme_start_freeze(struct nvme_ctrl *ctrl)
2432 mutex_lock(&ctrl->namespaces_mutex);
2433 list_for_each_entry(ns, &ctrl->namespaces, list)
2434 blk_freeze_queue_start(ns->queue);
2435 mutex_unlock(&ctrl->namespaces_mutex);
2437 EXPORT_SYMBOL_GPL(nvme_start_freeze);
2439 void nvme_stop_queues(struct nvme_ctrl *ctrl)
2443 mutex_lock(&ctrl->namespaces_mutex);
2444 list_for_each_entry(ns, &ctrl->namespaces, list)
2445 blk_mq_quiesce_queue(ns->queue);
2446 mutex_unlock(&ctrl->namespaces_mutex);
2448 EXPORT_SYMBOL_GPL(nvme_stop_queues);
2450 void nvme_start_queues(struct nvme_ctrl *ctrl)
2454 mutex_lock(&ctrl->namespaces_mutex);
2455 list_for_each_entry(ns, &ctrl->namespaces, list) {
2456 blk_mq_start_stopped_hw_queues(ns->queue, true);
2457 blk_mq_kick_requeue_list(ns->queue);
2459 mutex_unlock(&ctrl->namespaces_mutex);
2461 EXPORT_SYMBOL_GPL(nvme_start_queues);
2463 int __init nvme_core_init(void)
2467 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2471 else if (result > 0)
2472 nvme_char_major = result;
2474 nvme_class = class_create(THIS_MODULE, "nvme");
2475 if (IS_ERR(nvme_class)) {
2476 result = PTR_ERR(nvme_class);
2477 goto unregister_chrdev;
2483 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
2487 void nvme_core_exit(void)
2489 class_destroy(nvme_class);
2490 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
2493 MODULE_LICENSE("GPL");
2494 MODULE_VERSION("1.0");
2495 module_init(nvme_core_init);
2496 module_exit(nvme_core_exit);