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 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 void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
157 mutex_lock(&ctrl->lock);
158 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
161 for (i = 0; i < ctrl->nr_changed_ns; i++) {
162 if (ctrl->changed_ns_list[i] == nsid)
166 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
167 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
168 ctrl->nr_changed_ns = U32_MAX;
172 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
174 mutex_unlock(&ctrl->lock);
177 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
179 struct nvmet_ctrl *ctrl;
181 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
182 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
183 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
185 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
186 NVME_AER_NOTICE_NS_CHANGED,
187 NVME_LOG_CHANGED_NS);
191 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
192 struct nvmet_port *port)
194 struct nvmet_ctrl *ctrl;
196 mutex_lock(&subsys->lock);
197 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
198 if (port && ctrl->port != port)
200 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
202 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
203 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
205 mutex_unlock(&subsys->lock);
208 void nvmet_port_send_ana_event(struct nvmet_port *port)
210 struct nvmet_subsys_link *p;
212 down_read(&nvmet_config_sem);
213 list_for_each_entry(p, &port->subsystems, entry)
214 nvmet_send_ana_event(p->subsys, port);
215 up_read(&nvmet_config_sem);
218 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
222 down_write(&nvmet_config_sem);
223 if (nvmet_transports[ops->type])
226 nvmet_transports[ops->type] = ops;
227 up_write(&nvmet_config_sem);
231 EXPORT_SYMBOL_GPL(nvmet_register_transport);
233 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
235 down_write(&nvmet_config_sem);
236 nvmet_transports[ops->type] = NULL;
237 up_write(&nvmet_config_sem);
239 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
241 int nvmet_enable_port(struct nvmet_port *port)
243 const struct nvmet_fabrics_ops *ops;
246 lockdep_assert_held(&nvmet_config_sem);
248 ops = nvmet_transports[port->disc_addr.trtype];
250 up_write(&nvmet_config_sem);
251 request_module("nvmet-transport-%d", port->disc_addr.trtype);
252 down_write(&nvmet_config_sem);
253 ops = nvmet_transports[port->disc_addr.trtype];
255 pr_err("transport type %d not supported\n",
256 port->disc_addr.trtype);
261 if (!try_module_get(ops->owner))
264 ret = ops->add_port(port);
266 module_put(ops->owner);
270 /* If the transport didn't set inline_data_size, then disable it. */
271 if (port->inline_data_size < 0)
272 port->inline_data_size = 0;
274 port->enabled = true;
278 void nvmet_disable_port(struct nvmet_port *port)
280 const struct nvmet_fabrics_ops *ops;
282 lockdep_assert_held(&nvmet_config_sem);
284 port->enabled = false;
286 ops = nvmet_transports[port->disc_addr.trtype];
287 ops->remove_port(port);
288 module_put(ops->owner);
291 static void nvmet_keep_alive_timer(struct work_struct *work)
293 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
294 struct nvmet_ctrl, ka_work);
295 bool cmd_seen = ctrl->cmd_seen;
297 ctrl->cmd_seen = false;
299 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
301 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
305 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
306 ctrl->cntlid, ctrl->kato);
308 nvmet_ctrl_fatal_error(ctrl);
311 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
313 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
314 ctrl->cntlid, ctrl->kato);
316 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
317 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
320 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
322 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
324 cancel_delayed_work_sync(&ctrl->ka_work);
327 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
332 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
333 if (ns->nsid == le32_to_cpu(nsid))
340 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
345 ns = __nvmet_find_namespace(ctrl, nsid);
347 percpu_ref_get(&ns->ref);
353 static void nvmet_destroy_namespace(struct percpu_ref *ref)
355 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
357 complete(&ns->disable_done);
360 void nvmet_put_namespace(struct nvmet_ns *ns)
362 percpu_ref_put(&ns->ref);
365 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
367 nvmet_bdev_ns_disable(ns);
368 nvmet_file_ns_disable(ns);
371 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
374 struct pci_dev *p2p_dev;
380 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
384 if (!blk_queue_pci_p2pdma(ns->bdev->bd_queue)) {
385 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
391 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
396 * Right now we just check that there is p2pmem available so
397 * we can report an error to the user right away if there
398 * is not. We'll find the actual device to use once we
399 * setup the controller when the port's device is available.
402 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
404 pr_err("no peer-to-peer memory is available for %s\n",
409 pci_dev_put(p2p_dev);
416 * Note: ctrl->subsys->lock should be held when calling this function
418 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
421 struct device *clients[2];
422 struct pci_dev *p2p_dev;
425 if (!ctrl->p2p_client || !ns->use_p2pmem)
429 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
433 p2p_dev = pci_dev_get(ns->p2p_dev);
435 clients[0] = ctrl->p2p_client;
436 clients[1] = nvmet_ns_dev(ns);
438 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
440 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
441 dev_name(ctrl->p2p_client), ns->device_path);
446 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
448 pci_dev_put(p2p_dev);
450 pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
454 int nvmet_ns_enable(struct nvmet_ns *ns)
456 struct nvmet_subsys *subsys = ns->subsys;
457 struct nvmet_ctrl *ctrl;
460 mutex_lock(&subsys->lock);
462 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
468 ret = nvmet_bdev_ns_enable(ns);
470 ret = nvmet_file_ns_enable(ns);
474 ret = nvmet_p2pmem_ns_enable(ns);
478 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
479 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
481 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
486 if (ns->nsid > subsys->max_nsid)
487 subsys->max_nsid = ns->nsid;
490 * The namespaces list needs to be sorted to simplify the implementation
491 * of the Identify Namepace List subcommand.
493 if (list_empty(&subsys->namespaces)) {
494 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
496 struct nvmet_ns *old;
498 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
499 BUG_ON(ns->nsid == old->nsid);
500 if (ns->nsid < old->nsid)
504 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
506 subsys->nr_namespaces++;
508 nvmet_ns_changed(subsys, ns->nsid);
512 mutex_unlock(&subsys->lock);
515 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
516 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
518 nvmet_ns_dev_disable(ns);
522 void nvmet_ns_disable(struct nvmet_ns *ns)
524 struct nvmet_subsys *subsys = ns->subsys;
525 struct nvmet_ctrl *ctrl;
527 mutex_lock(&subsys->lock);
532 list_del_rcu(&ns->dev_link);
533 if (ns->nsid == subsys->max_nsid)
534 subsys->max_nsid = nvmet_max_nsid(subsys);
536 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
537 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
539 mutex_unlock(&subsys->lock);
542 * Now that we removed the namespaces from the lookup list, we
543 * can kill the per_cpu ref and wait for any remaining references
544 * to be dropped, as well as a RCU grace period for anyone only
545 * using the namepace under rcu_read_lock(). Note that we can't
546 * use call_rcu here as we need to ensure the namespaces have
547 * been fully destroyed before unloading the module.
549 percpu_ref_kill(&ns->ref);
551 wait_for_completion(&ns->disable_done);
552 percpu_ref_exit(&ns->ref);
554 mutex_lock(&subsys->lock);
556 subsys->nr_namespaces--;
557 nvmet_ns_changed(subsys, ns->nsid);
558 nvmet_ns_dev_disable(ns);
560 mutex_unlock(&subsys->lock);
563 void nvmet_ns_free(struct nvmet_ns *ns)
565 nvmet_ns_disable(ns);
567 down_write(&nvmet_ana_sem);
568 nvmet_ana_group_enabled[ns->anagrpid]--;
569 up_write(&nvmet_ana_sem);
571 kfree(ns->device_path);
575 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
579 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
583 INIT_LIST_HEAD(&ns->dev_link);
584 init_completion(&ns->disable_done);
589 down_write(&nvmet_ana_sem);
590 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
591 nvmet_ana_group_enabled[ns->anagrpid]++;
592 up_write(&nvmet_ana_sem);
595 ns->buffered_io = false;
600 static void nvmet_update_sq_head(struct nvmet_req *req)
603 u32 old_sqhd, new_sqhd;
606 old_sqhd = req->sq->sqhd;
607 new_sqhd = (old_sqhd + 1) % req->sq->size;
608 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
611 req->rsp->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
614 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
616 if (!req->sq->sqhd_disabled)
617 nvmet_update_sq_head(req);
618 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
619 req->rsp->command_id = req->cmd->common.command_id;
621 nvmet_set_status(req, status);
623 nvmet_put_namespace(req->ns);
624 req->ops->queue_response(req);
627 void nvmet_req_complete(struct nvmet_req *req, u16 status)
629 __nvmet_req_complete(req, status);
630 percpu_ref_put(&req->sq->ref);
632 EXPORT_SYMBOL_GPL(nvmet_req_complete);
634 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
643 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
653 static void nvmet_confirm_sq(struct percpu_ref *ref)
655 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
657 complete(&sq->confirm_done);
660 void nvmet_sq_destroy(struct nvmet_sq *sq)
663 * If this is the admin queue, complete all AERs so that our
664 * queue doesn't have outstanding requests on it.
666 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
667 nvmet_async_events_free(sq->ctrl);
668 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
669 wait_for_completion(&sq->confirm_done);
670 wait_for_completion(&sq->free_done);
671 percpu_ref_exit(&sq->ref);
674 nvmet_ctrl_put(sq->ctrl);
675 sq->ctrl = NULL; /* allows reusing the queue later */
678 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
680 static void nvmet_sq_free(struct percpu_ref *ref)
682 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
684 complete(&sq->free_done);
687 int nvmet_sq_init(struct nvmet_sq *sq)
691 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
693 pr_err("percpu_ref init failed!\n");
696 init_completion(&sq->free_done);
697 init_completion(&sq->confirm_done);
701 EXPORT_SYMBOL_GPL(nvmet_sq_init);
703 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
706 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
708 if (unlikely(state == NVME_ANA_INACCESSIBLE))
709 return NVME_SC_ANA_INACCESSIBLE;
710 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
711 return NVME_SC_ANA_PERSISTENT_LOSS;
712 if (unlikely(state == NVME_ANA_CHANGE))
713 return NVME_SC_ANA_TRANSITION;
717 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
719 if (unlikely(req->ns->readonly)) {
720 switch (req->cmd->common.opcode) {
725 return NVME_SC_NS_WRITE_PROTECTED;
732 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
734 struct nvme_command *cmd = req->cmd;
737 ret = nvmet_check_ctrl_status(req, cmd);
741 req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
742 if (unlikely(!req->ns))
743 return NVME_SC_INVALID_NS | NVME_SC_DNR;
744 ret = nvmet_check_ana_state(req->port, req->ns);
747 ret = nvmet_io_cmd_check_access(req);
752 return nvmet_file_parse_io_cmd(req);
754 return nvmet_bdev_parse_io_cmd(req);
757 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
758 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
760 u8 flags = req->cmd->common.flags;
768 req->transfer_len = 0;
769 req->rsp->status = 0;
770 req->rsp->sq_head = 0;
773 /* no support for fused commands yet */
774 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
775 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
780 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
781 * contains an address of a single contiguous physical buffer that is
784 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
785 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
789 if (unlikely(!req->sq->ctrl))
790 /* will return an error for any Non-connect command: */
791 status = nvmet_parse_connect_cmd(req);
792 else if (likely(req->sq->qid != 0))
793 status = nvmet_parse_io_cmd(req);
794 else if (req->cmd->common.opcode == nvme_fabrics_command)
795 status = nvmet_parse_fabrics_cmd(req);
796 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
797 status = nvmet_parse_discovery_cmd(req);
799 status = nvmet_parse_admin_cmd(req);
804 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
805 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
810 sq->ctrl->cmd_seen = true;
815 __nvmet_req_complete(req, status);
818 EXPORT_SYMBOL_GPL(nvmet_req_init);
820 void nvmet_req_uninit(struct nvmet_req *req)
822 percpu_ref_put(&req->sq->ref);
824 nvmet_put_namespace(req->ns);
826 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
828 void nvmet_req_execute(struct nvmet_req *req)
830 if (unlikely(req->data_len != req->transfer_len))
831 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
835 EXPORT_SYMBOL_GPL(nvmet_req_execute);
837 int nvmet_req_alloc_sgl(struct nvmet_req *req)
839 struct pci_dev *p2p_dev = NULL;
841 if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
842 if (req->sq->ctrl && req->ns)
843 p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
847 if (req->sq->qid && p2p_dev) {
848 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
851 req->p2p_dev = p2p_dev;
857 * If no P2P memory was available we fallback to using
862 req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
868 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
870 void nvmet_req_free_sgl(struct nvmet_req *req)
873 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
880 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
882 static inline bool nvmet_cc_en(u32 cc)
884 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
887 static inline u8 nvmet_cc_css(u32 cc)
889 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
892 static inline u8 nvmet_cc_mps(u32 cc)
894 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
897 static inline u8 nvmet_cc_ams(u32 cc)
899 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
902 static inline u8 nvmet_cc_shn(u32 cc)
904 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
907 static inline u8 nvmet_cc_iosqes(u32 cc)
909 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
912 static inline u8 nvmet_cc_iocqes(u32 cc)
914 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
917 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
919 lockdep_assert_held(&ctrl->lock);
921 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
922 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
923 nvmet_cc_mps(ctrl->cc) != 0 ||
924 nvmet_cc_ams(ctrl->cc) != 0 ||
925 nvmet_cc_css(ctrl->cc) != 0) {
926 ctrl->csts = NVME_CSTS_CFS;
930 ctrl->csts = NVME_CSTS_RDY;
933 * Controllers that are not yet enabled should not really enforce the
934 * keep alive timeout, but we still want to track a timeout and cleanup
935 * in case a host died before it enabled the controller. Hence, simply
936 * reset the keep alive timer when the controller is enabled.
938 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
941 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
943 lockdep_assert_held(&ctrl->lock);
945 /* XXX: tear down queues? */
946 ctrl->csts &= ~NVME_CSTS_RDY;
950 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
954 mutex_lock(&ctrl->lock);
958 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
959 nvmet_start_ctrl(ctrl);
960 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
961 nvmet_clear_ctrl(ctrl);
962 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
963 nvmet_clear_ctrl(ctrl);
964 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
966 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
967 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
968 mutex_unlock(&ctrl->lock);
971 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
973 /* command sets supported: NVMe command set: */
974 ctrl->cap = (1ULL << 37);
975 /* CC.EN timeout in 500msec units: */
976 ctrl->cap |= (15ULL << 24);
977 /* maximum queue entries supported: */
978 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
981 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
982 struct nvmet_req *req, struct nvmet_ctrl **ret)
984 struct nvmet_subsys *subsys;
985 struct nvmet_ctrl *ctrl;
988 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
990 pr_warn("connect request for invalid subsystem %s!\n",
992 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
993 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
996 mutex_lock(&subsys->lock);
997 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
998 if (ctrl->cntlid == cntlid) {
999 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1000 pr_warn("hostnqn mismatch.\n");
1003 if (!kref_get_unless_zero(&ctrl->ref))
1011 pr_warn("could not find controller %d for subsys %s / host %s\n",
1012 cntlid, subsysnqn, hostnqn);
1013 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1014 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1017 mutex_unlock(&subsys->lock);
1018 nvmet_subsys_put(subsys);
1022 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1024 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1025 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1026 cmd->common.opcode, req->sq->qid);
1027 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1030 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1031 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1032 cmd->common.opcode, req->sq->qid);
1033 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1038 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
1040 struct nvmet_host_link *p;
1042 lockdep_assert_held(&nvmet_config_sem);
1044 if (subsys->allow_any_host)
1047 if (subsys->type == NVME_NQN_DISC) /* allow all access to disc subsys */
1050 list_for_each_entry(p, &subsys->hosts, entry) {
1051 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1059 * Note: ctrl->subsys->lock should be held when calling this function
1061 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1062 struct nvmet_req *req)
1064 struct nvmet_ns *ns;
1066 if (!req->p2p_client)
1069 ctrl->p2p_client = get_device(req->p2p_client);
1071 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1072 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1076 * Note: ctrl->subsys->lock should be held when calling this function
1078 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1080 struct radix_tree_iter iter;
1083 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1084 pci_dev_put(radix_tree_deref_slot(slot));
1086 put_device(ctrl->p2p_client);
1089 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1090 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1092 struct nvmet_subsys *subsys;
1093 struct nvmet_ctrl *ctrl;
1097 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1098 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1100 pr_warn("connect request for invalid subsystem %s!\n",
1102 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1106 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1107 down_read(&nvmet_config_sem);
1108 if (!nvmet_host_allowed(subsys, hostnqn)) {
1109 pr_info("connect by host %s for subsystem %s not allowed\n",
1110 hostnqn, subsysnqn);
1111 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1112 up_read(&nvmet_config_sem);
1113 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1114 goto out_put_subsystem;
1116 up_read(&nvmet_config_sem);
1118 status = NVME_SC_INTERNAL;
1119 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1121 goto out_put_subsystem;
1122 mutex_init(&ctrl->lock);
1124 nvmet_init_cap(ctrl);
1126 ctrl->port = req->port;
1128 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1129 INIT_LIST_HEAD(&ctrl->async_events);
1130 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1132 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1133 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1135 kref_init(&ctrl->ref);
1136 ctrl->subsys = subsys;
1137 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1139 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1140 sizeof(__le32), GFP_KERNEL);
1141 if (!ctrl->changed_ns_list)
1144 ctrl->cqs = kcalloc(subsys->max_qid + 1,
1145 sizeof(struct nvmet_cq *),
1148 goto out_free_changed_ns_list;
1150 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1151 sizeof(struct nvmet_sq *),
1156 ret = ida_simple_get(&cntlid_ida,
1157 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1160 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1165 ctrl->ops = req->ops;
1168 * Discovery controllers may use some arbitrary high value
1169 * in order to cleanup stale discovery sessions
1171 if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1172 kato = NVMET_DISC_KATO_MS;
1174 /* keep-alive timeout in seconds */
1175 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1177 nvmet_start_keep_alive_timer(ctrl);
1179 mutex_lock(&subsys->lock);
1180 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1181 nvmet_setup_p2p_ns_map(ctrl, req);
1182 mutex_unlock(&subsys->lock);
1191 out_free_changed_ns_list:
1192 kfree(ctrl->changed_ns_list);
1196 nvmet_subsys_put(subsys);
1201 static void nvmet_ctrl_free(struct kref *ref)
1203 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1204 struct nvmet_subsys *subsys = ctrl->subsys;
1206 mutex_lock(&subsys->lock);
1207 nvmet_release_p2p_ns_map(ctrl);
1208 list_del(&ctrl->subsys_entry);
1209 mutex_unlock(&subsys->lock);
1211 nvmet_stop_keep_alive_timer(ctrl);
1213 flush_work(&ctrl->async_event_work);
1214 cancel_work_sync(&ctrl->fatal_err_work);
1216 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1220 kfree(ctrl->changed_ns_list);
1223 nvmet_subsys_put(subsys);
1226 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1228 kref_put(&ctrl->ref, nvmet_ctrl_free);
1231 static void nvmet_fatal_error_handler(struct work_struct *work)
1233 struct nvmet_ctrl *ctrl =
1234 container_of(work, struct nvmet_ctrl, fatal_err_work);
1236 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1237 ctrl->ops->delete_ctrl(ctrl);
1240 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1242 mutex_lock(&ctrl->lock);
1243 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1244 ctrl->csts |= NVME_CSTS_CFS;
1245 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1246 schedule_work(&ctrl->fatal_err_work);
1248 mutex_unlock(&ctrl->lock);
1250 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1252 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1253 const char *subsysnqn)
1255 struct nvmet_subsys_link *p;
1260 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1261 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1263 return nvmet_disc_subsys;
1266 down_read(&nvmet_config_sem);
1267 list_for_each_entry(p, &port->subsystems, entry) {
1268 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1270 if (!kref_get_unless_zero(&p->subsys->ref))
1272 up_read(&nvmet_config_sem);
1276 up_read(&nvmet_config_sem);
1280 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1281 enum nvme_subsys_type type)
1283 struct nvmet_subsys *subsys;
1285 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1289 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1290 /* generate a random serial number as our controllers are ephemeral: */
1291 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1295 subsys->max_qid = NVMET_NR_QUEUES;
1298 subsys->max_qid = 0;
1301 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1305 subsys->type = type;
1306 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1308 if (!subsys->subsysnqn) {
1313 kref_init(&subsys->ref);
1315 mutex_init(&subsys->lock);
1316 INIT_LIST_HEAD(&subsys->namespaces);
1317 INIT_LIST_HEAD(&subsys->ctrls);
1318 INIT_LIST_HEAD(&subsys->hosts);
1323 static void nvmet_subsys_free(struct kref *ref)
1325 struct nvmet_subsys *subsys =
1326 container_of(ref, struct nvmet_subsys, ref);
1328 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1330 kfree(subsys->subsysnqn);
1334 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1336 struct nvmet_ctrl *ctrl;
1338 mutex_lock(&subsys->lock);
1339 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1340 ctrl->ops->delete_ctrl(ctrl);
1341 mutex_unlock(&subsys->lock);
1344 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1346 kref_put(&subsys->ref, nvmet_subsys_free);
1349 static int __init nvmet_init(void)
1353 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1355 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1357 if (!buffered_io_wq) {
1362 error = nvmet_init_discovery();
1364 goto out_free_work_queue;
1366 error = nvmet_init_configfs();
1368 goto out_exit_discovery;
1372 nvmet_exit_discovery();
1373 out_free_work_queue:
1374 destroy_workqueue(buffered_io_wq);
1379 static void __exit nvmet_exit(void)
1381 nvmet_exit_configfs();
1382 nvmet_exit_discovery();
1383 ida_destroy(&cntlid_ida);
1384 destroy_workqueue(buffered_io_wq);
1386 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1387 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1390 module_init(nvmet_init);
1391 module_exit(nvmet_exit);
1393 MODULE_LICENSE("GPL v2");