1 // SPDX-License-Identifier: GPL-2.0
3 * Common code for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/random.h>
9 #include <linux/rculist.h>
10 #include <linux/pci-p2pdma.h>
14 struct workqueue_struct *buffered_io_wq;
15 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
16 static DEFINE_IDA(cntlid_ida);
19 * This read/write semaphore is used to synchronize access to configuration
20 * information on a target system that will result in discovery log page
21 * information change for at least one host.
22 * The full list of resources to protected by this semaphore is:
25 * - per-subsystem allowed hosts list
26 * - allow_any_host subsystem attribute
28 * - the nvmet_transports array
30 * When updating any of those lists/structures write lock should be obtained,
31 * while when reading (popolating discovery log page or checking host-subsystem
32 * link) read lock is obtained to allow concurrent reads.
34 DECLARE_RWSEM(nvmet_config_sem);
36 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
38 DECLARE_RWSEM(nvmet_ana_sem);
40 inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno)
46 req->error_loc = offsetof(struct nvme_rw_command, length);
47 status = NVME_SC_CAP_EXCEEDED | NVME_SC_DNR;
50 req->error_loc = offsetof(struct nvme_rw_command, slba);
51 status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
54 req->error_loc = offsetof(struct nvme_common_command, opcode);
55 switch (req->cmd->common.opcode) {
57 case nvme_cmd_write_zeroes:
58 status = NVME_SC_ONCS_NOT_SUPPORTED | NVME_SC_DNR;
61 status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
65 req->error_loc = offsetof(struct nvme_rw_command, nsid);
66 status = NVME_SC_ACCESS_DENIED;
71 req->error_loc = offsetof(struct nvme_common_command, opcode);
72 status = NVME_SC_INTERNAL | NVME_SC_DNR;
78 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
79 const char *subsysnqn);
81 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
84 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
85 req->error_loc = offsetof(struct nvme_common_command, dptr);
86 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
91 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
93 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
94 req->error_loc = offsetof(struct nvme_common_command, dptr);
95 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
100 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
102 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) {
103 req->error_loc = offsetof(struct nvme_common_command, dptr);
104 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
109 static unsigned int nvmet_max_nsid(struct nvmet_subsys *subsys)
113 if (list_empty(&subsys->namespaces))
116 ns = list_last_entry(&subsys->namespaces, struct nvmet_ns, dev_link);
120 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
122 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
125 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
127 struct nvmet_req *req;
130 mutex_lock(&ctrl->lock);
131 if (!ctrl->nr_async_event_cmds) {
132 mutex_unlock(&ctrl->lock);
136 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
137 mutex_unlock(&ctrl->lock);
138 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
142 static void nvmet_async_event_work(struct work_struct *work)
144 struct nvmet_ctrl *ctrl =
145 container_of(work, struct nvmet_ctrl, async_event_work);
146 struct nvmet_async_event *aen;
147 struct nvmet_req *req;
150 mutex_lock(&ctrl->lock);
151 aen = list_first_entry_or_null(&ctrl->async_events,
152 struct nvmet_async_event, entry);
153 if (!aen || !ctrl->nr_async_event_cmds) {
154 mutex_unlock(&ctrl->lock);
158 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
159 nvmet_set_result(req, nvmet_async_event_result(aen));
161 list_del(&aen->entry);
164 mutex_unlock(&ctrl->lock);
165 nvmet_req_complete(req, 0);
169 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
170 u8 event_info, u8 log_page)
172 struct nvmet_async_event *aen;
174 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
178 aen->event_type = event_type;
179 aen->event_info = event_info;
180 aen->log_page = log_page;
182 mutex_lock(&ctrl->lock);
183 list_add_tail(&aen->entry, &ctrl->async_events);
184 mutex_unlock(&ctrl->lock);
186 schedule_work(&ctrl->async_event_work);
189 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
193 mutex_lock(&ctrl->lock);
194 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
197 for (i = 0; i < ctrl->nr_changed_ns; i++) {
198 if (ctrl->changed_ns_list[i] == nsid)
202 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
203 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
204 ctrl->nr_changed_ns = U32_MAX;
208 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
210 mutex_unlock(&ctrl->lock);
213 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
215 struct nvmet_ctrl *ctrl;
217 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
218 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
219 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
221 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
222 NVME_AER_NOTICE_NS_CHANGED,
223 NVME_LOG_CHANGED_NS);
227 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
228 struct nvmet_port *port)
230 struct nvmet_ctrl *ctrl;
232 mutex_lock(&subsys->lock);
233 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
234 if (port && ctrl->port != port)
236 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
238 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
239 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
241 mutex_unlock(&subsys->lock);
244 void nvmet_port_send_ana_event(struct nvmet_port *port)
246 struct nvmet_subsys_link *p;
248 down_read(&nvmet_config_sem);
249 list_for_each_entry(p, &port->subsystems, entry)
250 nvmet_send_ana_event(p->subsys, port);
251 up_read(&nvmet_config_sem);
254 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
258 down_write(&nvmet_config_sem);
259 if (nvmet_transports[ops->type])
262 nvmet_transports[ops->type] = ops;
263 up_write(&nvmet_config_sem);
267 EXPORT_SYMBOL_GPL(nvmet_register_transport);
269 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
271 down_write(&nvmet_config_sem);
272 nvmet_transports[ops->type] = NULL;
273 up_write(&nvmet_config_sem);
275 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
277 int nvmet_enable_port(struct nvmet_port *port)
279 const struct nvmet_fabrics_ops *ops;
282 lockdep_assert_held(&nvmet_config_sem);
284 ops = nvmet_transports[port->disc_addr.trtype];
286 up_write(&nvmet_config_sem);
287 request_module("nvmet-transport-%d", port->disc_addr.trtype);
288 down_write(&nvmet_config_sem);
289 ops = nvmet_transports[port->disc_addr.trtype];
291 pr_err("transport type %d not supported\n",
292 port->disc_addr.trtype);
297 if (!try_module_get(ops->owner))
300 ret = ops->add_port(port);
302 module_put(ops->owner);
306 /* If the transport didn't set inline_data_size, then disable it. */
307 if (port->inline_data_size < 0)
308 port->inline_data_size = 0;
310 port->enabled = true;
314 void nvmet_disable_port(struct nvmet_port *port)
316 const struct nvmet_fabrics_ops *ops;
318 lockdep_assert_held(&nvmet_config_sem);
320 port->enabled = false;
322 ops = nvmet_transports[port->disc_addr.trtype];
323 ops->remove_port(port);
324 module_put(ops->owner);
327 static void nvmet_keep_alive_timer(struct work_struct *work)
329 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
330 struct nvmet_ctrl, ka_work);
331 bool cmd_seen = ctrl->cmd_seen;
333 ctrl->cmd_seen = false;
335 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
337 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
341 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
342 ctrl->cntlid, ctrl->kato);
344 nvmet_ctrl_fatal_error(ctrl);
347 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
349 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
350 ctrl->cntlid, ctrl->kato);
352 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
353 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
356 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
358 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
360 cancel_delayed_work_sync(&ctrl->ka_work);
363 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
368 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
369 if (ns->nsid == le32_to_cpu(nsid))
376 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
381 ns = __nvmet_find_namespace(ctrl, nsid);
383 percpu_ref_get(&ns->ref);
389 static void nvmet_destroy_namespace(struct percpu_ref *ref)
391 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
393 complete(&ns->disable_done);
396 void nvmet_put_namespace(struct nvmet_ns *ns)
398 percpu_ref_put(&ns->ref);
401 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
403 nvmet_bdev_ns_disable(ns);
404 nvmet_file_ns_disable(ns);
407 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
410 struct pci_dev *p2p_dev;
416 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
420 if (!blk_queue_pci_p2pdma(ns->bdev->bd_queue)) {
421 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
427 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
432 * Right now we just check that there is p2pmem available so
433 * we can report an error to the user right away if there
434 * is not. We'll find the actual device to use once we
435 * setup the controller when the port's device is available.
438 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
440 pr_err("no peer-to-peer memory is available for %s\n",
445 pci_dev_put(p2p_dev);
452 * Note: ctrl->subsys->lock should be held when calling this function
454 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
457 struct device *clients[2];
458 struct pci_dev *p2p_dev;
461 if (!ctrl->p2p_client || !ns->use_p2pmem)
465 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
469 p2p_dev = pci_dev_get(ns->p2p_dev);
471 clients[0] = ctrl->p2p_client;
472 clients[1] = nvmet_ns_dev(ns);
474 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
476 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
477 dev_name(ctrl->p2p_client), ns->device_path);
482 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
484 pci_dev_put(p2p_dev);
486 pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
490 int nvmet_ns_enable(struct nvmet_ns *ns)
492 struct nvmet_subsys *subsys = ns->subsys;
493 struct nvmet_ctrl *ctrl;
496 mutex_lock(&subsys->lock);
498 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
504 ret = nvmet_bdev_ns_enable(ns);
506 ret = nvmet_file_ns_enable(ns);
510 ret = nvmet_p2pmem_ns_enable(ns);
514 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
515 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
517 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
522 if (ns->nsid > subsys->max_nsid)
523 subsys->max_nsid = ns->nsid;
526 * The namespaces list needs to be sorted to simplify the implementation
527 * of the Identify Namepace List subcommand.
529 if (list_empty(&subsys->namespaces)) {
530 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
532 struct nvmet_ns *old;
534 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
535 BUG_ON(ns->nsid == old->nsid);
536 if (ns->nsid < old->nsid)
540 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
542 subsys->nr_namespaces++;
544 nvmet_ns_changed(subsys, ns->nsid);
548 mutex_unlock(&subsys->lock);
551 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
552 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
554 nvmet_ns_dev_disable(ns);
558 void nvmet_ns_disable(struct nvmet_ns *ns)
560 struct nvmet_subsys *subsys = ns->subsys;
561 struct nvmet_ctrl *ctrl;
563 mutex_lock(&subsys->lock);
568 list_del_rcu(&ns->dev_link);
569 if (ns->nsid == subsys->max_nsid)
570 subsys->max_nsid = nvmet_max_nsid(subsys);
572 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
573 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
575 mutex_unlock(&subsys->lock);
578 * Now that we removed the namespaces from the lookup list, we
579 * can kill the per_cpu ref and wait for any remaining references
580 * to be dropped, as well as a RCU grace period for anyone only
581 * using the namepace under rcu_read_lock(). Note that we can't
582 * use call_rcu here as we need to ensure the namespaces have
583 * been fully destroyed before unloading the module.
585 percpu_ref_kill(&ns->ref);
587 wait_for_completion(&ns->disable_done);
588 percpu_ref_exit(&ns->ref);
590 mutex_lock(&subsys->lock);
592 subsys->nr_namespaces--;
593 nvmet_ns_changed(subsys, ns->nsid);
594 nvmet_ns_dev_disable(ns);
596 mutex_unlock(&subsys->lock);
599 void nvmet_ns_free(struct nvmet_ns *ns)
601 nvmet_ns_disable(ns);
603 down_write(&nvmet_ana_sem);
604 nvmet_ana_group_enabled[ns->anagrpid]--;
605 up_write(&nvmet_ana_sem);
607 kfree(ns->device_path);
611 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
615 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
619 INIT_LIST_HEAD(&ns->dev_link);
620 init_completion(&ns->disable_done);
625 down_write(&nvmet_ana_sem);
626 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
627 nvmet_ana_group_enabled[ns->anagrpid]++;
628 up_write(&nvmet_ana_sem);
631 ns->buffered_io = false;
636 static void nvmet_update_sq_head(struct nvmet_req *req)
639 u32 old_sqhd, new_sqhd;
642 old_sqhd = req->sq->sqhd;
643 new_sqhd = (old_sqhd + 1) % req->sq->size;
644 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
647 req->rsp->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
650 static void nvmet_set_error(struct nvmet_req *req, u16 status)
652 struct nvmet_ctrl *ctrl = req->sq->ctrl;
653 struct nvme_error_slot *new_error_slot;
656 req->rsp->status = cpu_to_le16(status << 1);
658 if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC)
661 spin_lock_irqsave(&ctrl->error_lock, flags);
664 &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS];
666 new_error_slot->error_count = cpu_to_le64(ctrl->err_counter);
667 new_error_slot->sqid = cpu_to_le16(req->sq->qid);
668 new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id);
669 new_error_slot->status_field = cpu_to_le16(status << 1);
670 new_error_slot->param_error_location = cpu_to_le16(req->error_loc);
671 new_error_slot->lba = cpu_to_le64(req->error_slba);
672 new_error_slot->nsid = req->cmd->common.nsid;
673 spin_unlock_irqrestore(&ctrl->error_lock, flags);
675 /* set the more bit for this request */
676 req->rsp->status |= cpu_to_le16(1 << 14);
679 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
681 if (!req->sq->sqhd_disabled)
682 nvmet_update_sq_head(req);
683 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
684 req->rsp->command_id = req->cmd->common.command_id;
686 if (unlikely(status))
687 nvmet_set_error(req, status);
689 nvmet_put_namespace(req->ns);
690 req->ops->queue_response(req);
693 void nvmet_req_complete(struct nvmet_req *req, u16 status)
695 __nvmet_req_complete(req, status);
696 percpu_ref_put(&req->sq->ref);
698 EXPORT_SYMBOL_GPL(nvmet_req_complete);
700 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
709 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
719 static void nvmet_confirm_sq(struct percpu_ref *ref)
721 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
723 complete(&sq->confirm_done);
726 void nvmet_sq_destroy(struct nvmet_sq *sq)
729 * If this is the admin queue, complete all AERs so that our
730 * queue doesn't have outstanding requests on it.
732 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
733 nvmet_async_events_free(sq->ctrl);
734 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
735 wait_for_completion(&sq->confirm_done);
736 wait_for_completion(&sq->free_done);
737 percpu_ref_exit(&sq->ref);
740 nvmet_ctrl_put(sq->ctrl);
741 sq->ctrl = NULL; /* allows reusing the queue later */
744 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
746 static void nvmet_sq_free(struct percpu_ref *ref)
748 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
750 complete(&sq->free_done);
753 int nvmet_sq_init(struct nvmet_sq *sq)
757 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
759 pr_err("percpu_ref init failed!\n");
762 init_completion(&sq->free_done);
763 init_completion(&sq->confirm_done);
767 EXPORT_SYMBOL_GPL(nvmet_sq_init);
769 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
772 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
774 if (unlikely(state == NVME_ANA_INACCESSIBLE))
775 return NVME_SC_ANA_INACCESSIBLE;
776 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
777 return NVME_SC_ANA_PERSISTENT_LOSS;
778 if (unlikely(state == NVME_ANA_CHANGE))
779 return NVME_SC_ANA_TRANSITION;
783 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
785 if (unlikely(req->ns->readonly)) {
786 switch (req->cmd->common.opcode) {
791 return NVME_SC_NS_WRITE_PROTECTED;
798 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
800 struct nvme_command *cmd = req->cmd;
803 ret = nvmet_check_ctrl_status(req, cmd);
807 req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
808 if (unlikely(!req->ns)) {
809 req->error_loc = offsetof(struct nvme_common_command, nsid);
810 return NVME_SC_INVALID_NS | NVME_SC_DNR;
812 ret = nvmet_check_ana_state(req->port, req->ns);
814 req->error_loc = offsetof(struct nvme_common_command, nsid);
817 ret = nvmet_io_cmd_check_access(req);
819 req->error_loc = offsetof(struct nvme_common_command, nsid);
824 return nvmet_file_parse_io_cmd(req);
826 return nvmet_bdev_parse_io_cmd(req);
829 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
830 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
832 u8 flags = req->cmd->common.flags;
840 req->transfer_len = 0;
841 req->rsp->status = 0;
842 req->rsp->sq_head = 0;
844 req->error_loc = NVMET_NO_ERROR_LOC;
847 /* no support for fused commands yet */
848 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
849 req->error_loc = offsetof(struct nvme_common_command, flags);
850 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
855 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
856 * contains an address of a single contiguous physical buffer that is
859 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
860 req->error_loc = offsetof(struct nvme_common_command, flags);
861 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
865 if (unlikely(!req->sq->ctrl))
866 /* will return an error for any Non-connect command: */
867 status = nvmet_parse_connect_cmd(req);
868 else if (likely(req->sq->qid != 0))
869 status = nvmet_parse_io_cmd(req);
870 else if (req->cmd->common.opcode == nvme_fabrics_command)
871 status = nvmet_parse_fabrics_cmd(req);
872 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
873 status = nvmet_parse_discovery_cmd(req);
875 status = nvmet_parse_admin_cmd(req);
880 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
881 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
886 sq->ctrl->cmd_seen = true;
891 __nvmet_req_complete(req, status);
894 EXPORT_SYMBOL_GPL(nvmet_req_init);
896 void nvmet_req_uninit(struct nvmet_req *req)
898 percpu_ref_put(&req->sq->ref);
900 nvmet_put_namespace(req->ns);
902 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
904 void nvmet_req_execute(struct nvmet_req *req)
906 if (unlikely(req->data_len != req->transfer_len)) {
907 req->error_loc = offsetof(struct nvme_common_command, dptr);
908 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
912 EXPORT_SYMBOL_GPL(nvmet_req_execute);
914 int nvmet_req_alloc_sgl(struct nvmet_req *req)
916 struct pci_dev *p2p_dev = NULL;
918 if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
919 if (req->sq->ctrl && req->ns)
920 p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
924 if (req->sq->qid && p2p_dev) {
925 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
928 req->p2p_dev = p2p_dev;
934 * If no P2P memory was available we fallback to using
939 req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
945 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
947 void nvmet_req_free_sgl(struct nvmet_req *req)
950 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
957 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
959 static inline bool nvmet_cc_en(u32 cc)
961 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
964 static inline u8 nvmet_cc_css(u32 cc)
966 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
969 static inline u8 nvmet_cc_mps(u32 cc)
971 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
974 static inline u8 nvmet_cc_ams(u32 cc)
976 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
979 static inline u8 nvmet_cc_shn(u32 cc)
981 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
984 static inline u8 nvmet_cc_iosqes(u32 cc)
986 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
989 static inline u8 nvmet_cc_iocqes(u32 cc)
991 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
994 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
996 lockdep_assert_held(&ctrl->lock);
998 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
999 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
1000 nvmet_cc_mps(ctrl->cc) != 0 ||
1001 nvmet_cc_ams(ctrl->cc) != 0 ||
1002 nvmet_cc_css(ctrl->cc) != 0) {
1003 ctrl->csts = NVME_CSTS_CFS;
1007 ctrl->csts = NVME_CSTS_RDY;
1010 * Controllers that are not yet enabled should not really enforce the
1011 * keep alive timeout, but we still want to track a timeout and cleanup
1012 * in case a host died before it enabled the controller. Hence, simply
1013 * reset the keep alive timer when the controller is enabled.
1015 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
1018 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
1020 lockdep_assert_held(&ctrl->lock);
1022 /* XXX: tear down queues? */
1023 ctrl->csts &= ~NVME_CSTS_RDY;
1027 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
1031 mutex_lock(&ctrl->lock);
1035 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
1036 nvmet_start_ctrl(ctrl);
1037 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
1038 nvmet_clear_ctrl(ctrl);
1039 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
1040 nvmet_clear_ctrl(ctrl);
1041 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
1043 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
1044 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
1045 mutex_unlock(&ctrl->lock);
1048 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
1050 /* command sets supported: NVMe command set: */
1051 ctrl->cap = (1ULL << 37);
1052 /* CC.EN timeout in 500msec units: */
1053 ctrl->cap |= (15ULL << 24);
1054 /* maximum queue entries supported: */
1055 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
1058 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
1059 struct nvmet_req *req, struct nvmet_ctrl **ret)
1061 struct nvmet_subsys *subsys;
1062 struct nvmet_ctrl *ctrl;
1065 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1067 pr_warn("connect request for invalid subsystem %s!\n",
1069 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1070 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1073 mutex_lock(&subsys->lock);
1074 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
1075 if (ctrl->cntlid == cntlid) {
1076 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1077 pr_warn("hostnqn mismatch.\n");
1080 if (!kref_get_unless_zero(&ctrl->ref))
1088 pr_warn("could not find controller %d for subsys %s / host %s\n",
1089 cntlid, subsysnqn, hostnqn);
1090 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1091 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1094 mutex_unlock(&subsys->lock);
1095 nvmet_subsys_put(subsys);
1099 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1101 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1102 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1103 cmd->common.opcode, req->sq->qid);
1104 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1107 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1108 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1109 cmd->common.opcode, req->sq->qid);
1110 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1115 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
1117 struct nvmet_host_link *p;
1119 lockdep_assert_held(&nvmet_config_sem);
1121 if (subsys->allow_any_host)
1124 if (subsys->type == NVME_NQN_DISC) /* allow all access to disc subsys */
1127 list_for_each_entry(p, &subsys->hosts, entry) {
1128 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1136 * Note: ctrl->subsys->lock should be held when calling this function
1138 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1139 struct nvmet_req *req)
1141 struct nvmet_ns *ns;
1143 if (!req->p2p_client)
1146 ctrl->p2p_client = get_device(req->p2p_client);
1148 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1149 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1153 * Note: ctrl->subsys->lock should be held when calling this function
1155 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1157 struct radix_tree_iter iter;
1160 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1161 pci_dev_put(radix_tree_deref_slot(slot));
1163 put_device(ctrl->p2p_client);
1166 static void nvmet_fatal_error_handler(struct work_struct *work)
1168 struct nvmet_ctrl *ctrl =
1169 container_of(work, struct nvmet_ctrl, fatal_err_work);
1171 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1172 ctrl->ops->delete_ctrl(ctrl);
1175 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1176 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1178 struct nvmet_subsys *subsys;
1179 struct nvmet_ctrl *ctrl;
1183 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1184 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1186 pr_warn("connect request for invalid subsystem %s!\n",
1188 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1192 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1193 down_read(&nvmet_config_sem);
1194 if (!nvmet_host_allowed(subsys, hostnqn)) {
1195 pr_info("connect by host %s for subsystem %s not allowed\n",
1196 hostnqn, subsysnqn);
1197 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1198 up_read(&nvmet_config_sem);
1199 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1200 goto out_put_subsystem;
1202 up_read(&nvmet_config_sem);
1204 status = NVME_SC_INTERNAL;
1205 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1207 goto out_put_subsystem;
1208 mutex_init(&ctrl->lock);
1210 nvmet_init_cap(ctrl);
1212 ctrl->port = req->port;
1214 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1215 INIT_LIST_HEAD(&ctrl->async_events);
1216 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1217 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1219 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1220 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1222 kref_init(&ctrl->ref);
1223 ctrl->subsys = subsys;
1224 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1226 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1227 sizeof(__le32), GFP_KERNEL);
1228 if (!ctrl->changed_ns_list)
1231 ctrl->cqs = kcalloc(subsys->max_qid + 1,
1232 sizeof(struct nvmet_cq *),
1235 goto out_free_changed_ns_list;
1237 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1238 sizeof(struct nvmet_sq *),
1243 ret = ida_simple_get(&cntlid_ida,
1244 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1247 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1252 ctrl->ops = req->ops;
1255 * Discovery controllers may use some arbitrary high value
1256 * in order to cleanup stale discovery sessions
1258 if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1259 kato = NVMET_DISC_KATO_MS;
1261 /* keep-alive timeout in seconds */
1262 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1264 ctrl->err_counter = 0;
1265 spin_lock_init(&ctrl->error_lock);
1267 nvmet_start_keep_alive_timer(ctrl);
1269 mutex_lock(&subsys->lock);
1270 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1271 nvmet_setup_p2p_ns_map(ctrl, req);
1272 mutex_unlock(&subsys->lock);
1281 out_free_changed_ns_list:
1282 kfree(ctrl->changed_ns_list);
1286 nvmet_subsys_put(subsys);
1291 static void nvmet_ctrl_free(struct kref *ref)
1293 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1294 struct nvmet_subsys *subsys = ctrl->subsys;
1296 mutex_lock(&subsys->lock);
1297 nvmet_release_p2p_ns_map(ctrl);
1298 list_del(&ctrl->subsys_entry);
1299 mutex_unlock(&subsys->lock);
1301 nvmet_stop_keep_alive_timer(ctrl);
1303 flush_work(&ctrl->async_event_work);
1304 cancel_work_sync(&ctrl->fatal_err_work);
1306 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1310 kfree(ctrl->changed_ns_list);
1313 nvmet_subsys_put(subsys);
1316 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1318 kref_put(&ctrl->ref, nvmet_ctrl_free);
1321 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1323 mutex_lock(&ctrl->lock);
1324 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1325 ctrl->csts |= NVME_CSTS_CFS;
1326 schedule_work(&ctrl->fatal_err_work);
1328 mutex_unlock(&ctrl->lock);
1330 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1332 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1333 const char *subsysnqn)
1335 struct nvmet_subsys_link *p;
1340 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1341 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1343 return nvmet_disc_subsys;
1346 down_read(&nvmet_config_sem);
1347 list_for_each_entry(p, &port->subsystems, entry) {
1348 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1350 if (!kref_get_unless_zero(&p->subsys->ref))
1352 up_read(&nvmet_config_sem);
1356 up_read(&nvmet_config_sem);
1360 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1361 enum nvme_subsys_type type)
1363 struct nvmet_subsys *subsys;
1365 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1369 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1370 /* generate a random serial number as our controllers are ephemeral: */
1371 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1375 subsys->max_qid = NVMET_NR_QUEUES;
1378 subsys->max_qid = 0;
1381 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1385 subsys->type = type;
1386 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1388 if (!subsys->subsysnqn) {
1393 kref_init(&subsys->ref);
1395 mutex_init(&subsys->lock);
1396 INIT_LIST_HEAD(&subsys->namespaces);
1397 INIT_LIST_HEAD(&subsys->ctrls);
1398 INIT_LIST_HEAD(&subsys->hosts);
1403 static void nvmet_subsys_free(struct kref *ref)
1405 struct nvmet_subsys *subsys =
1406 container_of(ref, struct nvmet_subsys, ref);
1408 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1410 kfree(subsys->subsysnqn);
1414 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1416 struct nvmet_ctrl *ctrl;
1418 mutex_lock(&subsys->lock);
1419 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1420 ctrl->ops->delete_ctrl(ctrl);
1421 mutex_unlock(&subsys->lock);
1424 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1426 kref_put(&subsys->ref, nvmet_subsys_free);
1429 static int __init nvmet_init(void)
1433 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1435 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1437 if (!buffered_io_wq) {
1442 error = nvmet_init_discovery();
1444 goto out_free_work_queue;
1446 error = nvmet_init_configfs();
1448 goto out_exit_discovery;
1452 nvmet_exit_discovery();
1453 out_free_work_queue:
1454 destroy_workqueue(buffered_io_wq);
1459 static void __exit nvmet_exit(void)
1461 nvmet_exit_configfs();
1462 nvmet_exit_discovery();
1463 ida_destroy(&cntlid_ida);
1464 destroy_workqueue(buffered_io_wq);
1466 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1467 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1470 module_init(nvmet_init);
1471 module_exit(nvmet_exit);
1473 MODULE_LICENSE("GPL v2");