Lines Matching +full:supports +full:- +full:cqe

1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
10 #include <linux/pci-p2pdma.h>
36 * - subsystems list
37 * - per-subsystem allowed hosts list
38 * - allow_any_host subsystem attribute
39 * - nvmet_genctr
40 * - the nvmet_transports array
43 * while when reading (popolating discovery log page or checking host-subsystem
57 case -ENOSPC: in errno_to_nvme_status()
58 req->error_loc = offsetof(struct nvme_rw_command, length); in errno_to_nvme_status()
60 case -EREMOTEIO: in errno_to_nvme_status()
61 req->error_loc = offsetof(struct nvme_rw_command, slba); in errno_to_nvme_status()
63 case -EOPNOTSUPP: in errno_to_nvme_status()
64 req->error_loc = offsetof(struct nvme_common_command, opcode); in errno_to_nvme_status()
65 switch (req->cmd->common.opcode) { in errno_to_nvme_status()
73 case -ENODATA: in errno_to_nvme_status()
74 req->error_loc = offsetof(struct nvme_rw_command, nsid); in errno_to_nvme_status()
76 case -EIO: in errno_to_nvme_status()
79 req->error_loc = offsetof(struct nvme_common_command, opcode); in errno_to_nvme_status()
86 pr_debug("unhandled cmd %d on qid %d\n", req->cmd->common.opcode, in nvmet_report_invalid_opcode()
87 req->sq->qid); in nvmet_report_invalid_opcode()
89 req->error_loc = offsetof(struct nvme_common_command, opcode); in nvmet_report_invalid_opcode()
99 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { in nvmet_copy_to_sgl()
100 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_copy_to_sgl()
108 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { in nvmet_copy_from_sgl()
109 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_copy_from_sgl()
117 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) { in nvmet_zero_sgl()
118 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_zero_sgl()
130 xa_for_each(&subsys->namespaces, idx, cur) in nvmet_max_nsid()
131 nsid = cur->nsid; in nvmet_max_nsid()
138 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16); in nvmet_async_event_result()
145 mutex_lock(&ctrl->lock); in nvmet_async_events_failall()
146 while (ctrl->nr_async_event_cmds) { in nvmet_async_events_failall()
147 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; in nvmet_async_events_failall()
148 mutex_unlock(&ctrl->lock); in nvmet_async_events_failall()
150 mutex_lock(&ctrl->lock); in nvmet_async_events_failall()
152 mutex_unlock(&ctrl->lock); in nvmet_async_events_failall()
160 mutex_lock(&ctrl->lock); in nvmet_async_events_process()
161 while (ctrl->nr_async_event_cmds && !list_empty(&ctrl->async_events)) { in nvmet_async_events_process()
162 aen = list_first_entry(&ctrl->async_events, in nvmet_async_events_process()
164 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; in nvmet_async_events_process()
167 list_del(&aen->entry); in nvmet_async_events_process()
170 mutex_unlock(&ctrl->lock); in nvmet_async_events_process()
171 trace_nvmet_async_event(ctrl, req->cqe->result.u32); in nvmet_async_events_process()
173 mutex_lock(&ctrl->lock); in nvmet_async_events_process()
175 mutex_unlock(&ctrl->lock); in nvmet_async_events_process()
182 mutex_lock(&ctrl->lock); in nvmet_async_events_free()
183 list_for_each_entry_safe(aen, tmp, &ctrl->async_events, entry) { in nvmet_async_events_free()
184 list_del(&aen->entry); in nvmet_async_events_free()
187 mutex_unlock(&ctrl->lock); in nvmet_async_events_free()
207 aen->event_type = event_type; in nvmet_add_async_event()
208 aen->event_info = event_info; in nvmet_add_async_event()
209 aen->log_page = log_page; in nvmet_add_async_event()
211 mutex_lock(&ctrl->lock); in nvmet_add_async_event()
212 list_add_tail(&aen->entry, &ctrl->async_events); in nvmet_add_async_event()
213 mutex_unlock(&ctrl->lock); in nvmet_add_async_event()
215 queue_work(nvmet_wq, &ctrl->async_event_work); in nvmet_add_async_event()
222 mutex_lock(&ctrl->lock); in nvmet_add_to_changed_ns_log()
223 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES) in nvmet_add_to_changed_ns_log()
226 for (i = 0; i < ctrl->nr_changed_ns; i++) { in nvmet_add_to_changed_ns_log()
227 if (ctrl->changed_ns_list[i] == nsid) in nvmet_add_to_changed_ns_log()
231 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) { in nvmet_add_to_changed_ns_log()
232 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff); in nvmet_add_to_changed_ns_log()
233 ctrl->nr_changed_ns = U32_MAX; in nvmet_add_to_changed_ns_log()
237 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid; in nvmet_add_to_changed_ns_log()
239 mutex_unlock(&ctrl->lock); in nvmet_add_to_changed_ns_log()
246 lockdep_assert_held(&subsys->lock); in nvmet_ns_changed()
248 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { in nvmet_ns_changed()
263 mutex_lock(&subsys->lock); in nvmet_send_ana_event()
264 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { in nvmet_send_ana_event()
265 if (port && ctrl->port != port) in nvmet_send_ana_event()
272 mutex_unlock(&subsys->lock); in nvmet_send_ana_event()
280 list_for_each_entry(p, &port->subsystems, entry) in nvmet_port_send_ana_event()
281 nvmet_send_ana_event(p->subsys, port); in nvmet_port_send_ana_event()
290 if (nvmet_transports[ops->type]) in nvmet_register_transport()
291 ret = -EINVAL; in nvmet_register_transport()
293 nvmet_transports[ops->type] = ops; in nvmet_register_transport()
303 nvmet_transports[ops->type] = NULL; in nvmet_unregister_transport()
312 mutex_lock(&subsys->lock); in nvmet_port_del_ctrls()
313 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { in nvmet_port_del_ctrls()
314 if (ctrl->port == port) in nvmet_port_del_ctrls()
315 ctrl->ops->delete_ctrl(ctrl); in nvmet_port_del_ctrls()
317 mutex_unlock(&subsys->lock); in nvmet_port_del_ctrls()
327 ops = nvmet_transports[port->disc_addr.trtype]; in nvmet_enable_port()
330 request_module("nvmet-transport-%d", port->disc_addr.trtype); in nvmet_enable_port()
332 ops = nvmet_transports[port->disc_addr.trtype]; in nvmet_enable_port()
335 port->disc_addr.trtype); in nvmet_enable_port()
336 return -EINVAL; in nvmet_enable_port()
340 if (!try_module_get(ops->owner)) in nvmet_enable_port()
341 return -EINVAL; in nvmet_enable_port()
347 if (port->pi_enable && !(ops->flags & NVMF_METADATA_SUPPORTED)) { in nvmet_enable_port()
348 pr_err("T10-PI is not supported by transport type %d\n", in nvmet_enable_port()
349 port->disc_addr.trtype); in nvmet_enable_port()
350 ret = -EINVAL; in nvmet_enable_port()
354 ret = ops->add_port(port); in nvmet_enable_port()
359 if (port->inline_data_size < 0) in nvmet_enable_port()
360 port->inline_data_size = 0; in nvmet_enable_port()
367 if (port->max_queue_size < 0) in nvmet_enable_port()
368 port->max_queue_size = NVMET_MAX_QUEUE_SIZE; in nvmet_enable_port()
370 port->max_queue_size = clamp_t(int, port->max_queue_size, in nvmet_enable_port()
374 port->enabled = true; in nvmet_enable_port()
375 port->tr_ops = ops; in nvmet_enable_port()
379 module_put(ops->owner); in nvmet_enable_port()
389 port->enabled = false; in nvmet_disable_port()
390 port->tr_ops = NULL; in nvmet_disable_port()
392 ops = nvmet_transports[port->disc_addr.trtype]; in nvmet_disable_port()
393 ops->remove_port(port); in nvmet_disable_port()
394 module_put(ops->owner); in nvmet_disable_port()
401 bool reset_tbkas = ctrl->reset_tbkas; in nvmet_keep_alive_timer()
403 ctrl->reset_tbkas = false; in nvmet_keep_alive_timer()
405 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n", in nvmet_keep_alive_timer()
406 ctrl->cntlid); in nvmet_keep_alive_timer()
407 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); in nvmet_keep_alive_timer()
411 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n", in nvmet_keep_alive_timer()
412 ctrl->cntlid, ctrl->kato); in nvmet_keep_alive_timer()
419 if (unlikely(ctrl->kato == 0)) in nvmet_start_keep_alive_timer()
422 pr_debug("ctrl %d start keep-alive timer for %d secs\n", in nvmet_start_keep_alive_timer()
423 ctrl->cntlid, ctrl->kato); in nvmet_start_keep_alive_timer()
425 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); in nvmet_start_keep_alive_timer()
430 if (unlikely(ctrl->kato == 0)) in nvmet_stop_keep_alive_timer()
433 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid); in nvmet_stop_keep_alive_timer()
435 cancel_delayed_work_sync(&ctrl->ka_work); in nvmet_stop_keep_alive_timer()
440 u32 nsid = le32_to_cpu(req->cmd->common.nsid); in nvmet_req_find_ns()
443 req->ns = xa_load(&subsys->namespaces, nsid); in nvmet_req_find_ns()
444 if (unlikely(!req->ns)) { in nvmet_req_find_ns()
445 req->error_loc = offsetof(struct nvme_common_command, nsid); in nvmet_req_find_ns()
451 percpu_ref_get(&req->ns->ref); in nvmet_req_find_ns()
459 complete(&ns->disable_done); in nvmet_destroy_namespace()
464 percpu_ref_put(&ns->ref); in nvmet_put_namespace()
478 if (!ns->use_p2pmem) in nvmet_p2pmem_ns_enable()
481 if (!ns->bdev) { in nvmet_p2pmem_ns_enable()
482 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n"); in nvmet_p2pmem_ns_enable()
483 return -EINVAL; in nvmet_p2pmem_ns_enable()
486 if (!blk_queue_pci_p2pdma(ns->bdev->bd_disk->queue)) { in nvmet_p2pmem_ns_enable()
487 pr_err("peer-to-peer DMA is not supported by the driver of %s\n", in nvmet_p2pmem_ns_enable()
488 ns->device_path); in nvmet_p2pmem_ns_enable()
489 return -EINVAL; in nvmet_p2pmem_ns_enable()
492 if (ns->p2p_dev) { in nvmet_p2pmem_ns_enable()
493 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true); in nvmet_p2pmem_ns_enable()
495 return -EINVAL; in nvmet_p2pmem_ns_enable()
506 pr_err("no peer-to-peer memory is available for %s\n", in nvmet_p2pmem_ns_enable()
507 ns->device_path); in nvmet_p2pmem_ns_enable()
508 return -EINVAL; in nvmet_p2pmem_ns_enable()
518 * Note: ctrl->subsys->lock should be held when calling this function
527 if (!ctrl->p2p_client || !ns->use_p2pmem) in nvmet_p2pmem_ns_add_p2p()
530 if (ns->p2p_dev) { in nvmet_p2pmem_ns_add_p2p()
531 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true); in nvmet_p2pmem_ns_add_p2p()
535 p2p_dev = pci_dev_get(ns->p2p_dev); in nvmet_p2pmem_ns_add_p2p()
537 clients[0] = ctrl->p2p_client; in nvmet_p2pmem_ns_add_p2p()
542 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n", in nvmet_p2pmem_ns_add_p2p()
543 dev_name(ctrl->p2p_client), ns->device_path); in nvmet_p2pmem_ns_add_p2p()
548 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev); in nvmet_p2pmem_ns_add_p2p()
553 ns->nsid); in nvmet_p2pmem_ns_add_p2p()
558 loff_t oldsize = ns->size; in nvmet_ns_revalidate()
560 if (ns->bdev) in nvmet_ns_revalidate()
565 return oldsize != ns->size; in nvmet_ns_revalidate()
570 struct nvmet_subsys *subsys = ns->subsys; in nvmet_ns_enable()
574 mutex_lock(&subsys->lock); in nvmet_ns_enable()
582 if (ns->enabled) in nvmet_ns_enable()
585 ret = -EMFILE; in nvmet_ns_enable()
586 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES) in nvmet_ns_enable()
590 if (ret == -ENOTBLK) in nvmet_ns_enable()
599 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) in nvmet_ns_enable()
602 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace, in nvmet_ns_enable()
607 if (ns->nsid > subsys->max_nsid) in nvmet_ns_enable()
608 subsys->max_nsid = ns->nsid; in nvmet_ns_enable()
610 ret = xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL); in nvmet_ns_enable()
614 subsys->nr_namespaces++; in nvmet_ns_enable()
616 nvmet_ns_changed(subsys, ns->nsid); in nvmet_ns_enable()
617 ns->enabled = true; in nvmet_ns_enable()
620 mutex_unlock(&subsys->lock); in nvmet_ns_enable()
624 subsys->max_nsid = nvmet_max_nsid(subsys); in nvmet_ns_enable()
625 percpu_ref_exit(&ns->ref); in nvmet_ns_enable()
627 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) in nvmet_ns_enable()
628 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); in nvmet_ns_enable()
636 struct nvmet_subsys *subsys = ns->subsys; in nvmet_ns_disable()
639 mutex_lock(&subsys->lock); in nvmet_ns_disable()
640 if (!ns->enabled) in nvmet_ns_disable()
643 ns->enabled = false; in nvmet_ns_disable()
644 xa_erase(&ns->subsys->namespaces, ns->nsid); in nvmet_ns_disable()
645 if (ns->nsid == subsys->max_nsid) in nvmet_ns_disable()
646 subsys->max_nsid = nvmet_max_nsid(subsys); in nvmet_ns_disable()
648 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) in nvmet_ns_disable()
649 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); in nvmet_ns_disable()
651 mutex_unlock(&subsys->lock); in nvmet_ns_disable()
661 percpu_ref_kill(&ns->ref); in nvmet_ns_disable()
663 wait_for_completion(&ns->disable_done); in nvmet_ns_disable()
664 percpu_ref_exit(&ns->ref); in nvmet_ns_disable()
666 mutex_lock(&subsys->lock); in nvmet_ns_disable()
668 subsys->nr_namespaces--; in nvmet_ns_disable()
669 nvmet_ns_changed(subsys, ns->nsid); in nvmet_ns_disable()
672 mutex_unlock(&subsys->lock); in nvmet_ns_disable()
680 nvmet_ana_group_enabled[ns->anagrpid]--; in nvmet_ns_free()
683 kfree(ns->device_path); in nvmet_ns_free()
695 init_completion(&ns->disable_done); in nvmet_ns_alloc()
697 ns->nsid = nsid; in nvmet_ns_alloc()
698 ns->subsys = subsys; in nvmet_ns_alloc()
701 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID; in nvmet_ns_alloc()
702 nvmet_ana_group_enabled[ns->anagrpid]++; in nvmet_ns_alloc()
705 uuid_gen(&ns->uuid); in nvmet_ns_alloc()
706 ns->buffered_io = false; in nvmet_ns_alloc()
707 ns->csi = NVME_CSI_NVM; in nvmet_ns_alloc()
714 if (req->sq->size) { in nvmet_update_sq_head()
717 old_sqhd = READ_ONCE(req->sq->sqhd); in nvmet_update_sq_head()
719 new_sqhd = (old_sqhd + 1) % req->sq->size; in nvmet_update_sq_head()
720 } while (!try_cmpxchg(&req->sq->sqhd, &old_sqhd, new_sqhd)); in nvmet_update_sq_head()
722 req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF); in nvmet_update_sq_head()
727 struct nvmet_ctrl *ctrl = req->sq->ctrl; in nvmet_set_error()
731 req->cqe->status = cpu_to_le16(status << 1); in nvmet_set_error()
733 if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC) in nvmet_set_error()
736 spin_lock_irqsave(&ctrl->error_lock, flags); in nvmet_set_error()
737 ctrl->err_counter++; in nvmet_set_error()
739 &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS]; in nvmet_set_error()
741 new_error_slot->error_count = cpu_to_le64(ctrl->err_counter); in nvmet_set_error()
742 new_error_slot->sqid = cpu_to_le16(req->sq->qid); in nvmet_set_error()
743 new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id); in nvmet_set_error()
744 new_error_slot->status_field = cpu_to_le16(status << 1); in nvmet_set_error()
745 new_error_slot->param_error_location = cpu_to_le16(req->error_loc); in nvmet_set_error()
746 new_error_slot->lba = cpu_to_le64(req->error_slba); in nvmet_set_error()
747 new_error_slot->nsid = req->cmd->common.nsid; in nvmet_set_error()
748 spin_unlock_irqrestore(&ctrl->error_lock, flags); in nvmet_set_error()
751 req->cqe->status |= cpu_to_le16(1 << 14); in nvmet_set_error()
756 struct nvmet_ns *ns = req->ns; in __nvmet_req_complete()
758 if (!req->sq->sqhd_disabled) in __nvmet_req_complete()
760 req->cqe->sq_id = cpu_to_le16(req->sq->qid); in __nvmet_req_complete()
761 req->cqe->command_id = req->cmd->common.command_id; in __nvmet_req_complete()
768 req->ops->queue_response(req); in __nvmet_req_complete()
775 struct nvmet_sq *sq = req->sq; in nvmet_req_complete()
778 percpu_ref_put(&sq->ref); in nvmet_req_complete()
785 cq->qid = qid; in nvmet_cq_setup()
786 cq->size = size; in nvmet_cq_setup()
792 sq->sqhd = 0; in nvmet_sq_setup()
793 sq->qid = qid; in nvmet_sq_setup()
794 sq->size = size; in nvmet_sq_setup()
796 ctrl->sqs[qid] = sq; in nvmet_sq_setup()
803 complete(&sq->confirm_done); in nvmet_confirm_sq()
808 struct nvmet_ctrl *ctrl = sq->ctrl; in nvmet_sq_destroy()
814 if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq) in nvmet_sq_destroy()
816 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq); in nvmet_sq_destroy()
817 wait_for_completion(&sq->confirm_done); in nvmet_sq_destroy()
818 wait_for_completion(&sq->free_done); in nvmet_sq_destroy()
819 percpu_ref_exit(&sq->ref); in nvmet_sq_destroy()
825 * store sq->ctrl locally, but before we killed the percpu_ref. the in nvmet_sq_destroy()
826 * admin connect allocates and assigns sq->ctrl, which now needs a in nvmet_sq_destroy()
829 ctrl = sq->ctrl; in nvmet_sq_destroy()
834 * send us keep-alive during this period, hence reset the in nvmet_sq_destroy()
835 * traffic based keep-alive timer so we don't trigger a in nvmet_sq_destroy()
836 * controller teardown as a result of a keep-alive expiration. in nvmet_sq_destroy()
838 ctrl->reset_tbkas = true; in nvmet_sq_destroy()
839 sq->ctrl->sqs[sq->qid] = NULL; in nvmet_sq_destroy()
841 sq->ctrl = NULL; /* allows reusing the queue later */ in nvmet_sq_destroy()
850 complete(&sq->free_done); in nvmet_sq_free()
857 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL); in nvmet_sq_init()
862 init_completion(&sq->free_done); in nvmet_sq_init()
863 init_completion(&sq->confirm_done); in nvmet_sq_init()
873 enum nvme_ana_state state = port->ana_state[ns->anagrpid]; in nvmet_check_ana_state()
886 if (unlikely(req->ns->readonly)) { in nvmet_io_cmd_check_access()
887 switch (req->cmd->common.opcode) { in nvmet_io_cmd_check_access()
901 struct nvme_command *cmd = req->cmd; in nvmet_parse_io_cmd()
921 ret = nvmet_check_ana_state(req->port, req->ns); in nvmet_parse_io_cmd()
923 req->error_loc = offsetof(struct nvme_common_command, nsid); in nvmet_parse_io_cmd()
928 req->error_loc = offsetof(struct nvme_common_command, nsid); in nvmet_parse_io_cmd()
932 switch (req->ns->csi) { in nvmet_parse_io_cmd()
934 if (req->ns->file) in nvmet_parse_io_cmd()
949 u8 flags = req->cmd->common.flags; in nvmet_req_init()
952 req->cq = cq; in nvmet_req_init()
953 req->sq = sq; in nvmet_req_init()
954 req->ops = ops; in nvmet_req_init()
955 req->sg = NULL; in nvmet_req_init()
956 req->metadata_sg = NULL; in nvmet_req_init()
957 req->sg_cnt = 0; in nvmet_req_init()
958 req->metadata_sg_cnt = 0; in nvmet_req_init()
959 req->transfer_len = 0; in nvmet_req_init()
960 req->metadata_len = 0; in nvmet_req_init()
961 req->cqe->result.u64 = 0; in nvmet_req_init()
962 req->cqe->status = 0; in nvmet_req_init()
963 req->cqe->sq_head = 0; in nvmet_req_init()
964 req->ns = NULL; in nvmet_req_init()
965 req->error_loc = NVMET_NO_ERROR_LOC; in nvmet_req_init()
966 req->error_slba = 0; in nvmet_req_init()
970 req->error_loc = offsetof(struct nvme_common_command, flags); in nvmet_req_init()
981 req->error_loc = offsetof(struct nvme_common_command, flags); in nvmet_req_init()
986 if (unlikely(!req->sq->ctrl)) in nvmet_req_init()
987 /* will return an error for any non-connect command: */ in nvmet_req_init()
989 else if (likely(req->sq->qid != 0)) in nvmet_req_init()
997 trace_nvmet_req_init(req, req->cmd); in nvmet_req_init()
999 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) { in nvmet_req_init()
1004 if (sq->ctrl) in nvmet_req_init()
1005 sq->ctrl->reset_tbkas = true; in nvmet_req_init()
1017 percpu_ref_put(&req->sq->ref); in nvmet_req_uninit()
1018 if (req->ns) in nvmet_req_uninit()
1019 nvmet_put_namespace(req->ns); in nvmet_req_uninit()
1025 if (unlikely(len != req->transfer_len)) { in nvmet_check_transfer_len()
1026 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_check_transfer_len()
1037 if (unlikely(data_len > req->transfer_len)) { in nvmet_check_data_len_lte()
1038 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_check_data_len_lte()
1048 return req->transfer_len - req->metadata_len; in nvmet_data_transfer_len()
1054 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt, in nvmet_req_alloc_p2pmem_sgls()
1056 if (!req->sg) in nvmet_req_alloc_p2pmem_sgls()
1059 if (req->metadata_len) { in nvmet_req_alloc_p2pmem_sgls()
1060 req->metadata_sg = pci_p2pmem_alloc_sgl(p2p_dev, in nvmet_req_alloc_p2pmem_sgls()
1061 &req->metadata_sg_cnt, req->metadata_len); in nvmet_req_alloc_p2pmem_sgls()
1062 if (!req->metadata_sg) in nvmet_req_alloc_p2pmem_sgls()
1066 req->p2p_dev = p2p_dev; in nvmet_req_alloc_p2pmem_sgls()
1070 pci_p2pmem_free_sgl(req->p2p_dev, req->sg); in nvmet_req_alloc_p2pmem_sgls()
1072 return -ENOMEM; in nvmet_req_alloc_p2pmem_sgls()
1078 !req->sq->ctrl || !req->sq->qid || !req->ns) in nvmet_req_find_p2p_dev()
1080 return radix_tree_lookup(&req->sq->ctrl->p2p_ns_map, req->ns->nsid); in nvmet_req_find_p2p_dev()
1090 req->sg = sgl_alloc(nvmet_data_transfer_len(req), GFP_KERNEL, in nvmet_req_alloc_sgls()
1091 &req->sg_cnt); in nvmet_req_alloc_sgls()
1092 if (unlikely(!req->sg)) in nvmet_req_alloc_sgls()
1095 if (req->metadata_len) { in nvmet_req_alloc_sgls()
1096 req->metadata_sg = sgl_alloc(req->metadata_len, GFP_KERNEL, in nvmet_req_alloc_sgls()
1097 &req->metadata_sg_cnt); in nvmet_req_alloc_sgls()
1098 if (unlikely(!req->metadata_sg)) in nvmet_req_alloc_sgls()
1104 sgl_free(req->sg); in nvmet_req_alloc_sgls()
1106 return -ENOMEM; in nvmet_req_alloc_sgls()
1112 if (req->p2p_dev) { in nvmet_req_free_sgls()
1113 pci_p2pmem_free_sgl(req->p2p_dev, req->sg); in nvmet_req_free_sgls()
1114 if (req->metadata_sg) in nvmet_req_free_sgls()
1115 pci_p2pmem_free_sgl(req->p2p_dev, req->metadata_sg); in nvmet_req_free_sgls()
1116 req->p2p_dev = NULL; in nvmet_req_free_sgls()
1118 sgl_free(req->sg); in nvmet_req_free_sgls()
1119 if (req->metadata_sg) in nvmet_req_free_sgls()
1120 sgl_free(req->metadata_sg); in nvmet_req_free_sgls()
1123 req->sg = NULL; in nvmet_req_free_sgls()
1124 req->metadata_sg = NULL; in nvmet_req_free_sgls()
1125 req->sg_cnt = 0; in nvmet_req_free_sgls()
1126 req->metadata_sg_cnt = 0; in nvmet_req_free_sgls()
1178 lockdep_assert_held(&ctrl->lock); in nvmet_start_ctrl()
1186 if (!nvmet_is_disc_subsys(ctrl->subsys) && in nvmet_start_ctrl()
1187 (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES || in nvmet_start_ctrl()
1188 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) { in nvmet_start_ctrl()
1189 ctrl->csts = NVME_CSTS_CFS; in nvmet_start_ctrl()
1193 if (nvmet_cc_mps(ctrl->cc) != 0 || in nvmet_start_ctrl()
1194 nvmet_cc_ams(ctrl->cc) != 0 || in nvmet_start_ctrl()
1195 !nvmet_css_supported(nvmet_cc_css(ctrl->cc))) { in nvmet_start_ctrl()
1196 ctrl->csts = NVME_CSTS_CFS; in nvmet_start_ctrl()
1200 ctrl->csts = NVME_CSTS_RDY; in nvmet_start_ctrl()
1208 if (ctrl->kato) in nvmet_start_ctrl()
1209 mod_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); in nvmet_start_ctrl()
1214 lockdep_assert_held(&ctrl->lock); in nvmet_clear_ctrl()
1217 ctrl->csts &= ~NVME_CSTS_RDY; in nvmet_clear_ctrl()
1218 ctrl->cc = 0; in nvmet_clear_ctrl()
1225 mutex_lock(&ctrl->lock); in nvmet_update_cc()
1226 old = ctrl->cc; in nvmet_update_cc()
1227 ctrl->cc = new; in nvmet_update_cc()
1235 ctrl->csts |= NVME_CSTS_SHST_CMPLT; in nvmet_update_cc()
1238 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT; in nvmet_update_cc()
1239 mutex_unlock(&ctrl->lock); in nvmet_update_cc()
1245 ctrl->cap = (1ULL << 37); in nvmet_init_cap()
1246 /* Controller supports one or more I/O Command Sets */ in nvmet_init_cap()
1247 ctrl->cap |= (1ULL << 43); in nvmet_init_cap()
1249 ctrl->cap |= (15ULL << 24); in nvmet_init_cap()
1251 if (ctrl->ops->get_max_queue_size) in nvmet_init_cap()
1252 ctrl->cap |= min_t(u16, ctrl->ops->get_max_queue_size(ctrl), in nvmet_init_cap()
1253 ctrl->port->max_queue_size) - 1; in nvmet_init_cap()
1255 ctrl->cap |= ctrl->port->max_queue_size - 1; in nvmet_init_cap()
1257 if (nvmet_is_passthru_subsys(ctrl->subsys)) in nvmet_init_cap()
1268 subsys = nvmet_find_get_subsys(req->port, subsysnqn); in nvmet_ctrl_find_get()
1272 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn); in nvmet_ctrl_find_get()
1276 mutex_lock(&subsys->lock); in nvmet_ctrl_find_get()
1277 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { in nvmet_ctrl_find_get()
1278 if (ctrl->cntlid == cntlid) { in nvmet_ctrl_find_get()
1279 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) { in nvmet_ctrl_find_get()
1283 if (!kref_get_unless_zero(&ctrl->ref)) in nvmet_ctrl_find_get()
1294 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid); in nvmet_ctrl_find_get()
1297 mutex_unlock(&subsys->lock); in nvmet_ctrl_find_get()
1305 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) { in nvmet_check_ctrl_status()
1307 req->cmd->common.opcode, req->sq->qid); in nvmet_check_ctrl_status()
1311 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) { in nvmet_check_ctrl_status()
1313 req->cmd->common.opcode, req->sq->qid); in nvmet_check_ctrl_status()
1318 pr_warn("qid %d not authenticated\n", req->sq->qid); in nvmet_check_ctrl_status()
1330 if (subsys->allow_any_host) in nvmet_host_allowed()
1336 list_for_each_entry(p, &subsys->hosts, entry) { in nvmet_host_allowed()
1337 if (!strcmp(nvmet_host_name(p->host), hostnqn)) in nvmet_host_allowed()
1345 * Note: ctrl->subsys->lock should be held when calling this function
1353 if (!req->p2p_client) in nvmet_setup_p2p_ns_map()
1356 ctrl->p2p_client = get_device(req->p2p_client); in nvmet_setup_p2p_ns_map()
1358 xa_for_each(&ctrl->subsys->namespaces, idx, ns) in nvmet_setup_p2p_ns_map()
1363 * Note: ctrl->subsys->lock should be held when calling this function
1370 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0) in nvmet_release_p2p_ns_map()
1373 put_device(ctrl->p2p_client); in nvmet_release_p2p_ns_map()
1381 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid); in nvmet_fatal_error_handler()
1382 ctrl->ops->delete_ctrl(ctrl); in nvmet_fatal_error_handler()
1394 subsys = nvmet_find_get_subsys(req->port, subsysnqn); in nvmet_alloc_ctrl()
1398 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn); in nvmet_alloc_ctrl()
1399 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_alloc_ctrl()
1407 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn); in nvmet_alloc_ctrl()
1410 req->error_loc = offsetof(struct nvme_common_command, dptr); in nvmet_alloc_ctrl()
1419 mutex_init(&ctrl->lock); in nvmet_alloc_ctrl()
1421 ctrl->port = req->port; in nvmet_alloc_ctrl()
1422 ctrl->ops = req->ops; in nvmet_alloc_ctrl()
1426 if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP) in nvmet_alloc_ctrl()
1427 subsys->clear_ids = 1; in nvmet_alloc_ctrl()
1430 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work); in nvmet_alloc_ctrl()
1431 INIT_LIST_HEAD(&ctrl->async_events); in nvmet_alloc_ctrl()
1432 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL); in nvmet_alloc_ctrl()
1433 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler); in nvmet_alloc_ctrl()
1434 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer); in nvmet_alloc_ctrl()
1436 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE); in nvmet_alloc_ctrl()
1437 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE); in nvmet_alloc_ctrl()
1439 kref_init(&ctrl->ref); in nvmet_alloc_ctrl()
1440 ctrl->subsys = subsys; in nvmet_alloc_ctrl()
1441 ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support; in nvmet_alloc_ctrl()
1443 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL); in nvmet_alloc_ctrl()
1445 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES, in nvmet_alloc_ctrl()
1447 if (!ctrl->changed_ns_list) in nvmet_alloc_ctrl()
1450 ctrl->sqs = kcalloc(subsys->max_qid + 1, in nvmet_alloc_ctrl()
1453 if (!ctrl->sqs) in nvmet_alloc_ctrl()
1457 subsys->cntlid_min, subsys->cntlid_max, in nvmet_alloc_ctrl()
1463 ctrl->cntlid = ret; in nvmet_alloc_ctrl()
1469 if (nvmet_is_disc_subsys(ctrl->subsys) && !kato) in nvmet_alloc_ctrl()
1472 /* keep-alive timeout in seconds */ in nvmet_alloc_ctrl()
1473 ctrl->kato = DIV_ROUND_UP(kato, 1000); in nvmet_alloc_ctrl()
1475 ctrl->err_counter = 0; in nvmet_alloc_ctrl()
1476 spin_lock_init(&ctrl->error_lock); in nvmet_alloc_ctrl()
1480 mutex_lock(&subsys->lock); in nvmet_alloc_ctrl()
1481 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); in nvmet_alloc_ctrl()
1484 mutex_unlock(&subsys->lock); in nvmet_alloc_ctrl()
1490 kfree(ctrl->sqs); in nvmet_alloc_ctrl()
1492 kfree(ctrl->changed_ns_list); in nvmet_alloc_ctrl()
1504 struct nvmet_subsys *subsys = ctrl->subsys; in nvmet_ctrl_free()
1506 mutex_lock(&subsys->lock); in nvmet_ctrl_free()
1508 list_del(&ctrl->subsys_entry); in nvmet_ctrl_free()
1509 mutex_unlock(&subsys->lock); in nvmet_ctrl_free()
1513 flush_work(&ctrl->async_event_work); in nvmet_ctrl_free()
1514 cancel_work_sync(&ctrl->fatal_err_work); in nvmet_ctrl_free()
1520 ida_free(&cntlid_ida, ctrl->cntlid); in nvmet_ctrl_free()
1523 kfree(ctrl->sqs); in nvmet_ctrl_free()
1524 kfree(ctrl->changed_ns_list); in nvmet_ctrl_free()
1532 kref_put(&ctrl->ref, nvmet_ctrl_free); in nvmet_ctrl_put()
1537 mutex_lock(&ctrl->lock); in nvmet_ctrl_fatal_error()
1538 if (!(ctrl->csts & NVME_CSTS_CFS)) { in nvmet_ctrl_fatal_error()
1539 ctrl->csts |= NVME_CSTS_CFS; in nvmet_ctrl_fatal_error()
1540 queue_work(nvmet_wq, &ctrl->fatal_err_work); in nvmet_ctrl_fatal_error()
1542 mutex_unlock(&ctrl->lock); in nvmet_ctrl_fatal_error()
1549 if (!ctrl->ops->host_traddr) in nvmet_ctrl_host_traddr()
1550 return -EOPNOTSUPP; in nvmet_ctrl_host_traddr()
1551 return ctrl->ops->host_traddr(ctrl, traddr, traddr_len); in nvmet_ctrl_host_traddr()
1563 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref)) in nvmet_find_get_subsys()
1569 if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn, in nvmet_find_get_subsys()
1571 if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) { in nvmet_find_get_subsys()
1576 list_for_each_entry(p, &port->subsystems, entry) { in nvmet_find_get_subsys()
1577 if (!strncmp(p->subsys->subsysnqn, subsysnqn, in nvmet_find_get_subsys()
1579 if (!kref_get_unless_zero(&p->subsys->ref)) in nvmet_find_get_subsys()
1582 return p->subsys; in nvmet_find_get_subsys()
1598 return ERR_PTR(-ENOMEM); in nvmet_subsys_alloc()
1600 subsys->ver = NVMET_DEFAULT_VS; in nvmet_subsys_alloc()
1603 bin2hex(subsys->serial, &serial, sizeof(serial)); in nvmet_subsys_alloc()
1605 subsys->model_number = kstrdup(NVMET_DEFAULT_CTRL_MODEL, GFP_KERNEL); in nvmet_subsys_alloc()
1606 if (!subsys->model_number) { in nvmet_subsys_alloc()
1607 ret = -ENOMEM; in nvmet_subsys_alloc()
1611 subsys->ieee_oui = 0; in nvmet_subsys_alloc()
1613 subsys->firmware_rev = kstrndup(UTS_RELEASE, NVMET_FR_MAX_SIZE, GFP_KERNEL); in nvmet_subsys_alloc()
1614 if (!subsys->firmware_rev) { in nvmet_subsys_alloc()
1615 ret = -ENOMEM; in nvmet_subsys_alloc()
1621 subsys->max_qid = NVMET_NR_QUEUES; in nvmet_subsys_alloc()
1625 subsys->max_qid = 0; in nvmet_subsys_alloc()
1628 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type); in nvmet_subsys_alloc()
1629 ret = -EINVAL; in nvmet_subsys_alloc()
1632 subsys->type = type; in nvmet_subsys_alloc()
1633 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE, in nvmet_subsys_alloc()
1635 if (!subsys->subsysnqn) { in nvmet_subsys_alloc()
1636 ret = -ENOMEM; in nvmet_subsys_alloc()
1639 subsys->cntlid_min = NVME_CNTLID_MIN; in nvmet_subsys_alloc()
1640 subsys->cntlid_max = NVME_CNTLID_MAX; in nvmet_subsys_alloc()
1641 kref_init(&subsys->ref); in nvmet_subsys_alloc()
1643 mutex_init(&subsys->lock); in nvmet_subsys_alloc()
1644 xa_init(&subsys->namespaces); in nvmet_subsys_alloc()
1645 INIT_LIST_HEAD(&subsys->ctrls); in nvmet_subsys_alloc()
1646 INIT_LIST_HEAD(&subsys->hosts); in nvmet_subsys_alloc()
1655 kfree(subsys->subsysnqn); in nvmet_subsys_alloc()
1657 kfree(subsys->firmware_rev); in nvmet_subsys_alloc()
1659 kfree(subsys->model_number); in nvmet_subsys_alloc()
1670 WARN_ON_ONCE(!xa_empty(&subsys->namespaces)); in nvmet_subsys_free()
1674 xa_destroy(&subsys->namespaces); in nvmet_subsys_free()
1677 kfree(subsys->subsysnqn); in nvmet_subsys_free()
1678 kfree(subsys->model_number); in nvmet_subsys_free()
1679 kfree(subsys->firmware_rev); in nvmet_subsys_free()
1687 mutex_lock(&subsys->lock); in nvmet_subsys_del_ctrls()
1688 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) in nvmet_subsys_del_ctrls()
1689 ctrl->ops->delete_ctrl(ctrl); in nvmet_subsys_del_ctrls()
1690 mutex_unlock(&subsys->lock); in nvmet_subsys_del_ctrls()
1695 kref_put(&subsys->ref, nvmet_subsys_free); in nvmet_subsys_put()
1700 int error = -ENOMEM; in nvmet_init()
1704 nvmet_bvec_cache = kmem_cache_create("nvmet-bvec", in nvmet_init()
1708 return -ENOMEM; in nvmet_init()
1710 zbd_wq = alloc_workqueue("nvmet-zbd-wq", WQ_MEM_RECLAIM, 0); in nvmet_init()
1714 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq", in nvmet_init()
1719 nvmet_wq = alloc_workqueue("nvmet-wq", in nvmet_init()