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>
18 #include <linux/pci-p2pdma.h>
22 struct workqueue_struct *buffered_io_wq;
23 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
24 static DEFINE_IDA(cntlid_ida);
27 * This read/write semaphore is used to synchronize access to configuration
28 * information on a target system that will result in discovery log page
29 * information change for at least one host.
30 * The full list of resources to protected by this semaphore is:
33 * - per-subsystem allowed hosts list
34 * - allow_any_host subsystem attribute
36 * - the nvmet_transports array
38 * When updating any of those lists/structures write lock should be obtained,
39 * while when reading (popolating discovery log page or checking host-subsystem
40 * link) read lock is obtained to allow concurrent reads.
42 DECLARE_RWSEM(nvmet_config_sem);
44 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
46 DECLARE_RWSEM(nvmet_ana_sem);
48 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
49 const char *subsysnqn);
51 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
54 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
55 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
59 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
61 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
62 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
66 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
68 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len)
69 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
73 static unsigned int nvmet_max_nsid(struct nvmet_subsys *subsys)
77 if (list_empty(&subsys->namespaces))
80 ns = list_last_entry(&subsys->namespaces, struct nvmet_ns, dev_link);
84 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
86 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
89 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
91 struct nvmet_req *req;
94 mutex_lock(&ctrl->lock);
95 if (!ctrl->nr_async_event_cmds) {
96 mutex_unlock(&ctrl->lock);
100 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
101 mutex_unlock(&ctrl->lock);
102 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
106 static void nvmet_async_event_work(struct work_struct *work)
108 struct nvmet_ctrl *ctrl =
109 container_of(work, struct nvmet_ctrl, async_event_work);
110 struct nvmet_async_event *aen;
111 struct nvmet_req *req;
114 mutex_lock(&ctrl->lock);
115 aen = list_first_entry_or_null(&ctrl->async_events,
116 struct nvmet_async_event, entry);
117 if (!aen || !ctrl->nr_async_event_cmds) {
118 mutex_unlock(&ctrl->lock);
122 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
123 nvmet_set_result(req, nvmet_async_event_result(aen));
125 list_del(&aen->entry);
128 mutex_unlock(&ctrl->lock);
129 nvmet_req_complete(req, 0);
133 static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
134 u8 event_info, u8 log_page)
136 struct nvmet_async_event *aen;
138 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
142 aen->event_type = event_type;
143 aen->event_info = event_info;
144 aen->log_page = log_page;
146 mutex_lock(&ctrl->lock);
147 list_add_tail(&aen->entry, &ctrl->async_events);
148 mutex_unlock(&ctrl->lock);
150 schedule_work(&ctrl->async_event_work);
153 static bool nvmet_aen_disabled(struct nvmet_ctrl *ctrl, u32 aen)
155 if (!(READ_ONCE(ctrl->aen_enabled) & aen))
157 return test_and_set_bit(aen, &ctrl->aen_masked);
160 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
164 mutex_lock(&ctrl->lock);
165 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
168 for (i = 0; i < ctrl->nr_changed_ns; i++) {
169 if (ctrl->changed_ns_list[i] == nsid)
173 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
174 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
175 ctrl->nr_changed_ns = U32_MAX;
179 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
181 mutex_unlock(&ctrl->lock);
184 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
186 struct nvmet_ctrl *ctrl;
188 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
189 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
190 if (nvmet_aen_disabled(ctrl, NVME_AEN_CFG_NS_ATTR))
192 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
193 NVME_AER_NOTICE_NS_CHANGED,
194 NVME_LOG_CHANGED_NS);
198 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
199 struct nvmet_port *port)
201 struct nvmet_ctrl *ctrl;
203 mutex_lock(&subsys->lock);
204 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
205 if (port && ctrl->port != port)
207 if (nvmet_aen_disabled(ctrl, NVME_AEN_CFG_ANA_CHANGE))
209 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
210 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
212 mutex_unlock(&subsys->lock);
215 void nvmet_port_send_ana_event(struct nvmet_port *port)
217 struct nvmet_subsys_link *p;
219 down_read(&nvmet_config_sem);
220 list_for_each_entry(p, &port->subsystems, entry)
221 nvmet_send_ana_event(p->subsys, port);
222 up_read(&nvmet_config_sem);
225 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
229 down_write(&nvmet_config_sem);
230 if (nvmet_transports[ops->type])
233 nvmet_transports[ops->type] = ops;
234 up_write(&nvmet_config_sem);
238 EXPORT_SYMBOL_GPL(nvmet_register_transport);
240 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
242 down_write(&nvmet_config_sem);
243 nvmet_transports[ops->type] = NULL;
244 up_write(&nvmet_config_sem);
246 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
248 int nvmet_enable_port(struct nvmet_port *port)
250 const struct nvmet_fabrics_ops *ops;
253 lockdep_assert_held(&nvmet_config_sem);
255 ops = nvmet_transports[port->disc_addr.trtype];
257 up_write(&nvmet_config_sem);
258 request_module("nvmet-transport-%d", port->disc_addr.trtype);
259 down_write(&nvmet_config_sem);
260 ops = nvmet_transports[port->disc_addr.trtype];
262 pr_err("transport type %d not supported\n",
263 port->disc_addr.trtype);
268 if (!try_module_get(ops->owner))
271 ret = ops->add_port(port);
273 module_put(ops->owner);
277 /* If the transport didn't set inline_data_size, then disable it. */
278 if (port->inline_data_size < 0)
279 port->inline_data_size = 0;
281 port->enabled = true;
285 void nvmet_disable_port(struct nvmet_port *port)
287 const struct nvmet_fabrics_ops *ops;
289 lockdep_assert_held(&nvmet_config_sem);
291 port->enabled = false;
293 ops = nvmet_transports[port->disc_addr.trtype];
294 ops->remove_port(port);
295 module_put(ops->owner);
298 static void nvmet_keep_alive_timer(struct work_struct *work)
300 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
301 struct nvmet_ctrl, ka_work);
302 bool cmd_seen = ctrl->cmd_seen;
304 ctrl->cmd_seen = false;
306 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
308 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
312 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
313 ctrl->cntlid, ctrl->kato);
315 nvmet_ctrl_fatal_error(ctrl);
318 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
320 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
321 ctrl->cntlid, ctrl->kato);
323 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
324 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
327 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
329 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
331 cancel_delayed_work_sync(&ctrl->ka_work);
334 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
339 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
340 if (ns->nsid == le32_to_cpu(nsid))
347 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
352 ns = __nvmet_find_namespace(ctrl, nsid);
354 percpu_ref_get(&ns->ref);
360 static void nvmet_destroy_namespace(struct percpu_ref *ref)
362 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
364 complete(&ns->disable_done);
367 void nvmet_put_namespace(struct nvmet_ns *ns)
369 percpu_ref_put(&ns->ref);
372 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
374 nvmet_bdev_ns_disable(ns);
375 nvmet_file_ns_disable(ns);
378 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
381 struct pci_dev *p2p_dev;
387 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
391 if (!blk_queue_pci_p2pdma(ns->bdev->bd_queue)) {
392 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
398 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
403 * Right now we just check that there is p2pmem available so
404 * we can report an error to the user right away if there
405 * is not. We'll find the actual device to use once we
406 * setup the controller when the port's device is available.
409 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
411 pr_err("no peer-to-peer memory is available for %s\n",
416 pci_dev_put(p2p_dev);
423 * Note: ctrl->subsys->lock should be held when calling this function
425 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
428 struct device *clients[2];
429 struct pci_dev *p2p_dev;
432 if (!ctrl->p2p_client || !ns->use_p2pmem)
436 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
440 p2p_dev = pci_dev_get(ns->p2p_dev);
442 clients[0] = ctrl->p2p_client;
443 clients[1] = nvmet_ns_dev(ns);
445 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
447 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
448 dev_name(ctrl->p2p_client), ns->device_path);
453 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
455 pci_dev_put(p2p_dev);
457 pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
461 int nvmet_ns_enable(struct nvmet_ns *ns)
463 struct nvmet_subsys *subsys = ns->subsys;
464 struct nvmet_ctrl *ctrl;
467 mutex_lock(&subsys->lock);
469 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
475 ret = nvmet_bdev_ns_enable(ns);
477 ret = nvmet_file_ns_enable(ns);
481 ret = nvmet_p2pmem_ns_enable(ns);
485 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
486 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
488 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
493 if (ns->nsid > subsys->max_nsid)
494 subsys->max_nsid = ns->nsid;
497 * The namespaces list needs to be sorted to simplify the implementation
498 * of the Identify Namepace List subcommand.
500 if (list_empty(&subsys->namespaces)) {
501 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
503 struct nvmet_ns *old;
505 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
506 BUG_ON(ns->nsid == old->nsid);
507 if (ns->nsid < old->nsid)
511 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
513 subsys->nr_namespaces++;
515 nvmet_ns_changed(subsys, ns->nsid);
519 mutex_unlock(&subsys->lock);
522 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
523 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
525 nvmet_ns_dev_disable(ns);
529 void nvmet_ns_disable(struct nvmet_ns *ns)
531 struct nvmet_subsys *subsys = ns->subsys;
532 struct nvmet_ctrl *ctrl;
534 mutex_lock(&subsys->lock);
539 list_del_rcu(&ns->dev_link);
540 if (ns->nsid == subsys->max_nsid)
541 subsys->max_nsid = nvmet_max_nsid(subsys);
543 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
544 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
546 mutex_unlock(&subsys->lock);
549 * Now that we removed the namespaces from the lookup list, we
550 * can kill the per_cpu ref and wait for any remaining references
551 * to be dropped, as well as a RCU grace period for anyone only
552 * using the namepace under rcu_read_lock(). Note that we can't
553 * use call_rcu here as we need to ensure the namespaces have
554 * been fully destroyed before unloading the module.
556 percpu_ref_kill(&ns->ref);
558 wait_for_completion(&ns->disable_done);
559 percpu_ref_exit(&ns->ref);
561 mutex_lock(&subsys->lock);
563 subsys->nr_namespaces--;
564 nvmet_ns_changed(subsys, ns->nsid);
565 nvmet_ns_dev_disable(ns);
567 mutex_unlock(&subsys->lock);
570 void nvmet_ns_free(struct nvmet_ns *ns)
572 nvmet_ns_disable(ns);
574 down_write(&nvmet_ana_sem);
575 nvmet_ana_group_enabled[ns->anagrpid]--;
576 up_write(&nvmet_ana_sem);
578 kfree(ns->device_path);
582 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
586 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
590 INIT_LIST_HEAD(&ns->dev_link);
591 init_completion(&ns->disable_done);
596 down_write(&nvmet_ana_sem);
597 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
598 nvmet_ana_group_enabled[ns->anagrpid]++;
599 up_write(&nvmet_ana_sem);
602 ns->buffered_io = false;
607 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
609 u32 old_sqhd, new_sqhd;
613 nvmet_set_status(req, status);
617 old_sqhd = req->sq->sqhd;
618 new_sqhd = (old_sqhd + 1) % req->sq->size;
619 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
622 sqhd = req->sq->sqhd & 0x0000FFFF;
623 req->rsp->sq_head = cpu_to_le16(sqhd);
624 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
625 req->rsp->command_id = req->cmd->common.command_id;
628 nvmet_put_namespace(req->ns);
629 req->ops->queue_response(req);
632 void nvmet_req_complete(struct nvmet_req *req, u16 status)
634 __nvmet_req_complete(req, status);
635 percpu_ref_put(&req->sq->ref);
637 EXPORT_SYMBOL_GPL(nvmet_req_complete);
639 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
648 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
658 static void nvmet_confirm_sq(struct percpu_ref *ref)
660 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
662 complete(&sq->confirm_done);
665 void nvmet_sq_destroy(struct nvmet_sq *sq)
668 * If this is the admin queue, complete all AERs so that our
669 * queue doesn't have outstanding requests on it.
671 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
672 nvmet_async_events_free(sq->ctrl);
673 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
674 wait_for_completion(&sq->confirm_done);
675 wait_for_completion(&sq->free_done);
676 percpu_ref_exit(&sq->ref);
679 nvmet_ctrl_put(sq->ctrl);
680 sq->ctrl = NULL; /* allows reusing the queue later */
683 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
685 static void nvmet_sq_free(struct percpu_ref *ref)
687 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
689 complete(&sq->free_done);
692 int nvmet_sq_init(struct nvmet_sq *sq)
696 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
698 pr_err("percpu_ref init failed!\n");
701 init_completion(&sq->free_done);
702 init_completion(&sq->confirm_done);
706 EXPORT_SYMBOL_GPL(nvmet_sq_init);
708 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
711 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
713 if (unlikely(state == NVME_ANA_INACCESSIBLE))
714 return NVME_SC_ANA_INACCESSIBLE;
715 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
716 return NVME_SC_ANA_PERSISTENT_LOSS;
717 if (unlikely(state == NVME_ANA_CHANGE))
718 return NVME_SC_ANA_TRANSITION;
722 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
724 if (unlikely(req->ns->readonly)) {
725 switch (req->cmd->common.opcode) {
730 return NVME_SC_NS_WRITE_PROTECTED;
737 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
739 struct nvme_command *cmd = req->cmd;
742 ret = nvmet_check_ctrl_status(req, cmd);
746 req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
747 if (unlikely(!req->ns))
748 return NVME_SC_INVALID_NS | NVME_SC_DNR;
749 ret = nvmet_check_ana_state(req->port, req->ns);
752 ret = nvmet_io_cmd_check_access(req);
757 return nvmet_file_parse_io_cmd(req);
759 return nvmet_bdev_parse_io_cmd(req);
762 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
763 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
765 u8 flags = req->cmd->common.flags;
773 req->transfer_len = 0;
774 req->rsp->status = 0;
777 /* no support for fused commands yet */
778 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
779 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
784 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
785 * contains an address of a single contiguous physical buffer that is
788 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
789 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
793 if (unlikely(!req->sq->ctrl))
794 /* will return an error for any Non-connect command: */
795 status = nvmet_parse_connect_cmd(req);
796 else if (likely(req->sq->qid != 0))
797 status = nvmet_parse_io_cmd(req);
798 else if (req->cmd->common.opcode == nvme_fabrics_command)
799 status = nvmet_parse_fabrics_cmd(req);
800 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
801 status = nvmet_parse_discovery_cmd(req);
803 status = nvmet_parse_admin_cmd(req);
808 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
809 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
814 sq->ctrl->cmd_seen = true;
819 __nvmet_req_complete(req, status);
822 EXPORT_SYMBOL_GPL(nvmet_req_init);
824 void nvmet_req_uninit(struct nvmet_req *req)
826 percpu_ref_put(&req->sq->ref);
828 nvmet_put_namespace(req->ns);
830 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
832 void nvmet_req_execute(struct nvmet_req *req)
834 if (unlikely(req->data_len != req->transfer_len))
835 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
839 EXPORT_SYMBOL_GPL(nvmet_req_execute);
841 int nvmet_req_alloc_sgl(struct nvmet_req *req)
843 struct pci_dev *p2p_dev = NULL;
845 if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
846 if (req->sq->ctrl && req->ns)
847 p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
851 if (req->sq->qid && p2p_dev) {
852 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
855 req->p2p_dev = p2p_dev;
861 * If no P2P memory was available we fallback to using
866 req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
872 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
874 void nvmet_req_free_sgl(struct nvmet_req *req)
877 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
884 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
886 static inline bool nvmet_cc_en(u32 cc)
888 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
891 static inline u8 nvmet_cc_css(u32 cc)
893 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
896 static inline u8 nvmet_cc_mps(u32 cc)
898 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
901 static inline u8 nvmet_cc_ams(u32 cc)
903 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
906 static inline u8 nvmet_cc_shn(u32 cc)
908 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
911 static inline u8 nvmet_cc_iosqes(u32 cc)
913 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
916 static inline u8 nvmet_cc_iocqes(u32 cc)
918 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
921 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
923 lockdep_assert_held(&ctrl->lock);
925 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
926 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
927 nvmet_cc_mps(ctrl->cc) != 0 ||
928 nvmet_cc_ams(ctrl->cc) != 0 ||
929 nvmet_cc_css(ctrl->cc) != 0) {
930 ctrl->csts = NVME_CSTS_CFS;
934 ctrl->csts = NVME_CSTS_RDY;
937 * Controllers that are not yet enabled should not really enforce the
938 * keep alive timeout, but we still want to track a timeout and cleanup
939 * in case a host died before it enabled the controller. Hence, simply
940 * reset the keep alive timer when the controller is enabled.
942 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
945 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
947 lockdep_assert_held(&ctrl->lock);
949 /* XXX: tear down queues? */
950 ctrl->csts &= ~NVME_CSTS_RDY;
954 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
958 mutex_lock(&ctrl->lock);
962 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
963 nvmet_start_ctrl(ctrl);
964 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
965 nvmet_clear_ctrl(ctrl);
966 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
967 nvmet_clear_ctrl(ctrl);
968 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
970 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
971 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
972 mutex_unlock(&ctrl->lock);
975 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
977 /* command sets supported: NVMe command set: */
978 ctrl->cap = (1ULL << 37);
979 /* CC.EN timeout in 500msec units: */
980 ctrl->cap |= (15ULL << 24);
981 /* maximum queue entries supported: */
982 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
985 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
986 struct nvmet_req *req, struct nvmet_ctrl **ret)
988 struct nvmet_subsys *subsys;
989 struct nvmet_ctrl *ctrl;
992 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
994 pr_warn("connect request for invalid subsystem %s!\n",
996 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
997 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1000 mutex_lock(&subsys->lock);
1001 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
1002 if (ctrl->cntlid == cntlid) {
1003 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1004 pr_warn("hostnqn mismatch.\n");
1007 if (!kref_get_unless_zero(&ctrl->ref))
1015 pr_warn("could not find controller %d for subsys %s / host %s\n",
1016 cntlid, subsysnqn, hostnqn);
1017 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1018 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1021 mutex_unlock(&subsys->lock);
1022 nvmet_subsys_put(subsys);
1026 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1028 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1029 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1030 cmd->common.opcode, req->sq->qid);
1031 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1034 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1035 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1036 cmd->common.opcode, req->sq->qid);
1037 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1042 static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
1043 const char *hostnqn)
1045 struct nvmet_host_link *p;
1047 if (subsys->allow_any_host)
1050 list_for_each_entry(p, &subsys->hosts, entry) {
1051 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1058 static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
1059 const char *hostnqn)
1061 struct nvmet_subsys_link *s;
1063 list_for_each_entry(s, &req->port->subsystems, entry) {
1064 if (__nvmet_host_allowed(s->subsys, hostnqn))
1071 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
1072 const char *hostnqn)
1074 lockdep_assert_held(&nvmet_config_sem);
1076 if (subsys->type == NVME_NQN_DISC)
1077 return nvmet_host_discovery_allowed(req, hostnqn);
1079 return __nvmet_host_allowed(subsys, hostnqn);
1083 * Note: ctrl->subsys->lock should be held when calling this function
1085 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1086 struct nvmet_req *req)
1088 struct nvmet_ns *ns;
1090 if (!req->p2p_client)
1093 ctrl->p2p_client = get_device(req->p2p_client);
1095 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1096 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1100 * Note: ctrl->subsys->lock should be held when calling this function
1102 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1104 struct radix_tree_iter iter;
1107 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1108 pci_dev_put(radix_tree_deref_slot(slot));
1110 put_device(ctrl->p2p_client);
1113 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1114 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1116 struct nvmet_subsys *subsys;
1117 struct nvmet_ctrl *ctrl;
1121 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1122 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1124 pr_warn("connect request for invalid subsystem %s!\n",
1126 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1130 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1131 down_read(&nvmet_config_sem);
1132 if (!nvmet_host_allowed(req, subsys, hostnqn)) {
1133 pr_info("connect by host %s for subsystem %s not allowed\n",
1134 hostnqn, subsysnqn);
1135 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1136 up_read(&nvmet_config_sem);
1137 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1138 goto out_put_subsystem;
1140 up_read(&nvmet_config_sem);
1142 status = NVME_SC_INTERNAL;
1143 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1145 goto out_put_subsystem;
1146 mutex_init(&ctrl->lock);
1148 nvmet_init_cap(ctrl);
1150 ctrl->port = req->port;
1152 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1153 INIT_LIST_HEAD(&ctrl->async_events);
1154 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1156 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1157 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1159 kref_init(&ctrl->ref);
1160 ctrl->subsys = subsys;
1161 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1163 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1164 sizeof(__le32), GFP_KERNEL);
1165 if (!ctrl->changed_ns_list)
1168 ctrl->cqs = kcalloc(subsys->max_qid + 1,
1169 sizeof(struct nvmet_cq *),
1172 goto out_free_changed_ns_list;
1174 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1175 sizeof(struct nvmet_sq *),
1180 ret = ida_simple_get(&cntlid_ida,
1181 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1184 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1189 ctrl->ops = req->ops;
1190 if (ctrl->subsys->type == NVME_NQN_DISC) {
1191 /* Don't accept keep-alive timeout for discovery controllers */
1193 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
1194 goto out_remove_ida;
1198 * Discovery controllers use some arbitrary high value in order
1199 * to cleanup stale discovery sessions
1201 * From the latest base diff RC:
1202 * "The Keep Alive command is not supported by
1203 * Discovery controllers. A transport may specify a
1204 * fixed Discovery controller activity timeout value
1205 * (e.g., 2 minutes). If no commands are received
1206 * by a Discovery controller within that time
1207 * period, the controller may perform the
1208 * actions for Keep Alive Timer expiration".
1210 ctrl->kato = NVMET_DISC_KATO;
1212 /* keep-alive timeout in seconds */
1213 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1215 nvmet_start_keep_alive_timer(ctrl);
1217 mutex_lock(&subsys->lock);
1218 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1219 nvmet_setup_p2p_ns_map(ctrl, req);
1220 mutex_unlock(&subsys->lock);
1226 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1231 out_free_changed_ns_list:
1232 kfree(ctrl->changed_ns_list);
1236 nvmet_subsys_put(subsys);
1241 static void nvmet_ctrl_free(struct kref *ref)
1243 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1244 struct nvmet_subsys *subsys = ctrl->subsys;
1246 mutex_lock(&subsys->lock);
1247 nvmet_release_p2p_ns_map(ctrl);
1248 list_del(&ctrl->subsys_entry);
1249 mutex_unlock(&subsys->lock);
1251 nvmet_stop_keep_alive_timer(ctrl);
1253 flush_work(&ctrl->async_event_work);
1254 cancel_work_sync(&ctrl->fatal_err_work);
1256 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1260 kfree(ctrl->changed_ns_list);
1263 nvmet_subsys_put(subsys);
1266 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1268 kref_put(&ctrl->ref, nvmet_ctrl_free);
1271 static void nvmet_fatal_error_handler(struct work_struct *work)
1273 struct nvmet_ctrl *ctrl =
1274 container_of(work, struct nvmet_ctrl, fatal_err_work);
1276 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1277 ctrl->ops->delete_ctrl(ctrl);
1280 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1282 mutex_lock(&ctrl->lock);
1283 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1284 ctrl->csts |= NVME_CSTS_CFS;
1285 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1286 schedule_work(&ctrl->fatal_err_work);
1288 mutex_unlock(&ctrl->lock);
1290 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1292 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1293 const char *subsysnqn)
1295 struct nvmet_subsys_link *p;
1300 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1301 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1303 return nvmet_disc_subsys;
1306 down_read(&nvmet_config_sem);
1307 list_for_each_entry(p, &port->subsystems, entry) {
1308 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1310 if (!kref_get_unless_zero(&p->subsys->ref))
1312 up_read(&nvmet_config_sem);
1316 up_read(&nvmet_config_sem);
1320 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1321 enum nvme_subsys_type type)
1323 struct nvmet_subsys *subsys;
1325 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1329 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1330 /* generate a random serial number as our controllers are ephemeral: */
1331 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1335 subsys->max_qid = NVMET_NR_QUEUES;
1338 subsys->max_qid = 0;
1341 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1345 subsys->type = type;
1346 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1348 if (!subsys->subsysnqn) {
1353 kref_init(&subsys->ref);
1355 mutex_init(&subsys->lock);
1356 INIT_LIST_HEAD(&subsys->namespaces);
1357 INIT_LIST_HEAD(&subsys->ctrls);
1358 INIT_LIST_HEAD(&subsys->hosts);
1363 static void nvmet_subsys_free(struct kref *ref)
1365 struct nvmet_subsys *subsys =
1366 container_of(ref, struct nvmet_subsys, ref);
1368 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1370 kfree(subsys->subsysnqn);
1374 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1376 struct nvmet_ctrl *ctrl;
1378 mutex_lock(&subsys->lock);
1379 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1380 ctrl->ops->delete_ctrl(ctrl);
1381 mutex_unlock(&subsys->lock);
1384 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1386 kref_put(&subsys->ref, nvmet_subsys_free);
1389 static int __init nvmet_init(void)
1393 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1395 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1397 if (!buffered_io_wq) {
1402 error = nvmet_init_discovery();
1404 goto out_free_work_queue;
1406 error = nvmet_init_configfs();
1408 goto out_exit_discovery;
1412 nvmet_exit_discovery();
1413 out_free_work_queue:
1414 destroy_workqueue(buffered_io_wq);
1419 static void __exit nvmet_exit(void)
1421 nvmet_exit_configfs();
1422 nvmet_exit_discovery();
1423 ida_destroy(&cntlid_ida);
1424 destroy_workqueue(buffered_io_wq);
1426 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1427 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1430 module_init(nvmet_init);
1431 module_exit(nvmet_exit);
1433 MODULE_LICENSE("GPL v2");