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 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_req_complete(struct nvmet_req *req, u16 status)
602 u32 old_sqhd, new_sqhd;
606 nvmet_set_status(req, status);
610 old_sqhd = req->sq->sqhd;
611 new_sqhd = (old_sqhd + 1) % req->sq->size;
612 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
615 sqhd = req->sq->sqhd & 0x0000FFFF;
616 req->rsp->sq_head = cpu_to_le16(sqhd);
617 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
618 req->rsp->command_id = req->cmd->common.command_id;
621 nvmet_put_namespace(req->ns);
622 req->ops->queue_response(req);
625 void nvmet_req_complete(struct nvmet_req *req, u16 status)
627 __nvmet_req_complete(req, status);
628 percpu_ref_put(&req->sq->ref);
630 EXPORT_SYMBOL_GPL(nvmet_req_complete);
632 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
641 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
651 static void nvmet_confirm_sq(struct percpu_ref *ref)
653 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
655 complete(&sq->confirm_done);
658 void nvmet_sq_destroy(struct nvmet_sq *sq)
661 * If this is the admin queue, complete all AERs so that our
662 * queue doesn't have outstanding requests on it.
664 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
665 nvmet_async_events_free(sq->ctrl);
666 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
667 wait_for_completion(&sq->confirm_done);
668 wait_for_completion(&sq->free_done);
669 percpu_ref_exit(&sq->ref);
672 nvmet_ctrl_put(sq->ctrl);
673 sq->ctrl = NULL; /* allows reusing the queue later */
676 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
678 static void nvmet_sq_free(struct percpu_ref *ref)
680 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
682 complete(&sq->free_done);
685 int nvmet_sq_init(struct nvmet_sq *sq)
689 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
691 pr_err("percpu_ref init failed!\n");
694 init_completion(&sq->free_done);
695 init_completion(&sq->confirm_done);
699 EXPORT_SYMBOL_GPL(nvmet_sq_init);
701 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
704 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
706 if (unlikely(state == NVME_ANA_INACCESSIBLE))
707 return NVME_SC_ANA_INACCESSIBLE;
708 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
709 return NVME_SC_ANA_PERSISTENT_LOSS;
710 if (unlikely(state == NVME_ANA_CHANGE))
711 return NVME_SC_ANA_TRANSITION;
715 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
717 if (unlikely(req->ns->readonly)) {
718 switch (req->cmd->common.opcode) {
723 return NVME_SC_NS_WRITE_PROTECTED;
730 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
732 struct nvme_command *cmd = req->cmd;
735 ret = nvmet_check_ctrl_status(req, cmd);
739 req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
740 if (unlikely(!req->ns))
741 return NVME_SC_INVALID_NS | NVME_SC_DNR;
742 ret = nvmet_check_ana_state(req->port, req->ns);
745 ret = nvmet_io_cmd_check_access(req);
750 return nvmet_file_parse_io_cmd(req);
752 return nvmet_bdev_parse_io_cmd(req);
755 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
756 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
758 u8 flags = req->cmd->common.flags;
766 req->transfer_len = 0;
767 req->rsp->status = 0;
770 /* no support for fused commands yet */
771 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
772 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
777 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
778 * contains an address of a single contiguous physical buffer that is
781 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
782 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
786 if (unlikely(!req->sq->ctrl))
787 /* will return an error for any Non-connect command: */
788 status = nvmet_parse_connect_cmd(req);
789 else if (likely(req->sq->qid != 0))
790 status = nvmet_parse_io_cmd(req);
791 else if (req->cmd->common.opcode == nvme_fabrics_command)
792 status = nvmet_parse_fabrics_cmd(req);
793 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
794 status = nvmet_parse_discovery_cmd(req);
796 status = nvmet_parse_admin_cmd(req);
801 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
802 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
807 sq->ctrl->cmd_seen = true;
812 __nvmet_req_complete(req, status);
815 EXPORT_SYMBOL_GPL(nvmet_req_init);
817 void nvmet_req_uninit(struct nvmet_req *req)
819 percpu_ref_put(&req->sq->ref);
821 nvmet_put_namespace(req->ns);
823 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
825 void nvmet_req_execute(struct nvmet_req *req)
827 if (unlikely(req->data_len != req->transfer_len))
828 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
832 EXPORT_SYMBOL_GPL(nvmet_req_execute);
834 int nvmet_req_alloc_sgl(struct nvmet_req *req)
836 struct pci_dev *p2p_dev = NULL;
838 if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
839 if (req->sq->ctrl && req->ns)
840 p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
844 if (req->sq->qid && p2p_dev) {
845 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
848 req->p2p_dev = p2p_dev;
854 * If no P2P memory was available we fallback to using
859 req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
865 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
867 void nvmet_req_free_sgl(struct nvmet_req *req)
870 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
877 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
879 static inline bool nvmet_cc_en(u32 cc)
881 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
884 static inline u8 nvmet_cc_css(u32 cc)
886 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
889 static inline u8 nvmet_cc_mps(u32 cc)
891 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
894 static inline u8 nvmet_cc_ams(u32 cc)
896 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
899 static inline u8 nvmet_cc_shn(u32 cc)
901 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
904 static inline u8 nvmet_cc_iosqes(u32 cc)
906 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
909 static inline u8 nvmet_cc_iocqes(u32 cc)
911 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
914 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
916 lockdep_assert_held(&ctrl->lock);
918 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
919 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
920 nvmet_cc_mps(ctrl->cc) != 0 ||
921 nvmet_cc_ams(ctrl->cc) != 0 ||
922 nvmet_cc_css(ctrl->cc) != 0) {
923 ctrl->csts = NVME_CSTS_CFS;
927 ctrl->csts = NVME_CSTS_RDY;
930 * Controllers that are not yet enabled should not really enforce the
931 * keep alive timeout, but we still want to track a timeout and cleanup
932 * in case a host died before it enabled the controller. Hence, simply
933 * reset the keep alive timer when the controller is enabled.
935 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
938 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
940 lockdep_assert_held(&ctrl->lock);
942 /* XXX: tear down queues? */
943 ctrl->csts &= ~NVME_CSTS_RDY;
947 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
951 mutex_lock(&ctrl->lock);
955 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
956 nvmet_start_ctrl(ctrl);
957 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
958 nvmet_clear_ctrl(ctrl);
959 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
960 nvmet_clear_ctrl(ctrl);
961 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
963 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
964 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
965 mutex_unlock(&ctrl->lock);
968 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
970 /* command sets supported: NVMe command set: */
971 ctrl->cap = (1ULL << 37);
972 /* CC.EN timeout in 500msec units: */
973 ctrl->cap |= (15ULL << 24);
974 /* maximum queue entries supported: */
975 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
978 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
979 struct nvmet_req *req, struct nvmet_ctrl **ret)
981 struct nvmet_subsys *subsys;
982 struct nvmet_ctrl *ctrl;
985 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
987 pr_warn("connect request for invalid subsystem %s!\n",
989 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
990 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
993 mutex_lock(&subsys->lock);
994 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
995 if (ctrl->cntlid == cntlid) {
996 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
997 pr_warn("hostnqn mismatch.\n");
1000 if (!kref_get_unless_zero(&ctrl->ref))
1008 pr_warn("could not find controller %d for subsys %s / host %s\n",
1009 cntlid, subsysnqn, hostnqn);
1010 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1011 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1014 mutex_unlock(&subsys->lock);
1015 nvmet_subsys_put(subsys);
1019 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1021 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1022 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1023 cmd->common.opcode, req->sq->qid);
1024 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1027 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1028 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1029 cmd->common.opcode, req->sq->qid);
1030 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1035 static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
1036 const char *hostnqn)
1038 struct nvmet_host_link *p;
1040 if (subsys->allow_any_host)
1043 list_for_each_entry(p, &subsys->hosts, entry) {
1044 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1051 static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
1052 const char *hostnqn)
1054 struct nvmet_subsys_link *s;
1056 list_for_each_entry(s, &req->port->subsystems, entry) {
1057 if (__nvmet_host_allowed(s->subsys, hostnqn))
1064 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
1065 const char *hostnqn)
1067 lockdep_assert_held(&nvmet_config_sem);
1069 if (subsys->type == NVME_NQN_DISC)
1070 return nvmet_host_discovery_allowed(req, hostnqn);
1072 return __nvmet_host_allowed(subsys, hostnqn);
1076 * Note: ctrl->subsys->lock should be held when calling this function
1078 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1079 struct nvmet_req *req)
1081 struct nvmet_ns *ns;
1083 if (!req->p2p_client)
1086 ctrl->p2p_client = get_device(req->p2p_client);
1088 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1089 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1093 * Note: ctrl->subsys->lock should be held when calling this function
1095 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1097 struct radix_tree_iter iter;
1100 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1101 pci_dev_put(radix_tree_deref_slot(slot));
1103 put_device(ctrl->p2p_client);
1106 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1107 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1109 struct nvmet_subsys *subsys;
1110 struct nvmet_ctrl *ctrl;
1114 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1115 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1117 pr_warn("connect request for invalid subsystem %s!\n",
1119 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1123 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1124 down_read(&nvmet_config_sem);
1125 if (!nvmet_host_allowed(req, subsys, hostnqn)) {
1126 pr_info("connect by host %s for subsystem %s not allowed\n",
1127 hostnqn, subsysnqn);
1128 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1129 up_read(&nvmet_config_sem);
1130 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1131 goto out_put_subsystem;
1133 up_read(&nvmet_config_sem);
1135 status = NVME_SC_INTERNAL;
1136 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1138 goto out_put_subsystem;
1139 mutex_init(&ctrl->lock);
1141 nvmet_init_cap(ctrl);
1143 ctrl->port = req->port;
1145 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1146 INIT_LIST_HEAD(&ctrl->async_events);
1147 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1149 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1150 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1152 kref_init(&ctrl->ref);
1153 ctrl->subsys = subsys;
1154 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1156 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1157 sizeof(__le32), GFP_KERNEL);
1158 if (!ctrl->changed_ns_list)
1161 ctrl->cqs = kcalloc(subsys->max_qid + 1,
1162 sizeof(struct nvmet_cq *),
1165 goto out_free_changed_ns_list;
1167 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1168 sizeof(struct nvmet_sq *),
1173 ret = ida_simple_get(&cntlid_ida,
1174 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1177 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1182 ctrl->ops = req->ops;
1185 * Discovery controllers may use some arbitrary high value
1186 * in order to cleanup stale discovery sessions
1188 if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1189 kato = NVMET_DISC_KATO_MS;
1191 /* keep-alive timeout in seconds */
1192 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1194 nvmet_start_keep_alive_timer(ctrl);
1196 mutex_lock(&subsys->lock);
1197 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1198 nvmet_setup_p2p_ns_map(ctrl, req);
1199 mutex_unlock(&subsys->lock);
1208 out_free_changed_ns_list:
1209 kfree(ctrl->changed_ns_list);
1213 nvmet_subsys_put(subsys);
1218 static void nvmet_ctrl_free(struct kref *ref)
1220 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1221 struct nvmet_subsys *subsys = ctrl->subsys;
1223 mutex_lock(&subsys->lock);
1224 nvmet_release_p2p_ns_map(ctrl);
1225 list_del(&ctrl->subsys_entry);
1226 mutex_unlock(&subsys->lock);
1228 nvmet_stop_keep_alive_timer(ctrl);
1230 flush_work(&ctrl->async_event_work);
1231 cancel_work_sync(&ctrl->fatal_err_work);
1233 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1237 kfree(ctrl->changed_ns_list);
1240 nvmet_subsys_put(subsys);
1243 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1245 kref_put(&ctrl->ref, nvmet_ctrl_free);
1248 static void nvmet_fatal_error_handler(struct work_struct *work)
1250 struct nvmet_ctrl *ctrl =
1251 container_of(work, struct nvmet_ctrl, fatal_err_work);
1253 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1254 ctrl->ops->delete_ctrl(ctrl);
1257 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1259 mutex_lock(&ctrl->lock);
1260 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1261 ctrl->csts |= NVME_CSTS_CFS;
1262 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1263 schedule_work(&ctrl->fatal_err_work);
1265 mutex_unlock(&ctrl->lock);
1267 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1269 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1270 const char *subsysnqn)
1272 struct nvmet_subsys_link *p;
1277 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1278 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1280 return nvmet_disc_subsys;
1283 down_read(&nvmet_config_sem);
1284 list_for_each_entry(p, &port->subsystems, entry) {
1285 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1287 if (!kref_get_unless_zero(&p->subsys->ref))
1289 up_read(&nvmet_config_sem);
1293 up_read(&nvmet_config_sem);
1297 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1298 enum nvme_subsys_type type)
1300 struct nvmet_subsys *subsys;
1302 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1306 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1307 /* generate a random serial number as our controllers are ephemeral: */
1308 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1312 subsys->max_qid = NVMET_NR_QUEUES;
1315 subsys->max_qid = 0;
1318 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1322 subsys->type = type;
1323 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1325 if (!subsys->subsysnqn) {
1330 kref_init(&subsys->ref);
1332 mutex_init(&subsys->lock);
1333 INIT_LIST_HEAD(&subsys->namespaces);
1334 INIT_LIST_HEAD(&subsys->ctrls);
1335 INIT_LIST_HEAD(&subsys->hosts);
1340 static void nvmet_subsys_free(struct kref *ref)
1342 struct nvmet_subsys *subsys =
1343 container_of(ref, struct nvmet_subsys, ref);
1345 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1347 kfree(subsys->subsysnqn);
1351 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1353 struct nvmet_ctrl *ctrl;
1355 mutex_lock(&subsys->lock);
1356 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1357 ctrl->ops->delete_ctrl(ctrl);
1358 mutex_unlock(&subsys->lock);
1361 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1363 kref_put(&subsys->ref, nvmet_subsys_free);
1366 static int __init nvmet_init(void)
1370 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1372 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1374 if (!buffered_io_wq) {
1379 error = nvmet_init_discovery();
1381 goto out_free_work_queue;
1383 error = nvmet_init_configfs();
1385 goto out_exit_discovery;
1389 nvmet_exit_discovery();
1390 out_free_work_queue:
1391 destroy_workqueue(buffered_io_wq);
1396 static void __exit nvmet_exit(void)
1398 nvmet_exit_configfs();
1399 nvmet_exit_discovery();
1400 ida_destroy(&cntlid_ida);
1401 destroy_workqueue(buffered_io_wq);
1403 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1404 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1407 module_init(nvmet_init);
1408 module_exit(nvmet_exit);
1410 MODULE_LICENSE("GPL v2");