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>
11 #include <linux/scatterlist.h>
15 struct workqueue_struct *buffered_io_wq;
16 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
17 static DEFINE_IDA(cntlid_ida);
20 * This read/write semaphore is used to synchronize access to configuration
21 * information on a target system that will result in discovery log page
22 * information change for at least one host.
23 * The full list of resources to protected by this semaphore is:
26 * - per-subsystem allowed hosts list
27 * - allow_any_host subsystem attribute
29 * - the nvmet_transports array
31 * When updating any of those lists/structures write lock should be obtained,
32 * while when reading (popolating discovery log page or checking host-subsystem
33 * link) read lock is obtained to allow concurrent reads.
35 DECLARE_RWSEM(nvmet_config_sem);
37 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
39 DECLARE_RWSEM(nvmet_ana_sem);
41 inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno)
47 req->error_loc = offsetof(struct nvme_rw_command, length);
48 status = NVME_SC_CAP_EXCEEDED | NVME_SC_DNR;
51 req->error_loc = offsetof(struct nvme_rw_command, slba);
52 status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
55 req->error_loc = offsetof(struct nvme_common_command, opcode);
56 switch (req->cmd->common.opcode) {
58 case nvme_cmd_write_zeroes:
59 status = NVME_SC_ONCS_NOT_SUPPORTED | NVME_SC_DNR;
62 status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
66 req->error_loc = offsetof(struct nvme_rw_command, nsid);
67 status = NVME_SC_ACCESS_DENIED;
72 req->error_loc = offsetof(struct nvme_common_command, opcode);
73 status = NVME_SC_INTERNAL | NVME_SC_DNR;
79 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
80 const char *subsysnqn);
82 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
85 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
86 req->error_loc = offsetof(struct nvme_common_command, dptr);
87 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
92 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
94 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
95 req->error_loc = offsetof(struct nvme_common_command, dptr);
96 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
101 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
103 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) {
104 req->error_loc = offsetof(struct nvme_common_command, dptr);
105 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
110 static unsigned int nvmet_max_nsid(struct nvmet_subsys *subsys)
114 if (list_empty(&subsys->namespaces))
117 ns = list_last_entry(&subsys->namespaces, struct nvmet_ns, dev_link);
121 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
123 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
126 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
128 struct nvmet_req *req;
131 mutex_lock(&ctrl->lock);
132 if (!ctrl->nr_async_event_cmds) {
133 mutex_unlock(&ctrl->lock);
137 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
138 mutex_unlock(&ctrl->lock);
139 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
143 static void nvmet_async_event_work(struct work_struct *work)
145 struct nvmet_ctrl *ctrl =
146 container_of(work, struct nvmet_ctrl, async_event_work);
147 struct nvmet_async_event *aen;
148 struct nvmet_req *req;
151 mutex_lock(&ctrl->lock);
152 aen = list_first_entry_or_null(&ctrl->async_events,
153 struct nvmet_async_event, entry);
154 if (!aen || !ctrl->nr_async_event_cmds) {
155 mutex_unlock(&ctrl->lock);
159 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
160 nvmet_set_result(req, nvmet_async_event_result(aen));
162 list_del(&aen->entry);
165 mutex_unlock(&ctrl->lock);
166 nvmet_req_complete(req, 0);
170 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
171 u8 event_info, u8 log_page)
173 struct nvmet_async_event *aen;
175 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
179 aen->event_type = event_type;
180 aen->event_info = event_info;
181 aen->log_page = log_page;
183 mutex_lock(&ctrl->lock);
184 list_add_tail(&aen->entry, &ctrl->async_events);
185 mutex_unlock(&ctrl->lock);
187 schedule_work(&ctrl->async_event_work);
190 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
194 mutex_lock(&ctrl->lock);
195 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
198 for (i = 0; i < ctrl->nr_changed_ns; i++) {
199 if (ctrl->changed_ns_list[i] == nsid)
203 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
204 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
205 ctrl->nr_changed_ns = U32_MAX;
209 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
211 mutex_unlock(&ctrl->lock);
214 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
216 struct nvmet_ctrl *ctrl;
218 lockdep_assert_held(&subsys->lock);
220 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
221 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
222 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
224 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
225 NVME_AER_NOTICE_NS_CHANGED,
226 NVME_LOG_CHANGED_NS);
230 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
231 struct nvmet_port *port)
233 struct nvmet_ctrl *ctrl;
235 mutex_lock(&subsys->lock);
236 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
237 if (port && ctrl->port != port)
239 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
241 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
242 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
244 mutex_unlock(&subsys->lock);
247 void nvmet_port_send_ana_event(struct nvmet_port *port)
249 struct nvmet_subsys_link *p;
251 down_read(&nvmet_config_sem);
252 list_for_each_entry(p, &port->subsystems, entry)
253 nvmet_send_ana_event(p->subsys, port);
254 up_read(&nvmet_config_sem);
257 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
261 down_write(&nvmet_config_sem);
262 if (nvmet_transports[ops->type])
265 nvmet_transports[ops->type] = ops;
266 up_write(&nvmet_config_sem);
270 EXPORT_SYMBOL_GPL(nvmet_register_transport);
272 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
274 down_write(&nvmet_config_sem);
275 nvmet_transports[ops->type] = NULL;
276 up_write(&nvmet_config_sem);
278 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
280 int nvmet_enable_port(struct nvmet_port *port)
282 const struct nvmet_fabrics_ops *ops;
285 lockdep_assert_held(&nvmet_config_sem);
287 ops = nvmet_transports[port->disc_addr.trtype];
289 up_write(&nvmet_config_sem);
290 request_module("nvmet-transport-%d", port->disc_addr.trtype);
291 down_write(&nvmet_config_sem);
292 ops = nvmet_transports[port->disc_addr.trtype];
294 pr_err("transport type %d not supported\n",
295 port->disc_addr.trtype);
300 if (!try_module_get(ops->owner))
303 ret = ops->add_port(port);
305 module_put(ops->owner);
309 /* If the transport didn't set inline_data_size, then disable it. */
310 if (port->inline_data_size < 0)
311 port->inline_data_size = 0;
313 port->enabled = true;
317 void nvmet_disable_port(struct nvmet_port *port)
319 const struct nvmet_fabrics_ops *ops;
321 lockdep_assert_held(&nvmet_config_sem);
323 port->enabled = false;
325 ops = nvmet_transports[port->disc_addr.trtype];
326 ops->remove_port(port);
327 module_put(ops->owner);
330 static void nvmet_keep_alive_timer(struct work_struct *work)
332 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
333 struct nvmet_ctrl, ka_work);
334 bool cmd_seen = ctrl->cmd_seen;
336 ctrl->cmd_seen = false;
338 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
340 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
344 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
345 ctrl->cntlid, ctrl->kato);
347 nvmet_ctrl_fatal_error(ctrl);
350 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
352 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
353 ctrl->cntlid, ctrl->kato);
355 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
356 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
359 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
361 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
363 cancel_delayed_work_sync(&ctrl->ka_work);
366 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
371 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
372 if (ns->nsid == le32_to_cpu(nsid))
379 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
384 ns = __nvmet_find_namespace(ctrl, nsid);
386 percpu_ref_get(&ns->ref);
392 static void nvmet_destroy_namespace(struct percpu_ref *ref)
394 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
396 complete(&ns->disable_done);
399 void nvmet_put_namespace(struct nvmet_ns *ns)
401 percpu_ref_put(&ns->ref);
404 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
406 nvmet_bdev_ns_disable(ns);
407 nvmet_file_ns_disable(ns);
410 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
413 struct pci_dev *p2p_dev;
419 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
423 if (!blk_queue_pci_p2pdma(ns->bdev->bd_queue)) {
424 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
430 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
435 * Right now we just check that there is p2pmem available so
436 * we can report an error to the user right away if there
437 * is not. We'll find the actual device to use once we
438 * setup the controller when the port's device is available.
441 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
443 pr_err("no peer-to-peer memory is available for %s\n",
448 pci_dev_put(p2p_dev);
455 * Note: ctrl->subsys->lock should be held when calling this function
457 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
460 struct device *clients[2];
461 struct pci_dev *p2p_dev;
464 if (!ctrl->p2p_client || !ns->use_p2pmem)
468 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
472 p2p_dev = pci_dev_get(ns->p2p_dev);
474 clients[0] = ctrl->p2p_client;
475 clients[1] = nvmet_ns_dev(ns);
477 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
479 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
480 dev_name(ctrl->p2p_client), ns->device_path);
485 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
487 pci_dev_put(p2p_dev);
489 pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
493 int nvmet_ns_enable(struct nvmet_ns *ns)
495 struct nvmet_subsys *subsys = ns->subsys;
496 struct nvmet_ctrl *ctrl;
499 mutex_lock(&subsys->lock);
505 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
508 ret = nvmet_bdev_ns_enable(ns);
510 ret = nvmet_file_ns_enable(ns);
514 ret = nvmet_p2pmem_ns_enable(ns);
516 goto out_dev_disable;
518 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
519 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
521 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
526 if (ns->nsid > subsys->max_nsid)
527 subsys->max_nsid = ns->nsid;
530 * The namespaces list needs to be sorted to simplify the implementation
531 * of the Identify Namepace List subcommand.
533 if (list_empty(&subsys->namespaces)) {
534 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
536 struct nvmet_ns *old;
538 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
539 BUG_ON(ns->nsid == old->nsid);
540 if (ns->nsid < old->nsid)
544 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
546 subsys->nr_namespaces++;
548 nvmet_ns_changed(subsys, ns->nsid);
552 mutex_unlock(&subsys->lock);
555 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
556 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
558 nvmet_ns_dev_disable(ns);
562 void nvmet_ns_disable(struct nvmet_ns *ns)
564 struct nvmet_subsys *subsys = ns->subsys;
565 struct nvmet_ctrl *ctrl;
567 mutex_lock(&subsys->lock);
572 list_del_rcu(&ns->dev_link);
573 if (ns->nsid == subsys->max_nsid)
574 subsys->max_nsid = nvmet_max_nsid(subsys);
576 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
577 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
579 mutex_unlock(&subsys->lock);
582 * Now that we removed the namespaces from the lookup list, we
583 * can kill the per_cpu ref and wait for any remaining references
584 * to be dropped, as well as a RCU grace period for anyone only
585 * using the namepace under rcu_read_lock(). Note that we can't
586 * use call_rcu here as we need to ensure the namespaces have
587 * been fully destroyed before unloading the module.
589 percpu_ref_kill(&ns->ref);
591 wait_for_completion(&ns->disable_done);
592 percpu_ref_exit(&ns->ref);
594 mutex_lock(&subsys->lock);
596 subsys->nr_namespaces--;
597 nvmet_ns_changed(subsys, ns->nsid);
598 nvmet_ns_dev_disable(ns);
600 mutex_unlock(&subsys->lock);
603 void nvmet_ns_free(struct nvmet_ns *ns)
605 nvmet_ns_disable(ns);
607 down_write(&nvmet_ana_sem);
608 nvmet_ana_group_enabled[ns->anagrpid]--;
609 up_write(&nvmet_ana_sem);
611 kfree(ns->device_path);
615 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
619 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
623 INIT_LIST_HEAD(&ns->dev_link);
624 init_completion(&ns->disable_done);
629 down_write(&nvmet_ana_sem);
630 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
631 nvmet_ana_group_enabled[ns->anagrpid]++;
632 up_write(&nvmet_ana_sem);
635 ns->buffered_io = false;
640 static void nvmet_update_sq_head(struct nvmet_req *req)
643 u32 old_sqhd, new_sqhd;
646 old_sqhd = req->sq->sqhd;
647 new_sqhd = (old_sqhd + 1) % req->sq->size;
648 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
651 req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
654 static void nvmet_set_error(struct nvmet_req *req, u16 status)
656 struct nvmet_ctrl *ctrl = req->sq->ctrl;
657 struct nvme_error_slot *new_error_slot;
660 req->cqe->status = cpu_to_le16(status << 1);
662 if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC)
665 spin_lock_irqsave(&ctrl->error_lock, flags);
668 &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS];
670 new_error_slot->error_count = cpu_to_le64(ctrl->err_counter);
671 new_error_slot->sqid = cpu_to_le16(req->sq->qid);
672 new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id);
673 new_error_slot->status_field = cpu_to_le16(status << 1);
674 new_error_slot->param_error_location = cpu_to_le16(req->error_loc);
675 new_error_slot->lba = cpu_to_le64(req->error_slba);
676 new_error_slot->nsid = req->cmd->common.nsid;
677 spin_unlock_irqrestore(&ctrl->error_lock, flags);
679 /* set the more bit for this request */
680 req->cqe->status |= cpu_to_le16(1 << 14);
683 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
685 if (!req->sq->sqhd_disabled)
686 nvmet_update_sq_head(req);
687 req->cqe->sq_id = cpu_to_le16(req->sq->qid);
688 req->cqe->command_id = req->cmd->common.command_id;
690 if (unlikely(status))
691 nvmet_set_error(req, status);
693 nvmet_put_namespace(req->ns);
694 req->ops->queue_response(req);
697 void nvmet_req_complete(struct nvmet_req *req, u16 status)
699 __nvmet_req_complete(req, status);
700 percpu_ref_put(&req->sq->ref);
702 EXPORT_SYMBOL_GPL(nvmet_req_complete);
704 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
713 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
723 static void nvmet_confirm_sq(struct percpu_ref *ref)
725 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
727 complete(&sq->confirm_done);
730 void nvmet_sq_destroy(struct nvmet_sq *sq)
733 * If this is the admin queue, complete all AERs so that our
734 * queue doesn't have outstanding requests on it.
736 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
737 nvmet_async_events_free(sq->ctrl);
738 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
739 wait_for_completion(&sq->confirm_done);
740 wait_for_completion(&sq->free_done);
741 percpu_ref_exit(&sq->ref);
744 nvmet_ctrl_put(sq->ctrl);
745 sq->ctrl = NULL; /* allows reusing the queue later */
748 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
750 static void nvmet_sq_free(struct percpu_ref *ref)
752 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
754 complete(&sq->free_done);
757 int nvmet_sq_init(struct nvmet_sq *sq)
761 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
763 pr_err("percpu_ref init failed!\n");
766 init_completion(&sq->free_done);
767 init_completion(&sq->confirm_done);
771 EXPORT_SYMBOL_GPL(nvmet_sq_init);
773 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
776 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
778 if (unlikely(state == NVME_ANA_INACCESSIBLE))
779 return NVME_SC_ANA_INACCESSIBLE;
780 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
781 return NVME_SC_ANA_PERSISTENT_LOSS;
782 if (unlikely(state == NVME_ANA_CHANGE))
783 return NVME_SC_ANA_TRANSITION;
787 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
789 if (unlikely(req->ns->readonly)) {
790 switch (req->cmd->common.opcode) {
795 return NVME_SC_NS_WRITE_PROTECTED;
802 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
804 struct nvme_command *cmd = req->cmd;
807 ret = nvmet_check_ctrl_status(req, cmd);
811 req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
812 if (unlikely(!req->ns)) {
813 req->error_loc = offsetof(struct nvme_common_command, nsid);
814 return NVME_SC_INVALID_NS | NVME_SC_DNR;
816 ret = nvmet_check_ana_state(req->port, req->ns);
818 req->error_loc = offsetof(struct nvme_common_command, nsid);
821 ret = nvmet_io_cmd_check_access(req);
823 req->error_loc = offsetof(struct nvme_common_command, nsid);
828 return nvmet_file_parse_io_cmd(req);
830 return nvmet_bdev_parse_io_cmd(req);
833 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
834 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
836 u8 flags = req->cmd->common.flags;
844 req->transfer_len = 0;
845 req->cqe->status = 0;
846 req->cqe->sq_head = 0;
848 req->error_loc = NVMET_NO_ERROR_LOC;
851 /* no support for fused commands yet */
852 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
853 req->error_loc = offsetof(struct nvme_common_command, flags);
854 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
859 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
860 * contains an address of a single contiguous physical buffer that is
863 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
864 req->error_loc = offsetof(struct nvme_common_command, flags);
865 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
869 if (unlikely(!req->sq->ctrl))
870 /* will return an error for any Non-connect command: */
871 status = nvmet_parse_connect_cmd(req);
872 else if (likely(req->sq->qid != 0))
873 status = nvmet_parse_io_cmd(req);
874 else if (req->cmd->common.opcode == nvme_fabrics_command)
875 status = nvmet_parse_fabrics_cmd(req);
876 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
877 status = nvmet_parse_discovery_cmd(req);
879 status = nvmet_parse_admin_cmd(req);
884 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
885 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
890 sq->ctrl->cmd_seen = true;
895 __nvmet_req_complete(req, status);
898 EXPORT_SYMBOL_GPL(nvmet_req_init);
900 void nvmet_req_uninit(struct nvmet_req *req)
902 percpu_ref_put(&req->sq->ref);
904 nvmet_put_namespace(req->ns);
906 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
908 void nvmet_req_execute(struct nvmet_req *req)
910 if (unlikely(req->data_len != req->transfer_len)) {
911 req->error_loc = offsetof(struct nvme_common_command, dptr);
912 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
916 EXPORT_SYMBOL_GPL(nvmet_req_execute);
918 int nvmet_req_alloc_sgl(struct nvmet_req *req)
920 struct pci_dev *p2p_dev = NULL;
922 if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
923 if (req->sq->ctrl && req->ns)
924 p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
928 if (req->sq->qid && p2p_dev) {
929 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
932 req->p2p_dev = p2p_dev;
938 * If no P2P memory was available we fallback to using
943 req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
949 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
951 void nvmet_req_free_sgl(struct nvmet_req *req)
954 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
961 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
963 static inline bool nvmet_cc_en(u32 cc)
965 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
968 static inline u8 nvmet_cc_css(u32 cc)
970 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
973 static inline u8 nvmet_cc_mps(u32 cc)
975 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
978 static inline u8 nvmet_cc_ams(u32 cc)
980 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
983 static inline u8 nvmet_cc_shn(u32 cc)
985 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
988 static inline u8 nvmet_cc_iosqes(u32 cc)
990 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
993 static inline u8 nvmet_cc_iocqes(u32 cc)
995 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
998 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
1000 lockdep_assert_held(&ctrl->lock);
1002 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
1003 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
1004 nvmet_cc_mps(ctrl->cc) != 0 ||
1005 nvmet_cc_ams(ctrl->cc) != 0 ||
1006 nvmet_cc_css(ctrl->cc) != 0) {
1007 ctrl->csts = NVME_CSTS_CFS;
1011 ctrl->csts = NVME_CSTS_RDY;
1014 * Controllers that are not yet enabled should not really enforce the
1015 * keep alive timeout, but we still want to track a timeout and cleanup
1016 * in case a host died before it enabled the controller. Hence, simply
1017 * reset the keep alive timer when the controller is enabled.
1019 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
1022 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
1024 lockdep_assert_held(&ctrl->lock);
1026 /* XXX: tear down queues? */
1027 ctrl->csts &= ~NVME_CSTS_RDY;
1031 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
1035 mutex_lock(&ctrl->lock);
1039 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
1040 nvmet_start_ctrl(ctrl);
1041 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
1042 nvmet_clear_ctrl(ctrl);
1043 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
1044 nvmet_clear_ctrl(ctrl);
1045 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
1047 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
1048 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
1049 mutex_unlock(&ctrl->lock);
1052 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
1054 /* command sets supported: NVMe command set: */
1055 ctrl->cap = (1ULL << 37);
1056 /* CC.EN timeout in 500msec units: */
1057 ctrl->cap |= (15ULL << 24);
1058 /* maximum queue entries supported: */
1059 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
1062 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
1063 struct nvmet_req *req, struct nvmet_ctrl **ret)
1065 struct nvmet_subsys *subsys;
1066 struct nvmet_ctrl *ctrl;
1069 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1071 pr_warn("connect request for invalid subsystem %s!\n",
1073 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1074 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1077 mutex_lock(&subsys->lock);
1078 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
1079 if (ctrl->cntlid == cntlid) {
1080 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1081 pr_warn("hostnqn mismatch.\n");
1084 if (!kref_get_unless_zero(&ctrl->ref))
1092 pr_warn("could not find controller %d for subsys %s / host %s\n",
1093 cntlid, subsysnqn, hostnqn);
1094 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1095 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1098 mutex_unlock(&subsys->lock);
1099 nvmet_subsys_put(subsys);
1103 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1105 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1106 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1107 cmd->common.opcode, req->sq->qid);
1108 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1111 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1112 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1113 cmd->common.opcode, req->sq->qid);
1114 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1119 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
1121 struct nvmet_host_link *p;
1123 lockdep_assert_held(&nvmet_config_sem);
1125 if (subsys->allow_any_host)
1128 if (subsys->type == NVME_NQN_DISC) /* allow all access to disc subsys */
1131 list_for_each_entry(p, &subsys->hosts, entry) {
1132 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1140 * Note: ctrl->subsys->lock should be held when calling this function
1142 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1143 struct nvmet_req *req)
1145 struct nvmet_ns *ns;
1147 if (!req->p2p_client)
1150 ctrl->p2p_client = get_device(req->p2p_client);
1152 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1153 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1157 * Note: ctrl->subsys->lock should be held when calling this function
1159 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1161 struct radix_tree_iter iter;
1164 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1165 pci_dev_put(radix_tree_deref_slot(slot));
1167 put_device(ctrl->p2p_client);
1170 static void nvmet_fatal_error_handler(struct work_struct *work)
1172 struct nvmet_ctrl *ctrl =
1173 container_of(work, struct nvmet_ctrl, fatal_err_work);
1175 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1176 ctrl->ops->delete_ctrl(ctrl);
1179 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1180 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1182 struct nvmet_subsys *subsys;
1183 struct nvmet_ctrl *ctrl;
1187 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1188 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1190 pr_warn("connect request for invalid subsystem %s!\n",
1192 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1196 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1197 down_read(&nvmet_config_sem);
1198 if (!nvmet_host_allowed(subsys, hostnqn)) {
1199 pr_info("connect by host %s for subsystem %s not allowed\n",
1200 hostnqn, subsysnqn);
1201 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1202 up_read(&nvmet_config_sem);
1203 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1204 goto out_put_subsystem;
1206 up_read(&nvmet_config_sem);
1208 status = NVME_SC_INTERNAL;
1209 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1211 goto out_put_subsystem;
1212 mutex_init(&ctrl->lock);
1214 nvmet_init_cap(ctrl);
1216 ctrl->port = req->port;
1218 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1219 INIT_LIST_HEAD(&ctrl->async_events);
1220 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1221 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1223 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1224 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1226 kref_init(&ctrl->ref);
1227 ctrl->subsys = subsys;
1228 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1230 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1231 sizeof(__le32), GFP_KERNEL);
1232 if (!ctrl->changed_ns_list)
1235 ctrl->cqs = kcalloc(subsys->max_qid + 1,
1236 sizeof(struct nvmet_cq *),
1239 goto out_free_changed_ns_list;
1241 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1242 sizeof(struct nvmet_sq *),
1247 ret = ida_simple_get(&cntlid_ida,
1248 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1251 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1256 ctrl->ops = req->ops;
1259 * Discovery controllers may use some arbitrary high value
1260 * in order to cleanup stale discovery sessions
1262 if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1263 kato = NVMET_DISC_KATO_MS;
1265 /* keep-alive timeout in seconds */
1266 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1268 ctrl->err_counter = 0;
1269 spin_lock_init(&ctrl->error_lock);
1271 nvmet_start_keep_alive_timer(ctrl);
1273 mutex_lock(&subsys->lock);
1274 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1275 nvmet_setup_p2p_ns_map(ctrl, req);
1276 mutex_unlock(&subsys->lock);
1285 out_free_changed_ns_list:
1286 kfree(ctrl->changed_ns_list);
1290 nvmet_subsys_put(subsys);
1295 static void nvmet_ctrl_free(struct kref *ref)
1297 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1298 struct nvmet_subsys *subsys = ctrl->subsys;
1300 mutex_lock(&subsys->lock);
1301 nvmet_release_p2p_ns_map(ctrl);
1302 list_del(&ctrl->subsys_entry);
1303 mutex_unlock(&subsys->lock);
1305 nvmet_stop_keep_alive_timer(ctrl);
1307 flush_work(&ctrl->async_event_work);
1308 cancel_work_sync(&ctrl->fatal_err_work);
1310 ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1314 kfree(ctrl->changed_ns_list);
1317 nvmet_subsys_put(subsys);
1320 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1322 kref_put(&ctrl->ref, nvmet_ctrl_free);
1325 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1327 mutex_lock(&ctrl->lock);
1328 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1329 ctrl->csts |= NVME_CSTS_CFS;
1330 schedule_work(&ctrl->fatal_err_work);
1332 mutex_unlock(&ctrl->lock);
1334 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1336 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1337 const char *subsysnqn)
1339 struct nvmet_subsys_link *p;
1344 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1345 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1347 return nvmet_disc_subsys;
1350 down_read(&nvmet_config_sem);
1351 list_for_each_entry(p, &port->subsystems, entry) {
1352 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1354 if (!kref_get_unless_zero(&p->subsys->ref))
1356 up_read(&nvmet_config_sem);
1360 up_read(&nvmet_config_sem);
1364 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1365 enum nvme_subsys_type type)
1367 struct nvmet_subsys *subsys;
1369 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1371 return ERR_PTR(-ENOMEM);
1373 subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1374 /* generate a random serial number as our controllers are ephemeral: */
1375 get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1379 subsys->max_qid = NVMET_NR_QUEUES;
1382 subsys->max_qid = 0;
1385 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1387 return ERR_PTR(-EINVAL);
1389 subsys->type = type;
1390 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1392 if (!subsys->subsysnqn) {
1394 return ERR_PTR(-ENOMEM);
1397 kref_init(&subsys->ref);
1399 mutex_init(&subsys->lock);
1400 INIT_LIST_HEAD(&subsys->namespaces);
1401 INIT_LIST_HEAD(&subsys->ctrls);
1402 INIT_LIST_HEAD(&subsys->hosts);
1407 static void nvmet_subsys_free(struct kref *ref)
1409 struct nvmet_subsys *subsys =
1410 container_of(ref, struct nvmet_subsys, ref);
1412 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1414 kfree(subsys->subsysnqn);
1418 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1420 struct nvmet_ctrl *ctrl;
1422 mutex_lock(&subsys->lock);
1423 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1424 ctrl->ops->delete_ctrl(ctrl);
1425 mutex_unlock(&subsys->lock);
1428 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1430 kref_put(&subsys->ref, nvmet_subsys_free);
1433 static int __init nvmet_init(void)
1437 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1439 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1441 if (!buffered_io_wq) {
1446 error = nvmet_init_discovery();
1448 goto out_free_work_queue;
1450 error = nvmet_init_configfs();
1452 goto out_exit_discovery;
1456 nvmet_exit_discovery();
1457 out_free_work_queue:
1458 destroy_workqueue(buffered_io_wq);
1463 static void __exit nvmet_exit(void)
1465 nvmet_exit_configfs();
1466 nvmet_exit_discovery();
1467 ida_destroy(&cntlid_ida);
1468 destroy_workqueue(buffered_io_wq);
1470 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1471 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1474 module_init(nvmet_init);
1475 module_exit(nvmet_exit);
1477 MODULE_LICENSE("GPL v2");