2 * Common code for the NVMe target.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/rculist.h>
21 static struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
22 static DEFINE_IDA(cntlid_ida);
25 * This read/write semaphore is used to synchronize access to configuration
26 * information on a target system that will result in discovery log page
27 * information change for at least one host.
28 * The full list of resources to protected by this semaphore is:
31 * - per-subsystem allowed hosts list
32 * - allow_any_host subsystem attribute
34 * - the nvmet_transports array
36 * When updating any of those lists/structures write lock should be obtained,
37 * while when reading (popolating discovery log page or checking host-subsystem
38 * link) read lock is obtained to allow concurrent reads.
40 DECLARE_RWSEM(nvmet_config_sem);
42 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
43 const char *subsysnqn);
45 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
48 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
49 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
53 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
55 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
56 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
60 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
62 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
65 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
67 struct nvmet_req *req;
70 mutex_lock(&ctrl->lock);
71 if (!ctrl->nr_async_event_cmds) {
72 mutex_unlock(&ctrl->lock);
76 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
77 mutex_unlock(&ctrl->lock);
78 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
82 static void nvmet_async_event_work(struct work_struct *work)
84 struct nvmet_ctrl *ctrl =
85 container_of(work, struct nvmet_ctrl, async_event_work);
86 struct nvmet_async_event *aen;
87 struct nvmet_req *req;
90 mutex_lock(&ctrl->lock);
91 aen = list_first_entry_or_null(&ctrl->async_events,
92 struct nvmet_async_event, entry);
93 if (!aen || !ctrl->nr_async_event_cmds) {
94 mutex_unlock(&ctrl->lock);
98 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
99 nvmet_set_result(req, nvmet_async_event_result(aen));
101 list_del(&aen->entry);
104 mutex_unlock(&ctrl->lock);
105 nvmet_req_complete(req, 0);
109 static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
110 u8 event_info, u8 log_page)
112 struct nvmet_async_event *aen;
114 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
118 aen->event_type = event_type;
119 aen->event_info = event_info;
120 aen->log_page = log_page;
122 mutex_lock(&ctrl->lock);
123 list_add_tail(&aen->entry, &ctrl->async_events);
124 mutex_unlock(&ctrl->lock);
126 schedule_work(&ctrl->async_event_work);
129 int nvmet_register_transport(struct nvmet_fabrics_ops *ops)
133 down_write(&nvmet_config_sem);
134 if (nvmet_transports[ops->type])
137 nvmet_transports[ops->type] = ops;
138 up_write(&nvmet_config_sem);
142 EXPORT_SYMBOL_GPL(nvmet_register_transport);
144 void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops)
146 down_write(&nvmet_config_sem);
147 nvmet_transports[ops->type] = NULL;
148 up_write(&nvmet_config_sem);
150 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
152 int nvmet_enable_port(struct nvmet_port *port)
154 struct nvmet_fabrics_ops *ops;
157 lockdep_assert_held(&nvmet_config_sem);
159 ops = nvmet_transports[port->disc_addr.trtype];
161 up_write(&nvmet_config_sem);
162 request_module("nvmet-transport-%d", port->disc_addr.trtype);
163 down_write(&nvmet_config_sem);
164 ops = nvmet_transports[port->disc_addr.trtype];
166 pr_err("transport type %d not supported\n",
167 port->disc_addr.trtype);
172 if (!try_module_get(ops->owner))
175 ret = ops->add_port(port);
177 module_put(ops->owner);
181 port->enabled = true;
185 void nvmet_disable_port(struct nvmet_port *port)
187 struct nvmet_fabrics_ops *ops;
189 lockdep_assert_held(&nvmet_config_sem);
191 port->enabled = false;
193 ops = nvmet_transports[port->disc_addr.trtype];
194 ops->remove_port(port);
195 module_put(ops->owner);
198 static void nvmet_keep_alive_timer(struct work_struct *work)
200 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
201 struct nvmet_ctrl, ka_work);
203 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
204 ctrl->cntlid, ctrl->kato);
206 nvmet_ctrl_fatal_error(ctrl);
209 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
211 if (unlikely(ctrl->kato == 0))
214 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
215 ctrl->cntlid, ctrl->kato);
217 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
218 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
221 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
223 if (unlikely(ctrl->kato == 0))
226 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
228 cancel_delayed_work_sync(&ctrl->ka_work);
231 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
236 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
237 if (ns->nsid == le32_to_cpu(nsid))
244 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
249 ns = __nvmet_find_namespace(ctrl, nsid);
251 percpu_ref_get(&ns->ref);
257 static void nvmet_destroy_namespace(struct percpu_ref *ref)
259 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
261 complete(&ns->disable_done);
264 void nvmet_put_namespace(struct nvmet_ns *ns)
266 percpu_ref_put(&ns->ref);
269 int nvmet_ns_enable(struct nvmet_ns *ns)
271 struct nvmet_subsys *subsys = ns->subsys;
272 struct nvmet_ctrl *ctrl;
275 mutex_lock(&subsys->lock);
279 ns->bdev = blkdev_get_by_path(ns->device_path, FMODE_READ | FMODE_WRITE,
281 if (IS_ERR(ns->bdev)) {
282 pr_err("failed to open block device %s: (%ld)\n",
283 ns->device_path, PTR_ERR(ns->bdev));
284 ret = PTR_ERR(ns->bdev);
289 ns->size = i_size_read(ns->bdev->bd_inode);
290 ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
292 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
297 if (ns->nsid > subsys->max_nsid)
298 subsys->max_nsid = ns->nsid;
301 * The namespaces list needs to be sorted to simplify the implementation
302 * of the Identify Namepace List subcommand.
304 if (list_empty(&subsys->namespaces)) {
305 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
307 struct nvmet_ns *old;
309 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
310 BUG_ON(ns->nsid == old->nsid);
311 if (ns->nsid < old->nsid)
315 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
318 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
319 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
324 mutex_unlock(&subsys->lock);
327 blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
332 void nvmet_ns_disable(struct nvmet_ns *ns)
334 struct nvmet_subsys *subsys = ns->subsys;
335 struct nvmet_ctrl *ctrl;
337 mutex_lock(&subsys->lock);
342 list_del_rcu(&ns->dev_link);
343 mutex_unlock(&subsys->lock);
346 * Now that we removed the namespaces from the lookup list, we
347 * can kill the per_cpu ref and wait for any remaining references
348 * to be dropped, as well as a RCU grace period for anyone only
349 * using the namepace under rcu_read_lock(). Note that we can't
350 * use call_rcu here as we need to ensure the namespaces have
351 * been fully destroyed before unloading the module.
353 percpu_ref_kill(&ns->ref);
355 wait_for_completion(&ns->disable_done);
356 percpu_ref_exit(&ns->ref);
358 mutex_lock(&subsys->lock);
359 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
360 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
363 blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
365 mutex_unlock(&subsys->lock);
368 void nvmet_ns_free(struct nvmet_ns *ns)
370 nvmet_ns_disable(ns);
372 kfree(ns->device_path);
376 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
380 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
384 INIT_LIST_HEAD(&ns->dev_link);
385 init_completion(&ns->disable_done);
394 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
396 u32 old_sqhd, new_sqhd;
400 nvmet_set_status(req, status);
404 old_sqhd = req->sq->sqhd;
405 new_sqhd = (old_sqhd + 1) % req->sq->size;
406 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
409 sqhd = req->sq->sqhd & 0x0000FFFF;
410 req->rsp->sq_head = cpu_to_le16(sqhd);
411 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
412 req->rsp->command_id = req->cmd->common.command_id;
415 nvmet_put_namespace(req->ns);
416 req->ops->queue_response(req);
419 void nvmet_req_complete(struct nvmet_req *req, u16 status)
421 __nvmet_req_complete(req, status);
422 percpu_ref_put(&req->sq->ref);
424 EXPORT_SYMBOL_GPL(nvmet_req_complete);
426 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
435 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
445 static void nvmet_confirm_sq(struct percpu_ref *ref)
447 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
449 complete(&sq->confirm_done);
452 void nvmet_sq_destroy(struct nvmet_sq *sq)
455 * If this is the admin queue, complete all AERs so that our
456 * queue doesn't have outstanding requests on it.
458 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
459 nvmet_async_events_free(sq->ctrl);
460 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
461 wait_for_completion(&sq->confirm_done);
462 wait_for_completion(&sq->free_done);
463 percpu_ref_exit(&sq->ref);
466 nvmet_ctrl_put(sq->ctrl);
467 sq->ctrl = NULL; /* allows reusing the queue later */
470 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
472 static void nvmet_sq_free(struct percpu_ref *ref)
474 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
476 complete(&sq->free_done);
479 int nvmet_sq_init(struct nvmet_sq *sq)
483 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
485 pr_err("percpu_ref init failed!\n");
488 init_completion(&sq->free_done);
489 init_completion(&sq->confirm_done);
493 EXPORT_SYMBOL_GPL(nvmet_sq_init);
495 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
496 struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops)
498 u8 flags = req->cmd->common.flags;
506 req->rsp->status = 0;
508 /* no support for fused commands yet */
509 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
510 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
515 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
516 * contains an address of a single contiguous physical buffer that is
519 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
520 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
524 if (unlikely(!req->sq->ctrl))
525 /* will return an error for any Non-connect command: */
526 status = nvmet_parse_connect_cmd(req);
527 else if (likely(req->sq->qid != 0))
528 status = nvmet_parse_io_cmd(req);
529 else if (req->cmd->common.opcode == nvme_fabrics_command)
530 status = nvmet_parse_fabrics_cmd(req);
531 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
532 status = nvmet_parse_discovery_cmd(req);
534 status = nvmet_parse_admin_cmd(req);
539 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
540 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
547 __nvmet_req_complete(req, status);
550 EXPORT_SYMBOL_GPL(nvmet_req_init);
552 void nvmet_req_uninit(struct nvmet_req *req)
554 percpu_ref_put(&req->sq->ref);
556 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
558 static inline bool nvmet_cc_en(u32 cc)
560 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
563 static inline u8 nvmet_cc_css(u32 cc)
565 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
568 static inline u8 nvmet_cc_mps(u32 cc)
570 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
573 static inline u8 nvmet_cc_ams(u32 cc)
575 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
578 static inline u8 nvmet_cc_shn(u32 cc)
580 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
583 static inline u8 nvmet_cc_iosqes(u32 cc)
585 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
588 static inline u8 nvmet_cc_iocqes(u32 cc)
590 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
593 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
595 lockdep_assert_held(&ctrl->lock);
598 * Only I/O controllers should verify iosqes,iocqes.
599 * Strictly speaking, the spec says a discovery controller
600 * should verify iosqes,iocqes are zeroed, however that
601 * would break backwards compatibility, so don't enforce it.
603 if (ctrl->subsys->type != NVME_NQN_DISC &&
604 (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
605 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) {
606 ctrl->csts = NVME_CSTS_CFS;
610 if (nvmet_cc_mps(ctrl->cc) != 0 ||
611 nvmet_cc_ams(ctrl->cc) != 0 ||
612 nvmet_cc_css(ctrl->cc) != 0) {
613 ctrl->csts = NVME_CSTS_CFS;
617 ctrl->csts = NVME_CSTS_RDY;
620 * Controllers that are not yet enabled should not really enforce the
621 * keep alive timeout, but we still want to track a timeout and cleanup
622 * in case a host died before it enabled the controller. Hence, simply
623 * reset the keep alive timer when the controller is enabled.
626 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
629 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
631 lockdep_assert_held(&ctrl->lock);
633 /* XXX: tear down queues? */
634 ctrl->csts &= ~NVME_CSTS_RDY;
638 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
642 mutex_lock(&ctrl->lock);
646 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
647 nvmet_start_ctrl(ctrl);
648 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
649 nvmet_clear_ctrl(ctrl);
650 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
651 nvmet_clear_ctrl(ctrl);
652 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
654 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
655 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
656 mutex_unlock(&ctrl->lock);
659 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
661 /* command sets supported: NVMe command set: */
662 ctrl->cap = (1ULL << 37);
663 /* CC.EN timeout in 500msec units: */
664 ctrl->cap |= (15ULL << 24);
665 /* maximum queue entries supported: */
666 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
669 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
670 struct nvmet_req *req, struct nvmet_ctrl **ret)
672 struct nvmet_subsys *subsys;
673 struct nvmet_ctrl *ctrl;
676 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
678 pr_warn("connect request for invalid subsystem %s!\n",
680 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
681 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
684 mutex_lock(&subsys->lock);
685 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
686 if (ctrl->cntlid == cntlid) {
687 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
688 pr_warn("hostnqn mismatch.\n");
691 if (!kref_get_unless_zero(&ctrl->ref))
699 pr_warn("could not find controller %d for subsys %s / host %s\n",
700 cntlid, subsysnqn, hostnqn);
701 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
702 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
705 mutex_unlock(&subsys->lock);
706 nvmet_subsys_put(subsys);
710 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
712 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
713 pr_err("got io cmd %d while CC.EN == 0 on qid = %d\n",
714 cmd->common.opcode, req->sq->qid);
715 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
718 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
719 pr_err("got io cmd %d while CSTS.RDY == 0 on qid = %d\n",
720 cmd->common.opcode, req->sq->qid);
722 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
727 static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
730 struct nvmet_host_link *p;
732 if (subsys->allow_any_host)
735 list_for_each_entry(p, &subsys->hosts, entry) {
736 if (!strcmp(nvmet_host_name(p->host), hostnqn))
743 static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
746 struct nvmet_subsys_link *s;
748 list_for_each_entry(s, &req->port->subsystems, entry) {
749 if (__nvmet_host_allowed(s->subsys, hostnqn))
756 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
759 lockdep_assert_held(&nvmet_config_sem);
761 if (subsys->type == NVME_NQN_DISC)
762 return nvmet_host_discovery_allowed(req, hostnqn);
764 return __nvmet_host_allowed(subsys, hostnqn);
767 static void nvmet_fatal_error_handler(struct work_struct *work)
769 struct nvmet_ctrl *ctrl =
770 container_of(work, struct nvmet_ctrl, fatal_err_work);
772 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
773 ctrl->ops->delete_ctrl(ctrl);
776 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
777 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
779 struct nvmet_subsys *subsys;
780 struct nvmet_ctrl *ctrl;
784 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
785 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
787 pr_warn("connect request for invalid subsystem %s!\n",
789 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
793 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
794 down_read(&nvmet_config_sem);
795 if (!nvmet_host_allowed(req, subsys, hostnqn)) {
796 pr_info("connect by host %s for subsystem %s not allowed\n",
798 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
799 up_read(&nvmet_config_sem);
800 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
801 goto out_put_subsystem;
803 up_read(&nvmet_config_sem);
805 status = NVME_SC_INTERNAL;
806 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
808 goto out_put_subsystem;
809 mutex_init(&ctrl->lock);
811 nvmet_init_cap(ctrl);
813 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
814 INIT_LIST_HEAD(&ctrl->async_events);
815 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
817 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
818 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
820 kref_init(&ctrl->ref);
821 ctrl->subsys = subsys;
823 ctrl->cqs = kcalloc(subsys->max_qid + 1,
824 sizeof(struct nvmet_cq *),
829 ctrl->sqs = kcalloc(subsys->max_qid + 1,
830 sizeof(struct nvmet_sq *),
835 ret = ida_simple_get(&cntlid_ida,
836 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
839 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
844 ctrl->ops = req->ops;
845 if (ctrl->subsys->type == NVME_NQN_DISC) {
846 /* Don't accept keep-alive timeout for discovery controllers */
848 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
853 * Discovery controllers use some arbitrary high value in order
854 * to cleanup stale discovery sessions
856 * From the latest base diff RC:
857 * "The Keep Alive command is not supported by
858 * Discovery controllers. A transport may specify a
859 * fixed Discovery controller activity timeout value
860 * (e.g., 2 minutes). If no commands are received
861 * by a Discovery controller within that time
862 * period, the controller may perform the
863 * actions for Keep Alive Timer expiration".
865 ctrl->kato = NVMET_DISC_KATO;
867 /* keep-alive timeout in seconds */
868 ctrl->kato = DIV_ROUND_UP(kato, 1000);
870 nvmet_start_keep_alive_timer(ctrl);
872 mutex_lock(&subsys->lock);
873 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
874 mutex_unlock(&subsys->lock);
886 nvmet_subsys_put(subsys);
891 static void nvmet_ctrl_free(struct kref *ref)
893 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
894 struct nvmet_subsys *subsys = ctrl->subsys;
896 nvmet_stop_keep_alive_timer(ctrl);
898 mutex_lock(&subsys->lock);
899 list_del(&ctrl->subsys_entry);
900 mutex_unlock(&subsys->lock);
902 flush_work(&ctrl->async_event_work);
903 cancel_work_sync(&ctrl->fatal_err_work);
905 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
906 nvmet_subsys_put(subsys);
913 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
915 kref_put(&ctrl->ref, nvmet_ctrl_free);
918 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
920 mutex_lock(&ctrl->lock);
921 if (!(ctrl->csts & NVME_CSTS_CFS)) {
922 ctrl->csts |= NVME_CSTS_CFS;
923 schedule_work(&ctrl->fatal_err_work);
925 mutex_unlock(&ctrl->lock);
927 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
929 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
930 const char *subsysnqn)
932 struct nvmet_subsys_link *p;
937 if (!strncmp(NVME_DISC_SUBSYS_NAME, subsysnqn,
939 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
941 return nvmet_disc_subsys;
944 down_read(&nvmet_config_sem);
945 list_for_each_entry(p, &port->subsystems, entry) {
946 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
948 if (!kref_get_unless_zero(&p->subsys->ref))
950 up_read(&nvmet_config_sem);
954 up_read(&nvmet_config_sem);
958 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
959 enum nvme_subsys_type type)
961 struct nvmet_subsys *subsys;
963 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
967 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
968 /* generate a random serial number as our controllers are ephemeral: */
969 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
973 subsys->max_qid = NVMET_NR_QUEUES;
979 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
984 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
986 if (!subsys->subsysnqn) {
991 kref_init(&subsys->ref);
993 mutex_init(&subsys->lock);
994 INIT_LIST_HEAD(&subsys->namespaces);
995 INIT_LIST_HEAD(&subsys->ctrls);
996 INIT_LIST_HEAD(&subsys->hosts);
1001 static void nvmet_subsys_free(struct kref *ref)
1003 struct nvmet_subsys *subsys =
1004 container_of(ref, struct nvmet_subsys, ref);
1006 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1008 kfree(subsys->subsysnqn);
1012 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1014 struct nvmet_ctrl *ctrl;
1016 mutex_lock(&subsys->lock);
1017 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1018 ctrl->ops->delete_ctrl(ctrl);
1019 mutex_unlock(&subsys->lock);
1022 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1024 kref_put(&subsys->ref, nvmet_subsys_free);
1027 static int __init nvmet_init(void)
1031 error = nvmet_init_discovery();
1035 error = nvmet_init_configfs();
1037 goto out_exit_discovery;
1041 nvmet_exit_discovery();
1046 static void __exit nvmet_exit(void)
1048 nvmet_exit_configfs();
1049 nvmet_exit_discovery();
1050 ida_destroy(&cntlid_ida);
1052 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1053 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1056 module_init(nvmet_init);
1057 module_exit(nvmet_exit);
1059 MODULE_LICENSE("GPL v2");