18f000cacSChristoph Hellwig /* 28f000cacSChristoph Hellwig * NVMe over Fabrics RDMA target. 38f000cacSChristoph Hellwig * Copyright (c) 2015-2016 HGST, a Western Digital Company. 48f000cacSChristoph Hellwig * 58f000cacSChristoph Hellwig * This program is free software; you can redistribute it and/or modify it 68f000cacSChristoph Hellwig * under the terms and conditions of the GNU General Public License, 78f000cacSChristoph Hellwig * version 2, as published by the Free Software Foundation. 88f000cacSChristoph Hellwig * 98f000cacSChristoph Hellwig * This program is distributed in the hope it will be useful, but WITHOUT 108f000cacSChristoph Hellwig * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 118f000cacSChristoph Hellwig * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 128f000cacSChristoph Hellwig * more details. 138f000cacSChristoph Hellwig */ 148f000cacSChristoph Hellwig #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 158f000cacSChristoph Hellwig #include <linux/atomic.h> 168f000cacSChristoph Hellwig #include <linux/ctype.h> 178f000cacSChristoph Hellwig #include <linux/delay.h> 188f000cacSChristoph Hellwig #include <linux/err.h> 198f000cacSChristoph Hellwig #include <linux/init.h> 208f000cacSChristoph Hellwig #include <linux/module.h> 218f000cacSChristoph Hellwig #include <linux/nvme.h> 228f000cacSChristoph Hellwig #include <linux/slab.h> 238f000cacSChristoph Hellwig #include <linux/string.h> 248f000cacSChristoph Hellwig #include <linux/wait.h> 258f000cacSChristoph Hellwig #include <linux/inet.h> 268f000cacSChristoph Hellwig #include <asm/unaligned.h> 278f000cacSChristoph Hellwig 288f000cacSChristoph Hellwig #include <rdma/ib_verbs.h> 298f000cacSChristoph Hellwig #include <rdma/rdma_cm.h> 308f000cacSChristoph Hellwig #include <rdma/rw.h> 318f000cacSChristoph Hellwig 328f000cacSChristoph Hellwig #include <linux/nvme-rdma.h> 338f000cacSChristoph Hellwig #include "nvmet.h" 348f000cacSChristoph Hellwig 358f000cacSChristoph Hellwig /* 368f000cacSChristoph Hellwig * We allow up to a page of inline data to go with the SQE 378f000cacSChristoph Hellwig */ 388f000cacSChristoph Hellwig #define NVMET_RDMA_INLINE_DATA_SIZE PAGE_SIZE 398f000cacSChristoph Hellwig 408f000cacSChristoph Hellwig struct nvmet_rdma_cmd { 418f000cacSChristoph Hellwig struct ib_sge sge[2]; 428f000cacSChristoph Hellwig struct ib_cqe cqe; 438f000cacSChristoph Hellwig struct ib_recv_wr wr; 448f000cacSChristoph Hellwig struct scatterlist inline_sg; 458f000cacSChristoph Hellwig struct page *inline_page; 468f000cacSChristoph Hellwig struct nvme_command *nvme_cmd; 478f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 488f000cacSChristoph Hellwig }; 498f000cacSChristoph Hellwig 508f000cacSChristoph Hellwig enum { 518f000cacSChristoph Hellwig NVMET_RDMA_REQ_INLINE_DATA = (1 << 0), 528f000cacSChristoph Hellwig NVMET_RDMA_REQ_INVALIDATE_RKEY = (1 << 1), 538f000cacSChristoph Hellwig }; 548f000cacSChristoph Hellwig 558f000cacSChristoph Hellwig struct nvmet_rdma_rsp { 568f000cacSChristoph Hellwig struct ib_sge send_sge; 578f000cacSChristoph Hellwig struct ib_cqe send_cqe; 588f000cacSChristoph Hellwig struct ib_send_wr send_wr; 598f000cacSChristoph Hellwig 608f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmd; 618f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 628f000cacSChristoph Hellwig 638f000cacSChristoph Hellwig struct ib_cqe read_cqe; 648f000cacSChristoph Hellwig struct rdma_rw_ctx rw; 658f000cacSChristoph Hellwig 668f000cacSChristoph Hellwig struct nvmet_req req; 678f000cacSChristoph Hellwig 688f000cacSChristoph Hellwig u8 n_rdma; 698f000cacSChristoph Hellwig u32 flags; 708f000cacSChristoph Hellwig u32 invalidate_rkey; 718f000cacSChristoph Hellwig 728f000cacSChristoph Hellwig struct list_head wait_list; 738f000cacSChristoph Hellwig struct list_head free_list; 748f000cacSChristoph Hellwig }; 758f000cacSChristoph Hellwig 768f000cacSChristoph Hellwig enum nvmet_rdma_queue_state { 778f000cacSChristoph Hellwig NVMET_RDMA_Q_CONNECTING, 788f000cacSChristoph Hellwig NVMET_RDMA_Q_LIVE, 798f000cacSChristoph Hellwig NVMET_RDMA_Q_DISCONNECTING, 80d8f7750aSSagi Grimberg NVMET_RDMA_IN_DEVICE_REMOVAL, 818f000cacSChristoph Hellwig }; 828f000cacSChristoph Hellwig 838f000cacSChristoph Hellwig struct nvmet_rdma_queue { 848f000cacSChristoph Hellwig struct rdma_cm_id *cm_id; 858f000cacSChristoph Hellwig struct nvmet_port *port; 868f000cacSChristoph Hellwig struct ib_cq *cq; 878f000cacSChristoph Hellwig atomic_t sq_wr_avail; 888f000cacSChristoph Hellwig struct nvmet_rdma_device *dev; 898f000cacSChristoph Hellwig spinlock_t state_lock; 908f000cacSChristoph Hellwig enum nvmet_rdma_queue_state state; 918f000cacSChristoph Hellwig struct nvmet_cq nvme_cq; 928f000cacSChristoph Hellwig struct nvmet_sq nvme_sq; 938f000cacSChristoph Hellwig 948f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsps; 958f000cacSChristoph Hellwig struct list_head free_rsps; 968f000cacSChristoph Hellwig spinlock_t rsps_lock; 978f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmds; 988f000cacSChristoph Hellwig 998f000cacSChristoph Hellwig struct work_struct release_work; 1008f000cacSChristoph Hellwig struct list_head rsp_wait_list; 1018f000cacSChristoph Hellwig struct list_head rsp_wr_wait_list; 1028f000cacSChristoph Hellwig spinlock_t rsp_wr_wait_lock; 1038f000cacSChristoph Hellwig 1048f000cacSChristoph Hellwig int idx; 1058f000cacSChristoph Hellwig int host_qid; 1068f000cacSChristoph Hellwig int recv_queue_size; 1078f000cacSChristoph Hellwig int send_queue_size; 1088f000cacSChristoph Hellwig 1098f000cacSChristoph Hellwig struct list_head queue_list; 1108f000cacSChristoph Hellwig }; 1118f000cacSChristoph Hellwig 1128f000cacSChristoph Hellwig struct nvmet_rdma_device { 1138f000cacSChristoph Hellwig struct ib_device *device; 1148f000cacSChristoph Hellwig struct ib_pd *pd; 1158f000cacSChristoph Hellwig struct ib_srq *srq; 1168f000cacSChristoph Hellwig struct nvmet_rdma_cmd *srq_cmds; 1178f000cacSChristoph Hellwig size_t srq_size; 1188f000cacSChristoph Hellwig struct kref ref; 1198f000cacSChristoph Hellwig struct list_head entry; 1208f000cacSChristoph Hellwig }; 1218f000cacSChristoph Hellwig 1228f000cacSChristoph Hellwig static bool nvmet_rdma_use_srq; 1238f000cacSChristoph Hellwig module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444); 1248f000cacSChristoph Hellwig MODULE_PARM_DESC(use_srq, "Use shared receive queue."); 1258f000cacSChristoph Hellwig 1268f000cacSChristoph Hellwig static DEFINE_IDA(nvmet_rdma_queue_ida); 1278f000cacSChristoph Hellwig static LIST_HEAD(nvmet_rdma_queue_list); 1288f000cacSChristoph Hellwig static DEFINE_MUTEX(nvmet_rdma_queue_mutex); 1298f000cacSChristoph Hellwig 1308f000cacSChristoph Hellwig static LIST_HEAD(device_list); 1318f000cacSChristoph Hellwig static DEFINE_MUTEX(device_list_mutex); 1328f000cacSChristoph Hellwig 1338f000cacSChristoph Hellwig static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp); 1348f000cacSChristoph Hellwig static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc); 1358f000cacSChristoph Hellwig static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); 1368f000cacSChristoph Hellwig static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc); 1378f000cacSChristoph Hellwig static void nvmet_rdma_qp_event(struct ib_event *event, void *priv); 1388f000cacSChristoph Hellwig static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue); 1398f000cacSChristoph Hellwig 1408f000cacSChristoph Hellwig static struct nvmet_fabrics_ops nvmet_rdma_ops; 1418f000cacSChristoph Hellwig 1428f000cacSChristoph Hellwig /* XXX: really should move to a generic header sooner or later.. */ 1438f000cacSChristoph Hellwig static inline u32 get_unaligned_le24(const u8 *p) 1448f000cacSChristoph Hellwig { 1458f000cacSChristoph Hellwig return (u32)p[0] | (u32)p[1] << 8 | (u32)p[2] << 16; 1468f000cacSChristoph Hellwig } 1478f000cacSChristoph Hellwig 1488f000cacSChristoph Hellwig static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp) 1498f000cacSChristoph Hellwig { 1508f000cacSChristoph Hellwig return nvme_is_write(rsp->req.cmd) && 1518f000cacSChristoph Hellwig rsp->req.data_len && 1528f000cacSChristoph Hellwig !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 1538f000cacSChristoph Hellwig } 1548f000cacSChristoph Hellwig 1558f000cacSChristoph Hellwig static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp) 1568f000cacSChristoph Hellwig { 1578f000cacSChristoph Hellwig return !nvme_is_write(rsp->req.cmd) && 1588f000cacSChristoph Hellwig rsp->req.data_len && 1598f000cacSChristoph Hellwig !rsp->req.rsp->status && 1608f000cacSChristoph Hellwig !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 1618f000cacSChristoph Hellwig } 1628f000cacSChristoph Hellwig 1638f000cacSChristoph Hellwig static inline struct nvmet_rdma_rsp * 1648f000cacSChristoph Hellwig nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue) 1658f000cacSChristoph Hellwig { 1668f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp; 1678f000cacSChristoph Hellwig unsigned long flags; 1688f000cacSChristoph Hellwig 1698f000cacSChristoph Hellwig spin_lock_irqsave(&queue->rsps_lock, flags); 1708f000cacSChristoph Hellwig rsp = list_first_entry(&queue->free_rsps, 1718f000cacSChristoph Hellwig struct nvmet_rdma_rsp, free_list); 1728f000cacSChristoph Hellwig list_del(&rsp->free_list); 1738f000cacSChristoph Hellwig spin_unlock_irqrestore(&queue->rsps_lock, flags); 1748f000cacSChristoph Hellwig 1758f000cacSChristoph Hellwig return rsp; 1768f000cacSChristoph Hellwig } 1778f000cacSChristoph Hellwig 1788f000cacSChristoph Hellwig static inline void 1798f000cacSChristoph Hellwig nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp) 1808f000cacSChristoph Hellwig { 1818f000cacSChristoph Hellwig unsigned long flags; 1828f000cacSChristoph Hellwig 1838f000cacSChristoph Hellwig spin_lock_irqsave(&rsp->queue->rsps_lock, flags); 1848f000cacSChristoph Hellwig list_add_tail(&rsp->free_list, &rsp->queue->free_rsps); 1858f000cacSChristoph Hellwig spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags); 1868f000cacSChristoph Hellwig } 1878f000cacSChristoph Hellwig 1888f000cacSChristoph Hellwig static void nvmet_rdma_free_sgl(struct scatterlist *sgl, unsigned int nents) 1898f000cacSChristoph Hellwig { 1908f000cacSChristoph Hellwig struct scatterlist *sg; 1918f000cacSChristoph Hellwig int count; 1928f000cacSChristoph Hellwig 1938f000cacSChristoph Hellwig if (!sgl || !nents) 1948f000cacSChristoph Hellwig return; 1958f000cacSChristoph Hellwig 1968f000cacSChristoph Hellwig for_each_sg(sgl, sg, nents, count) 1978f000cacSChristoph Hellwig __free_page(sg_page(sg)); 1988f000cacSChristoph Hellwig kfree(sgl); 1998f000cacSChristoph Hellwig } 2008f000cacSChristoph Hellwig 2018f000cacSChristoph Hellwig static int nvmet_rdma_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, 2028f000cacSChristoph Hellwig u32 length) 2038f000cacSChristoph Hellwig { 2048f000cacSChristoph Hellwig struct scatterlist *sg; 2058f000cacSChristoph Hellwig struct page *page; 2068f000cacSChristoph Hellwig unsigned int nent; 2078f000cacSChristoph Hellwig int i = 0; 2088f000cacSChristoph Hellwig 2098f000cacSChristoph Hellwig nent = DIV_ROUND_UP(length, PAGE_SIZE); 2108f000cacSChristoph Hellwig sg = kmalloc_array(nent, sizeof(struct scatterlist), GFP_KERNEL); 2118f000cacSChristoph Hellwig if (!sg) 2128f000cacSChristoph Hellwig goto out; 2138f000cacSChristoph Hellwig 2148f000cacSChristoph Hellwig sg_init_table(sg, nent); 2158f000cacSChristoph Hellwig 2168f000cacSChristoph Hellwig while (length) { 2178f000cacSChristoph Hellwig u32 page_len = min_t(u32, length, PAGE_SIZE); 2188f000cacSChristoph Hellwig 2198f000cacSChristoph Hellwig page = alloc_page(GFP_KERNEL); 2208f000cacSChristoph Hellwig if (!page) 2218f000cacSChristoph Hellwig goto out_free_pages; 2228f000cacSChristoph Hellwig 2238f000cacSChristoph Hellwig sg_set_page(&sg[i], page, page_len, 0); 2248f000cacSChristoph Hellwig length -= page_len; 2258f000cacSChristoph Hellwig i++; 2268f000cacSChristoph Hellwig } 2278f000cacSChristoph Hellwig *sgl = sg; 2288f000cacSChristoph Hellwig *nents = nent; 2298f000cacSChristoph Hellwig return 0; 2308f000cacSChristoph Hellwig 2318f000cacSChristoph Hellwig out_free_pages: 2328f000cacSChristoph Hellwig while (i > 0) { 2338f000cacSChristoph Hellwig i--; 2348f000cacSChristoph Hellwig __free_page(sg_page(&sg[i])); 2358f000cacSChristoph Hellwig } 2368f000cacSChristoph Hellwig kfree(sg); 2378f000cacSChristoph Hellwig out: 2388f000cacSChristoph Hellwig return NVME_SC_INTERNAL; 2398f000cacSChristoph Hellwig } 2408f000cacSChristoph Hellwig 2418f000cacSChristoph Hellwig static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev, 2428f000cacSChristoph Hellwig struct nvmet_rdma_cmd *c, bool admin) 2438f000cacSChristoph Hellwig { 2448f000cacSChristoph Hellwig /* NVMe command / RDMA RECV */ 2458f000cacSChristoph Hellwig c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL); 2468f000cacSChristoph Hellwig if (!c->nvme_cmd) 2478f000cacSChristoph Hellwig goto out; 2488f000cacSChristoph Hellwig 2498f000cacSChristoph Hellwig c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd, 2508f000cacSChristoph Hellwig sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 2518f000cacSChristoph Hellwig if (ib_dma_mapping_error(ndev->device, c->sge[0].addr)) 2528f000cacSChristoph Hellwig goto out_free_cmd; 2538f000cacSChristoph Hellwig 2548f000cacSChristoph Hellwig c->sge[0].length = sizeof(*c->nvme_cmd); 2558f000cacSChristoph Hellwig c->sge[0].lkey = ndev->pd->local_dma_lkey; 2568f000cacSChristoph Hellwig 2578f000cacSChristoph Hellwig if (!admin) { 2588f000cacSChristoph Hellwig c->inline_page = alloc_pages(GFP_KERNEL, 2598f000cacSChristoph Hellwig get_order(NVMET_RDMA_INLINE_DATA_SIZE)); 2608f000cacSChristoph Hellwig if (!c->inline_page) 2618f000cacSChristoph Hellwig goto out_unmap_cmd; 2628f000cacSChristoph Hellwig c->sge[1].addr = ib_dma_map_page(ndev->device, 2638f000cacSChristoph Hellwig c->inline_page, 0, NVMET_RDMA_INLINE_DATA_SIZE, 2648f000cacSChristoph Hellwig DMA_FROM_DEVICE); 2658f000cacSChristoph Hellwig if (ib_dma_mapping_error(ndev->device, c->sge[1].addr)) 2668f000cacSChristoph Hellwig goto out_free_inline_page; 2678f000cacSChristoph Hellwig c->sge[1].length = NVMET_RDMA_INLINE_DATA_SIZE; 2688f000cacSChristoph Hellwig c->sge[1].lkey = ndev->pd->local_dma_lkey; 2698f000cacSChristoph Hellwig } 2708f000cacSChristoph Hellwig 2718f000cacSChristoph Hellwig c->cqe.done = nvmet_rdma_recv_done; 2728f000cacSChristoph Hellwig 2738f000cacSChristoph Hellwig c->wr.wr_cqe = &c->cqe; 2748f000cacSChristoph Hellwig c->wr.sg_list = c->sge; 2758f000cacSChristoph Hellwig c->wr.num_sge = admin ? 1 : 2; 2768f000cacSChristoph Hellwig 2778f000cacSChristoph Hellwig return 0; 2788f000cacSChristoph Hellwig 2798f000cacSChristoph Hellwig out_free_inline_page: 2808f000cacSChristoph Hellwig if (!admin) { 2818f000cacSChristoph Hellwig __free_pages(c->inline_page, 2828f000cacSChristoph Hellwig get_order(NVMET_RDMA_INLINE_DATA_SIZE)); 2838f000cacSChristoph Hellwig } 2848f000cacSChristoph Hellwig out_unmap_cmd: 2858f000cacSChristoph Hellwig ib_dma_unmap_single(ndev->device, c->sge[0].addr, 2868f000cacSChristoph Hellwig sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 2878f000cacSChristoph Hellwig out_free_cmd: 2888f000cacSChristoph Hellwig kfree(c->nvme_cmd); 2898f000cacSChristoph Hellwig 2908f000cacSChristoph Hellwig out: 2918f000cacSChristoph Hellwig return -ENOMEM; 2928f000cacSChristoph Hellwig } 2938f000cacSChristoph Hellwig 2948f000cacSChristoph Hellwig static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev, 2958f000cacSChristoph Hellwig struct nvmet_rdma_cmd *c, bool admin) 2968f000cacSChristoph Hellwig { 2978f000cacSChristoph Hellwig if (!admin) { 2988f000cacSChristoph Hellwig ib_dma_unmap_page(ndev->device, c->sge[1].addr, 2998f000cacSChristoph Hellwig NVMET_RDMA_INLINE_DATA_SIZE, DMA_FROM_DEVICE); 3008f000cacSChristoph Hellwig __free_pages(c->inline_page, 3018f000cacSChristoph Hellwig get_order(NVMET_RDMA_INLINE_DATA_SIZE)); 3028f000cacSChristoph Hellwig } 3038f000cacSChristoph Hellwig ib_dma_unmap_single(ndev->device, c->sge[0].addr, 3048f000cacSChristoph Hellwig sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 3058f000cacSChristoph Hellwig kfree(c->nvme_cmd); 3068f000cacSChristoph Hellwig } 3078f000cacSChristoph Hellwig 3088f000cacSChristoph Hellwig static struct nvmet_rdma_cmd * 3098f000cacSChristoph Hellwig nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev, 3108f000cacSChristoph Hellwig int nr_cmds, bool admin) 3118f000cacSChristoph Hellwig { 3128f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmds; 3138f000cacSChristoph Hellwig int ret = -EINVAL, i; 3148f000cacSChristoph Hellwig 3158f000cacSChristoph Hellwig cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL); 3168f000cacSChristoph Hellwig if (!cmds) 3178f000cacSChristoph Hellwig goto out; 3188f000cacSChristoph Hellwig 3198f000cacSChristoph Hellwig for (i = 0; i < nr_cmds; i++) { 3208f000cacSChristoph Hellwig ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin); 3218f000cacSChristoph Hellwig if (ret) 3228f000cacSChristoph Hellwig goto out_free; 3238f000cacSChristoph Hellwig } 3248f000cacSChristoph Hellwig 3258f000cacSChristoph Hellwig return cmds; 3268f000cacSChristoph Hellwig 3278f000cacSChristoph Hellwig out_free: 3288f000cacSChristoph Hellwig while (--i >= 0) 3298f000cacSChristoph Hellwig nvmet_rdma_free_cmd(ndev, cmds + i, admin); 3308f000cacSChristoph Hellwig kfree(cmds); 3318f000cacSChristoph Hellwig out: 3328f000cacSChristoph Hellwig return ERR_PTR(ret); 3338f000cacSChristoph Hellwig } 3348f000cacSChristoph Hellwig 3358f000cacSChristoph Hellwig static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev, 3368f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin) 3378f000cacSChristoph Hellwig { 3388f000cacSChristoph Hellwig int i; 3398f000cacSChristoph Hellwig 3408f000cacSChristoph Hellwig for (i = 0; i < nr_cmds; i++) 3418f000cacSChristoph Hellwig nvmet_rdma_free_cmd(ndev, cmds + i, admin); 3428f000cacSChristoph Hellwig kfree(cmds); 3438f000cacSChristoph Hellwig } 3448f000cacSChristoph Hellwig 3458f000cacSChristoph Hellwig static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, 3468f000cacSChristoph Hellwig struct nvmet_rdma_rsp *r) 3478f000cacSChristoph Hellwig { 3488f000cacSChristoph Hellwig /* NVMe CQE / RDMA SEND */ 3498f000cacSChristoph Hellwig r->req.rsp = kmalloc(sizeof(*r->req.rsp), GFP_KERNEL); 3508f000cacSChristoph Hellwig if (!r->req.rsp) 3518f000cacSChristoph Hellwig goto out; 3528f000cacSChristoph Hellwig 3538f000cacSChristoph Hellwig r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.rsp, 3548f000cacSChristoph Hellwig sizeof(*r->req.rsp), DMA_TO_DEVICE); 3558f000cacSChristoph Hellwig if (ib_dma_mapping_error(ndev->device, r->send_sge.addr)) 3568f000cacSChristoph Hellwig goto out_free_rsp; 3578f000cacSChristoph Hellwig 3588f000cacSChristoph Hellwig r->send_sge.length = sizeof(*r->req.rsp); 3598f000cacSChristoph Hellwig r->send_sge.lkey = ndev->pd->local_dma_lkey; 3608f000cacSChristoph Hellwig 3618f000cacSChristoph Hellwig r->send_cqe.done = nvmet_rdma_send_done; 3628f000cacSChristoph Hellwig 3638f000cacSChristoph Hellwig r->send_wr.wr_cqe = &r->send_cqe; 3648f000cacSChristoph Hellwig r->send_wr.sg_list = &r->send_sge; 3658f000cacSChristoph Hellwig r->send_wr.num_sge = 1; 3668f000cacSChristoph Hellwig r->send_wr.send_flags = IB_SEND_SIGNALED; 3678f000cacSChristoph Hellwig 3688f000cacSChristoph Hellwig /* Data In / RDMA READ */ 3698f000cacSChristoph Hellwig r->read_cqe.done = nvmet_rdma_read_data_done; 3708f000cacSChristoph Hellwig return 0; 3718f000cacSChristoph Hellwig 3728f000cacSChristoph Hellwig out_free_rsp: 3738f000cacSChristoph Hellwig kfree(r->req.rsp); 3748f000cacSChristoph Hellwig out: 3758f000cacSChristoph Hellwig return -ENOMEM; 3768f000cacSChristoph Hellwig } 3778f000cacSChristoph Hellwig 3788f000cacSChristoph Hellwig static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, 3798f000cacSChristoph Hellwig struct nvmet_rdma_rsp *r) 3808f000cacSChristoph Hellwig { 3818f000cacSChristoph Hellwig ib_dma_unmap_single(ndev->device, r->send_sge.addr, 3828f000cacSChristoph Hellwig sizeof(*r->req.rsp), DMA_TO_DEVICE); 3838f000cacSChristoph Hellwig kfree(r->req.rsp); 3848f000cacSChristoph Hellwig } 3858f000cacSChristoph Hellwig 3868f000cacSChristoph Hellwig static int 3878f000cacSChristoph Hellwig nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue) 3888f000cacSChristoph Hellwig { 3898f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev = queue->dev; 3908f000cacSChristoph Hellwig int nr_rsps = queue->recv_queue_size * 2; 3918f000cacSChristoph Hellwig int ret = -EINVAL, i; 3928f000cacSChristoph Hellwig 3938f000cacSChristoph Hellwig queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp), 3948f000cacSChristoph Hellwig GFP_KERNEL); 3958f000cacSChristoph Hellwig if (!queue->rsps) 3968f000cacSChristoph Hellwig goto out; 3978f000cacSChristoph Hellwig 3988f000cacSChristoph Hellwig for (i = 0; i < nr_rsps; i++) { 3998f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 4008f000cacSChristoph Hellwig 4018f000cacSChristoph Hellwig ret = nvmet_rdma_alloc_rsp(ndev, rsp); 4028f000cacSChristoph Hellwig if (ret) 4038f000cacSChristoph Hellwig goto out_free; 4048f000cacSChristoph Hellwig 4058f000cacSChristoph Hellwig list_add_tail(&rsp->free_list, &queue->free_rsps); 4068f000cacSChristoph Hellwig } 4078f000cacSChristoph Hellwig 4088f000cacSChristoph Hellwig return 0; 4098f000cacSChristoph Hellwig 4108f000cacSChristoph Hellwig out_free: 4118f000cacSChristoph Hellwig while (--i >= 0) { 4128f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 4138f000cacSChristoph Hellwig 4148f000cacSChristoph Hellwig list_del(&rsp->free_list); 4158f000cacSChristoph Hellwig nvmet_rdma_free_rsp(ndev, rsp); 4168f000cacSChristoph Hellwig } 4178f000cacSChristoph Hellwig kfree(queue->rsps); 4188f000cacSChristoph Hellwig out: 4198f000cacSChristoph Hellwig return ret; 4208f000cacSChristoph Hellwig } 4218f000cacSChristoph Hellwig 4228f000cacSChristoph Hellwig static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue) 4238f000cacSChristoph Hellwig { 4248f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev = queue->dev; 4258f000cacSChristoph Hellwig int i, nr_rsps = queue->recv_queue_size * 2; 4268f000cacSChristoph Hellwig 4278f000cacSChristoph Hellwig for (i = 0; i < nr_rsps; i++) { 4288f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 4298f000cacSChristoph Hellwig 4308f000cacSChristoph Hellwig list_del(&rsp->free_list); 4318f000cacSChristoph Hellwig nvmet_rdma_free_rsp(ndev, rsp); 4328f000cacSChristoph Hellwig } 4338f000cacSChristoph Hellwig kfree(queue->rsps); 4348f000cacSChristoph Hellwig } 4358f000cacSChristoph Hellwig 4368f000cacSChristoph Hellwig static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev, 4378f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmd) 4388f000cacSChristoph Hellwig { 4398f000cacSChristoph Hellwig struct ib_recv_wr *bad_wr; 4408f000cacSChristoph Hellwig 4418f000cacSChristoph Hellwig if (ndev->srq) 4428f000cacSChristoph Hellwig return ib_post_srq_recv(ndev->srq, &cmd->wr, &bad_wr); 4438f000cacSChristoph Hellwig return ib_post_recv(cmd->queue->cm_id->qp, &cmd->wr, &bad_wr); 4448f000cacSChristoph Hellwig } 4458f000cacSChristoph Hellwig 4468f000cacSChristoph Hellwig static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue) 4478f000cacSChristoph Hellwig { 4488f000cacSChristoph Hellwig spin_lock(&queue->rsp_wr_wait_lock); 4498f000cacSChristoph Hellwig while (!list_empty(&queue->rsp_wr_wait_list)) { 4508f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp; 4518f000cacSChristoph Hellwig bool ret; 4528f000cacSChristoph Hellwig 4538f000cacSChristoph Hellwig rsp = list_entry(queue->rsp_wr_wait_list.next, 4548f000cacSChristoph Hellwig struct nvmet_rdma_rsp, wait_list); 4558f000cacSChristoph Hellwig list_del(&rsp->wait_list); 4568f000cacSChristoph Hellwig 4578f000cacSChristoph Hellwig spin_unlock(&queue->rsp_wr_wait_lock); 4588f000cacSChristoph Hellwig ret = nvmet_rdma_execute_command(rsp); 4598f000cacSChristoph Hellwig spin_lock(&queue->rsp_wr_wait_lock); 4608f000cacSChristoph Hellwig 4618f000cacSChristoph Hellwig if (!ret) { 4628f000cacSChristoph Hellwig list_add(&rsp->wait_list, &queue->rsp_wr_wait_list); 4638f000cacSChristoph Hellwig break; 4648f000cacSChristoph Hellwig } 4658f000cacSChristoph Hellwig } 4668f000cacSChristoph Hellwig spin_unlock(&queue->rsp_wr_wait_lock); 4678f000cacSChristoph Hellwig } 4688f000cacSChristoph Hellwig 4698f000cacSChristoph Hellwig 4708f000cacSChristoph Hellwig static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp) 4718f000cacSChristoph Hellwig { 4728f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = rsp->queue; 4738f000cacSChristoph Hellwig 4748f000cacSChristoph Hellwig atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 4758f000cacSChristoph Hellwig 4768f000cacSChristoph Hellwig if (rsp->n_rdma) { 4778f000cacSChristoph Hellwig rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, 4788f000cacSChristoph Hellwig queue->cm_id->port_num, rsp->req.sg, 4798f000cacSChristoph Hellwig rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); 4808f000cacSChristoph Hellwig } 4818f000cacSChristoph Hellwig 4828f000cacSChristoph Hellwig if (rsp->req.sg != &rsp->cmd->inline_sg) 4838f000cacSChristoph Hellwig nvmet_rdma_free_sgl(rsp->req.sg, rsp->req.sg_cnt); 4848f000cacSChristoph Hellwig 4858f000cacSChristoph Hellwig if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list))) 4868f000cacSChristoph Hellwig nvmet_rdma_process_wr_wait_list(queue); 4878f000cacSChristoph Hellwig 4888f000cacSChristoph Hellwig nvmet_rdma_put_rsp(rsp); 4898f000cacSChristoph Hellwig } 4908f000cacSChristoph Hellwig 4918f000cacSChristoph Hellwig static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue) 4928f000cacSChristoph Hellwig { 4938f000cacSChristoph Hellwig if (queue->nvme_sq.ctrl) { 4948f000cacSChristoph Hellwig nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl); 4958f000cacSChristoph Hellwig } else { 4968f000cacSChristoph Hellwig /* 4978f000cacSChristoph Hellwig * we didn't setup the controller yet in case 4988f000cacSChristoph Hellwig * of admin connect error, just disconnect and 4998f000cacSChristoph Hellwig * cleanup the queue 5008f000cacSChristoph Hellwig */ 5018f000cacSChristoph Hellwig nvmet_rdma_queue_disconnect(queue); 5028f000cacSChristoph Hellwig } 5038f000cacSChristoph Hellwig } 5048f000cacSChristoph Hellwig 5058f000cacSChristoph Hellwig static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 5068f000cacSChristoph Hellwig { 5078f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = 5088f000cacSChristoph Hellwig container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe); 5098f000cacSChristoph Hellwig 5108f000cacSChristoph Hellwig nvmet_rdma_release_rsp(rsp); 5118f000cacSChristoph Hellwig 5128f000cacSChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS && 5138f000cacSChristoph Hellwig wc->status != IB_WC_WR_FLUSH_ERR)) { 5148f000cacSChristoph Hellwig pr_err("SEND for CQE 0x%p failed with status %s (%d).\n", 5158f000cacSChristoph Hellwig wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 5168f000cacSChristoph Hellwig nvmet_rdma_error_comp(rsp->queue); 5178f000cacSChristoph Hellwig } 5188f000cacSChristoph Hellwig } 5198f000cacSChristoph Hellwig 5208f000cacSChristoph Hellwig static void nvmet_rdma_queue_response(struct nvmet_req *req) 5218f000cacSChristoph Hellwig { 5228f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = 5238f000cacSChristoph Hellwig container_of(req, struct nvmet_rdma_rsp, req); 5248f000cacSChristoph Hellwig struct rdma_cm_id *cm_id = rsp->queue->cm_id; 5258f000cacSChristoph Hellwig struct ib_send_wr *first_wr, *bad_wr; 5268f000cacSChristoph Hellwig 5278f000cacSChristoph Hellwig if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) { 5288f000cacSChristoph Hellwig rsp->send_wr.opcode = IB_WR_SEND_WITH_INV; 5298f000cacSChristoph Hellwig rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey; 5308f000cacSChristoph Hellwig } else { 5318f000cacSChristoph Hellwig rsp->send_wr.opcode = IB_WR_SEND; 5328f000cacSChristoph Hellwig } 5338f000cacSChristoph Hellwig 5348f000cacSChristoph Hellwig if (nvmet_rdma_need_data_out(rsp)) 5358f000cacSChristoph Hellwig first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp, 5368f000cacSChristoph Hellwig cm_id->port_num, NULL, &rsp->send_wr); 5378f000cacSChristoph Hellwig else 5388f000cacSChristoph Hellwig first_wr = &rsp->send_wr; 5398f000cacSChristoph Hellwig 5408f000cacSChristoph Hellwig nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd); 5418f000cacSChristoph Hellwig if (ib_post_send(cm_id->qp, first_wr, &bad_wr)) { 5428f000cacSChristoph Hellwig pr_err("sending cmd response failed\n"); 5438f000cacSChristoph Hellwig nvmet_rdma_release_rsp(rsp); 5448f000cacSChristoph Hellwig } 5458f000cacSChristoph Hellwig } 5468f000cacSChristoph Hellwig 5478f000cacSChristoph Hellwig static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc) 5488f000cacSChristoph Hellwig { 5498f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp = 5508f000cacSChristoph Hellwig container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe); 5518f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = cq->cq_context; 5528f000cacSChristoph Hellwig 5538f000cacSChristoph Hellwig WARN_ON(rsp->n_rdma <= 0); 5548f000cacSChristoph Hellwig atomic_add(rsp->n_rdma, &queue->sq_wr_avail); 5558f000cacSChristoph Hellwig rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, 5568f000cacSChristoph Hellwig queue->cm_id->port_num, rsp->req.sg, 5578f000cacSChristoph Hellwig rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); 5588f000cacSChristoph Hellwig rsp->n_rdma = 0; 5598f000cacSChristoph Hellwig 5608f000cacSChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 5618f000cacSChristoph Hellwig nvmet_rdma_release_rsp(rsp); 5628f000cacSChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) { 5638f000cacSChristoph Hellwig pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n", 5648f000cacSChristoph Hellwig wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 5658f000cacSChristoph Hellwig nvmet_rdma_error_comp(queue); 5668f000cacSChristoph Hellwig } 5678f000cacSChristoph Hellwig return; 5688f000cacSChristoph Hellwig } 5698f000cacSChristoph Hellwig 5708f000cacSChristoph Hellwig rsp->req.execute(&rsp->req); 5718f000cacSChristoph Hellwig } 5728f000cacSChristoph Hellwig 5738f000cacSChristoph Hellwig static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len, 5748f000cacSChristoph Hellwig u64 off) 5758f000cacSChristoph Hellwig { 5768f000cacSChristoph Hellwig sg_init_table(&rsp->cmd->inline_sg, 1); 5778f000cacSChristoph Hellwig sg_set_page(&rsp->cmd->inline_sg, rsp->cmd->inline_page, len, off); 5788f000cacSChristoph Hellwig rsp->req.sg = &rsp->cmd->inline_sg; 5798f000cacSChristoph Hellwig rsp->req.sg_cnt = 1; 5808f000cacSChristoph Hellwig } 5818f000cacSChristoph Hellwig 5828f000cacSChristoph Hellwig static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp) 5838f000cacSChristoph Hellwig { 5848f000cacSChristoph Hellwig struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl; 5858f000cacSChristoph Hellwig u64 off = le64_to_cpu(sgl->addr); 5868f000cacSChristoph Hellwig u32 len = le32_to_cpu(sgl->length); 5878f000cacSChristoph Hellwig 5888f000cacSChristoph Hellwig if (!nvme_is_write(rsp->req.cmd)) 5898f000cacSChristoph Hellwig return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 5908f000cacSChristoph Hellwig 5918f000cacSChristoph Hellwig if (off + len > NVMET_RDMA_INLINE_DATA_SIZE) { 5928f000cacSChristoph Hellwig pr_err("invalid inline data offset!\n"); 5938f000cacSChristoph Hellwig return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR; 5948f000cacSChristoph Hellwig } 5958f000cacSChristoph Hellwig 5968f000cacSChristoph Hellwig /* no data command? */ 5978f000cacSChristoph Hellwig if (!len) 5988f000cacSChristoph Hellwig return 0; 5998f000cacSChristoph Hellwig 6008f000cacSChristoph Hellwig nvmet_rdma_use_inline_sg(rsp, len, off); 6018f000cacSChristoph Hellwig rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA; 6028f000cacSChristoph Hellwig return 0; 6038f000cacSChristoph Hellwig } 6048f000cacSChristoph Hellwig 6058f000cacSChristoph Hellwig static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp, 6068f000cacSChristoph Hellwig struct nvme_keyed_sgl_desc *sgl, bool invalidate) 6078f000cacSChristoph Hellwig { 6088f000cacSChristoph Hellwig struct rdma_cm_id *cm_id = rsp->queue->cm_id; 6098f000cacSChristoph Hellwig u64 addr = le64_to_cpu(sgl->addr); 6108f000cacSChristoph Hellwig u32 len = get_unaligned_le24(sgl->length); 6118f000cacSChristoph Hellwig u32 key = get_unaligned_le32(sgl->key); 6128f000cacSChristoph Hellwig int ret; 6138f000cacSChristoph Hellwig u16 status; 6148f000cacSChristoph Hellwig 6158f000cacSChristoph Hellwig /* no data command? */ 6168f000cacSChristoph Hellwig if (!len) 6178f000cacSChristoph Hellwig return 0; 6188f000cacSChristoph Hellwig 6198f000cacSChristoph Hellwig status = nvmet_rdma_alloc_sgl(&rsp->req.sg, &rsp->req.sg_cnt, 6208f000cacSChristoph Hellwig len); 6218f000cacSChristoph Hellwig if (status) 6228f000cacSChristoph Hellwig return status; 6238f000cacSChristoph Hellwig 6248f000cacSChristoph Hellwig ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num, 6258f000cacSChristoph Hellwig rsp->req.sg, rsp->req.sg_cnt, 0, addr, key, 6268f000cacSChristoph Hellwig nvmet_data_dir(&rsp->req)); 6278f000cacSChristoph Hellwig if (ret < 0) 6288f000cacSChristoph Hellwig return NVME_SC_INTERNAL; 6298f000cacSChristoph Hellwig rsp->n_rdma += ret; 6308f000cacSChristoph Hellwig 6318f000cacSChristoph Hellwig if (invalidate) { 6328f000cacSChristoph Hellwig rsp->invalidate_rkey = key; 6338f000cacSChristoph Hellwig rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY; 6348f000cacSChristoph Hellwig } 6358f000cacSChristoph Hellwig 6368f000cacSChristoph Hellwig return 0; 6378f000cacSChristoph Hellwig } 6388f000cacSChristoph Hellwig 6398f000cacSChristoph Hellwig static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp) 6408f000cacSChristoph Hellwig { 6418f000cacSChristoph Hellwig struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl; 6428f000cacSChristoph Hellwig 6438f000cacSChristoph Hellwig switch (sgl->type >> 4) { 6448f000cacSChristoph Hellwig case NVME_SGL_FMT_DATA_DESC: 6458f000cacSChristoph Hellwig switch (sgl->type & 0xf) { 6468f000cacSChristoph Hellwig case NVME_SGL_FMT_OFFSET: 6478f000cacSChristoph Hellwig return nvmet_rdma_map_sgl_inline(rsp); 6488f000cacSChristoph Hellwig default: 6498f000cacSChristoph Hellwig pr_err("invalid SGL subtype: %#x\n", sgl->type); 6508f000cacSChristoph Hellwig return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 6518f000cacSChristoph Hellwig } 6528f000cacSChristoph Hellwig case NVME_KEY_SGL_FMT_DATA_DESC: 6538f000cacSChristoph Hellwig switch (sgl->type & 0xf) { 6548f000cacSChristoph Hellwig case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE: 6558f000cacSChristoph Hellwig return nvmet_rdma_map_sgl_keyed(rsp, sgl, true); 6568f000cacSChristoph Hellwig case NVME_SGL_FMT_ADDRESS: 6578f000cacSChristoph Hellwig return nvmet_rdma_map_sgl_keyed(rsp, sgl, false); 6588f000cacSChristoph Hellwig default: 6598f000cacSChristoph Hellwig pr_err("invalid SGL subtype: %#x\n", sgl->type); 6608f000cacSChristoph Hellwig return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 6618f000cacSChristoph Hellwig } 6628f000cacSChristoph Hellwig default: 6638f000cacSChristoph Hellwig pr_err("invalid SGL type: %#x\n", sgl->type); 6648f000cacSChristoph Hellwig return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR; 6658f000cacSChristoph Hellwig } 6668f000cacSChristoph Hellwig } 6678f000cacSChristoph Hellwig 6688f000cacSChristoph Hellwig static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp) 6698f000cacSChristoph Hellwig { 6708f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = rsp->queue; 6718f000cacSChristoph Hellwig 6728f000cacSChristoph Hellwig if (unlikely(atomic_sub_return(1 + rsp->n_rdma, 6738f000cacSChristoph Hellwig &queue->sq_wr_avail) < 0)) { 6748f000cacSChristoph Hellwig pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n", 6758f000cacSChristoph Hellwig 1 + rsp->n_rdma, queue->idx, 6768f000cacSChristoph Hellwig queue->nvme_sq.ctrl->cntlid); 6778f000cacSChristoph Hellwig atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 6788f000cacSChristoph Hellwig return false; 6798f000cacSChristoph Hellwig } 6808f000cacSChristoph Hellwig 6818f000cacSChristoph Hellwig if (nvmet_rdma_need_data_in(rsp)) { 6828f000cacSChristoph Hellwig if (rdma_rw_ctx_post(&rsp->rw, queue->cm_id->qp, 6838f000cacSChristoph Hellwig queue->cm_id->port_num, &rsp->read_cqe, NULL)) 6848f000cacSChristoph Hellwig nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR); 6858f000cacSChristoph Hellwig } else { 6868f000cacSChristoph Hellwig rsp->req.execute(&rsp->req); 6878f000cacSChristoph Hellwig } 6888f000cacSChristoph Hellwig 6898f000cacSChristoph Hellwig return true; 6908f000cacSChristoph Hellwig } 6918f000cacSChristoph Hellwig 6928f000cacSChristoph Hellwig static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue, 6938f000cacSChristoph Hellwig struct nvmet_rdma_rsp *cmd) 6948f000cacSChristoph Hellwig { 6958f000cacSChristoph Hellwig u16 status; 6968f000cacSChristoph Hellwig 6978f000cacSChristoph Hellwig cmd->queue = queue; 6988f000cacSChristoph Hellwig cmd->n_rdma = 0; 6998f000cacSChristoph Hellwig cmd->req.port = queue->port; 7008f000cacSChristoph Hellwig 7018f000cacSChristoph Hellwig if (!nvmet_req_init(&cmd->req, &queue->nvme_cq, 7028f000cacSChristoph Hellwig &queue->nvme_sq, &nvmet_rdma_ops)) 7038f000cacSChristoph Hellwig return; 7048f000cacSChristoph Hellwig 7058f000cacSChristoph Hellwig status = nvmet_rdma_map_sgl(cmd); 7068f000cacSChristoph Hellwig if (status) 7078f000cacSChristoph Hellwig goto out_err; 7088f000cacSChristoph Hellwig 7098f000cacSChristoph Hellwig if (unlikely(!nvmet_rdma_execute_command(cmd))) { 7108f000cacSChristoph Hellwig spin_lock(&queue->rsp_wr_wait_lock); 7118f000cacSChristoph Hellwig list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list); 7128f000cacSChristoph Hellwig spin_unlock(&queue->rsp_wr_wait_lock); 7138f000cacSChristoph Hellwig } 7148f000cacSChristoph Hellwig 7158f000cacSChristoph Hellwig return; 7168f000cacSChristoph Hellwig 7178f000cacSChristoph Hellwig out_err: 7188f000cacSChristoph Hellwig nvmet_req_complete(&cmd->req, status); 7198f000cacSChristoph Hellwig } 7208f000cacSChristoph Hellwig 7218f000cacSChristoph Hellwig static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 7228f000cacSChristoph Hellwig { 7238f000cacSChristoph Hellwig struct nvmet_rdma_cmd *cmd = 7248f000cacSChristoph Hellwig container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe); 7258f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = cq->cq_context; 7268f000cacSChristoph Hellwig struct nvmet_rdma_rsp *rsp; 7278f000cacSChristoph Hellwig 7288f000cacSChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 7298f000cacSChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) { 7308f000cacSChristoph Hellwig pr_err("RECV for CQE 0x%p failed with status %s (%d)\n", 7318f000cacSChristoph Hellwig wc->wr_cqe, ib_wc_status_msg(wc->status), 7328f000cacSChristoph Hellwig wc->status); 7338f000cacSChristoph Hellwig nvmet_rdma_error_comp(queue); 7348f000cacSChristoph Hellwig } 7358f000cacSChristoph Hellwig return; 7368f000cacSChristoph Hellwig } 7378f000cacSChristoph Hellwig 7388f000cacSChristoph Hellwig if (unlikely(wc->byte_len < sizeof(struct nvme_command))) { 7398f000cacSChristoph Hellwig pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n"); 7408f000cacSChristoph Hellwig nvmet_rdma_error_comp(queue); 7418f000cacSChristoph Hellwig return; 7428f000cacSChristoph Hellwig } 7438f000cacSChristoph Hellwig 7448f000cacSChristoph Hellwig cmd->queue = queue; 7458f000cacSChristoph Hellwig rsp = nvmet_rdma_get_rsp(queue); 7468f000cacSChristoph Hellwig rsp->cmd = cmd; 7478f000cacSChristoph Hellwig rsp->flags = 0; 7488f000cacSChristoph Hellwig rsp->req.cmd = cmd->nvme_cmd; 7498f000cacSChristoph Hellwig 7508f000cacSChristoph Hellwig if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) { 7518f000cacSChristoph Hellwig unsigned long flags; 7528f000cacSChristoph Hellwig 7538f000cacSChristoph Hellwig spin_lock_irqsave(&queue->state_lock, flags); 7548f000cacSChristoph Hellwig if (queue->state == NVMET_RDMA_Q_CONNECTING) 7558f000cacSChristoph Hellwig list_add_tail(&rsp->wait_list, &queue->rsp_wait_list); 7568f000cacSChristoph Hellwig else 7578f000cacSChristoph Hellwig nvmet_rdma_put_rsp(rsp); 7588f000cacSChristoph Hellwig spin_unlock_irqrestore(&queue->state_lock, flags); 7598f000cacSChristoph Hellwig return; 7608f000cacSChristoph Hellwig } 7618f000cacSChristoph Hellwig 7628f000cacSChristoph Hellwig nvmet_rdma_handle_command(queue, rsp); 7638f000cacSChristoph Hellwig } 7648f000cacSChristoph Hellwig 7658f000cacSChristoph Hellwig static void nvmet_rdma_destroy_srq(struct nvmet_rdma_device *ndev) 7668f000cacSChristoph Hellwig { 7678f000cacSChristoph Hellwig if (!ndev->srq) 7688f000cacSChristoph Hellwig return; 7698f000cacSChristoph Hellwig 7708f000cacSChristoph Hellwig nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false); 7718f000cacSChristoph Hellwig ib_destroy_srq(ndev->srq); 7728f000cacSChristoph Hellwig } 7738f000cacSChristoph Hellwig 7748f000cacSChristoph Hellwig static int nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev) 7758f000cacSChristoph Hellwig { 7768f000cacSChristoph Hellwig struct ib_srq_init_attr srq_attr = { NULL, }; 7778f000cacSChristoph Hellwig struct ib_srq *srq; 7788f000cacSChristoph Hellwig size_t srq_size; 7798f000cacSChristoph Hellwig int ret, i; 7808f000cacSChristoph Hellwig 7818f000cacSChristoph Hellwig srq_size = 4095; /* XXX: tune */ 7828f000cacSChristoph Hellwig 7838f000cacSChristoph Hellwig srq_attr.attr.max_wr = srq_size; 7848f000cacSChristoph Hellwig srq_attr.attr.max_sge = 2; 7858f000cacSChristoph Hellwig srq_attr.attr.srq_limit = 0; 7868f000cacSChristoph Hellwig srq_attr.srq_type = IB_SRQT_BASIC; 7878f000cacSChristoph Hellwig srq = ib_create_srq(ndev->pd, &srq_attr); 7888f000cacSChristoph Hellwig if (IS_ERR(srq)) { 7898f000cacSChristoph Hellwig /* 7908f000cacSChristoph Hellwig * If SRQs aren't supported we just go ahead and use normal 7918f000cacSChristoph Hellwig * non-shared receive queues. 7928f000cacSChristoph Hellwig */ 7938f000cacSChristoph Hellwig pr_info("SRQ requested but not supported.\n"); 7948f000cacSChristoph Hellwig return 0; 7958f000cacSChristoph Hellwig } 7968f000cacSChristoph Hellwig 7978f000cacSChristoph Hellwig ndev->srq_cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false); 7988f000cacSChristoph Hellwig if (IS_ERR(ndev->srq_cmds)) { 7998f000cacSChristoph Hellwig ret = PTR_ERR(ndev->srq_cmds); 8008f000cacSChristoph Hellwig goto out_destroy_srq; 8018f000cacSChristoph Hellwig } 8028f000cacSChristoph Hellwig 8038f000cacSChristoph Hellwig ndev->srq = srq; 8048f000cacSChristoph Hellwig ndev->srq_size = srq_size; 8058f000cacSChristoph Hellwig 8068f000cacSChristoph Hellwig for (i = 0; i < srq_size; i++) 8078f000cacSChristoph Hellwig nvmet_rdma_post_recv(ndev, &ndev->srq_cmds[i]); 8088f000cacSChristoph Hellwig 8098f000cacSChristoph Hellwig return 0; 8108f000cacSChristoph Hellwig 8118f000cacSChristoph Hellwig out_destroy_srq: 8128f000cacSChristoph Hellwig ib_destroy_srq(srq); 8138f000cacSChristoph Hellwig return ret; 8148f000cacSChristoph Hellwig } 8158f000cacSChristoph Hellwig 8168f000cacSChristoph Hellwig static void nvmet_rdma_free_dev(struct kref *ref) 8178f000cacSChristoph Hellwig { 8188f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev = 8198f000cacSChristoph Hellwig container_of(ref, struct nvmet_rdma_device, ref); 8208f000cacSChristoph Hellwig 8218f000cacSChristoph Hellwig mutex_lock(&device_list_mutex); 8228f000cacSChristoph Hellwig list_del(&ndev->entry); 8238f000cacSChristoph Hellwig mutex_unlock(&device_list_mutex); 8248f000cacSChristoph Hellwig 8258f000cacSChristoph Hellwig nvmet_rdma_destroy_srq(ndev); 8268f000cacSChristoph Hellwig ib_dealloc_pd(ndev->pd); 8278f000cacSChristoph Hellwig 8288f000cacSChristoph Hellwig kfree(ndev); 8298f000cacSChristoph Hellwig } 8308f000cacSChristoph Hellwig 8318f000cacSChristoph Hellwig static struct nvmet_rdma_device * 8328f000cacSChristoph Hellwig nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id) 8338f000cacSChristoph Hellwig { 8348f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev; 8358f000cacSChristoph Hellwig int ret; 8368f000cacSChristoph Hellwig 8378f000cacSChristoph Hellwig mutex_lock(&device_list_mutex); 8388f000cacSChristoph Hellwig list_for_each_entry(ndev, &device_list, entry) { 8398f000cacSChristoph Hellwig if (ndev->device->node_guid == cm_id->device->node_guid && 8408f000cacSChristoph Hellwig kref_get_unless_zero(&ndev->ref)) 8418f000cacSChristoph Hellwig goto out_unlock; 8428f000cacSChristoph Hellwig } 8438f000cacSChristoph Hellwig 8448f000cacSChristoph Hellwig ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); 8458f000cacSChristoph Hellwig if (!ndev) 8468f000cacSChristoph Hellwig goto out_err; 8478f000cacSChristoph Hellwig 8488f000cacSChristoph Hellwig ndev->device = cm_id->device; 8498f000cacSChristoph Hellwig kref_init(&ndev->ref); 8508f000cacSChristoph Hellwig 851ed082d36SChristoph Hellwig ndev->pd = ib_alloc_pd(ndev->device, 0); 8528f000cacSChristoph Hellwig if (IS_ERR(ndev->pd)) 8538f000cacSChristoph Hellwig goto out_free_dev; 8548f000cacSChristoph Hellwig 8558f000cacSChristoph Hellwig if (nvmet_rdma_use_srq) { 8568f000cacSChristoph Hellwig ret = nvmet_rdma_init_srq(ndev); 8578f000cacSChristoph Hellwig if (ret) 8588f000cacSChristoph Hellwig goto out_free_pd; 8598f000cacSChristoph Hellwig } 8608f000cacSChristoph Hellwig 8618f000cacSChristoph Hellwig list_add(&ndev->entry, &device_list); 8628f000cacSChristoph Hellwig out_unlock: 8638f000cacSChristoph Hellwig mutex_unlock(&device_list_mutex); 8648f000cacSChristoph Hellwig pr_debug("added %s.\n", ndev->device->name); 8658f000cacSChristoph Hellwig return ndev; 8668f000cacSChristoph Hellwig 8678f000cacSChristoph Hellwig out_free_pd: 8688f000cacSChristoph Hellwig ib_dealloc_pd(ndev->pd); 8698f000cacSChristoph Hellwig out_free_dev: 8708f000cacSChristoph Hellwig kfree(ndev); 8718f000cacSChristoph Hellwig out_err: 8728f000cacSChristoph Hellwig mutex_unlock(&device_list_mutex); 8738f000cacSChristoph Hellwig return NULL; 8748f000cacSChristoph Hellwig } 8758f000cacSChristoph Hellwig 8768f000cacSChristoph Hellwig static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue) 8778f000cacSChristoph Hellwig { 8788f000cacSChristoph Hellwig struct ib_qp_init_attr qp_attr; 8798f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev = queue->dev; 8808f000cacSChristoph Hellwig int comp_vector, nr_cqe, ret, i; 8818f000cacSChristoph Hellwig 8828f000cacSChristoph Hellwig /* 8838f000cacSChristoph Hellwig * Spread the io queues across completion vectors, 8848f000cacSChristoph Hellwig * but still keep all admin queues on vector 0. 8858f000cacSChristoph Hellwig */ 8868f000cacSChristoph Hellwig comp_vector = !queue->host_qid ? 0 : 8878f000cacSChristoph Hellwig queue->idx % ndev->device->num_comp_vectors; 8888f000cacSChristoph Hellwig 8898f000cacSChristoph Hellwig /* 8908f000cacSChristoph Hellwig * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND. 8918f000cacSChristoph Hellwig */ 8928f000cacSChristoph Hellwig nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size; 8938f000cacSChristoph Hellwig 8948f000cacSChristoph Hellwig queue->cq = ib_alloc_cq(ndev->device, queue, 8958f000cacSChristoph Hellwig nr_cqe + 1, comp_vector, 8968f000cacSChristoph Hellwig IB_POLL_WORKQUEUE); 8978f000cacSChristoph Hellwig if (IS_ERR(queue->cq)) { 8988f000cacSChristoph Hellwig ret = PTR_ERR(queue->cq); 8998f000cacSChristoph Hellwig pr_err("failed to create CQ cqe= %d ret= %d\n", 9008f000cacSChristoph Hellwig nr_cqe + 1, ret); 9018f000cacSChristoph Hellwig goto out; 9028f000cacSChristoph Hellwig } 9038f000cacSChristoph Hellwig 9048f000cacSChristoph Hellwig memset(&qp_attr, 0, sizeof(qp_attr)); 9058f000cacSChristoph Hellwig qp_attr.qp_context = queue; 9068f000cacSChristoph Hellwig qp_attr.event_handler = nvmet_rdma_qp_event; 9078f000cacSChristoph Hellwig qp_attr.send_cq = queue->cq; 9088f000cacSChristoph Hellwig qp_attr.recv_cq = queue->cq; 9098f000cacSChristoph Hellwig qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 9108f000cacSChristoph Hellwig qp_attr.qp_type = IB_QPT_RC; 9118f000cacSChristoph Hellwig /* +1 for drain */ 9128f000cacSChristoph Hellwig qp_attr.cap.max_send_wr = queue->send_queue_size + 1; 9138f000cacSChristoph Hellwig qp_attr.cap.max_rdma_ctxs = queue->send_queue_size; 9148f000cacSChristoph Hellwig qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd, 9158f000cacSChristoph Hellwig ndev->device->attrs.max_sge); 9168f000cacSChristoph Hellwig 9178f000cacSChristoph Hellwig if (ndev->srq) { 9188f000cacSChristoph Hellwig qp_attr.srq = ndev->srq; 9198f000cacSChristoph Hellwig } else { 9208f000cacSChristoph Hellwig /* +1 for drain */ 9218f000cacSChristoph Hellwig qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size; 9228f000cacSChristoph Hellwig qp_attr.cap.max_recv_sge = 2; 9238f000cacSChristoph Hellwig } 9248f000cacSChristoph Hellwig 9258f000cacSChristoph Hellwig ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr); 9268f000cacSChristoph Hellwig if (ret) { 9278f000cacSChristoph Hellwig pr_err("failed to create_qp ret= %d\n", ret); 9288f000cacSChristoph Hellwig goto err_destroy_cq; 9298f000cacSChristoph Hellwig } 9308f000cacSChristoph Hellwig 9318f000cacSChristoph Hellwig atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr); 9328f000cacSChristoph Hellwig 9338f000cacSChristoph Hellwig pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n", 9348f000cacSChristoph Hellwig __func__, queue->cq->cqe, qp_attr.cap.max_send_sge, 9358f000cacSChristoph Hellwig qp_attr.cap.max_send_wr, queue->cm_id); 9368f000cacSChristoph Hellwig 9378f000cacSChristoph Hellwig if (!ndev->srq) { 9388f000cacSChristoph Hellwig for (i = 0; i < queue->recv_queue_size; i++) { 9398f000cacSChristoph Hellwig queue->cmds[i].queue = queue; 9408f000cacSChristoph Hellwig nvmet_rdma_post_recv(ndev, &queue->cmds[i]); 9418f000cacSChristoph Hellwig } 9428f000cacSChristoph Hellwig } 9438f000cacSChristoph Hellwig 9448f000cacSChristoph Hellwig out: 9458f000cacSChristoph Hellwig return ret; 9468f000cacSChristoph Hellwig 9478f000cacSChristoph Hellwig err_destroy_cq: 9488f000cacSChristoph Hellwig ib_free_cq(queue->cq); 9498f000cacSChristoph Hellwig goto out; 9508f000cacSChristoph Hellwig } 9518f000cacSChristoph Hellwig 9528f000cacSChristoph Hellwig static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue) 9538f000cacSChristoph Hellwig { 9548f000cacSChristoph Hellwig rdma_destroy_qp(queue->cm_id); 9558f000cacSChristoph Hellwig ib_free_cq(queue->cq); 9568f000cacSChristoph Hellwig } 9578f000cacSChristoph Hellwig 9588f000cacSChristoph Hellwig static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue) 9598f000cacSChristoph Hellwig { 9608f000cacSChristoph Hellwig pr_info("freeing queue %d\n", queue->idx); 9618f000cacSChristoph Hellwig 9628f000cacSChristoph Hellwig nvmet_sq_destroy(&queue->nvme_sq); 9638f000cacSChristoph Hellwig 9648f000cacSChristoph Hellwig nvmet_rdma_destroy_queue_ib(queue); 9658f000cacSChristoph Hellwig if (!queue->dev->srq) { 9668f000cacSChristoph Hellwig nvmet_rdma_free_cmds(queue->dev, queue->cmds, 9678f000cacSChristoph Hellwig queue->recv_queue_size, 9688f000cacSChristoph Hellwig !queue->host_qid); 9698f000cacSChristoph Hellwig } 9708f000cacSChristoph Hellwig nvmet_rdma_free_rsps(queue); 9718f000cacSChristoph Hellwig ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx); 9728f000cacSChristoph Hellwig kfree(queue); 9738f000cacSChristoph Hellwig } 9748f000cacSChristoph Hellwig 9758f000cacSChristoph Hellwig static void nvmet_rdma_release_queue_work(struct work_struct *w) 9768f000cacSChristoph Hellwig { 9778f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = 9788f000cacSChristoph Hellwig container_of(w, struct nvmet_rdma_queue, release_work); 9798f000cacSChristoph Hellwig struct rdma_cm_id *cm_id = queue->cm_id; 9808f000cacSChristoph Hellwig struct nvmet_rdma_device *dev = queue->dev; 9813256aaefSVincent Stehlé enum nvmet_rdma_queue_state state = queue->state; 9828f000cacSChristoph Hellwig 9838f000cacSChristoph Hellwig nvmet_rdma_free_queue(queue); 984d8f7750aSSagi Grimberg 9853256aaefSVincent Stehlé if (state != NVMET_RDMA_IN_DEVICE_REMOVAL) 9868f000cacSChristoph Hellwig rdma_destroy_id(cm_id); 987d8f7750aSSagi Grimberg 9888f000cacSChristoph Hellwig kref_put(&dev->ref, nvmet_rdma_free_dev); 9898f000cacSChristoph Hellwig } 9908f000cacSChristoph Hellwig 9918f000cacSChristoph Hellwig static int 9928f000cacSChristoph Hellwig nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn, 9938f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue) 9948f000cacSChristoph Hellwig { 9958f000cacSChristoph Hellwig struct nvme_rdma_cm_req *req; 9968f000cacSChristoph Hellwig 9978f000cacSChristoph Hellwig req = (struct nvme_rdma_cm_req *)conn->private_data; 9988f000cacSChristoph Hellwig if (!req || conn->private_data_len == 0) 9998f000cacSChristoph Hellwig return NVME_RDMA_CM_INVALID_LEN; 10008f000cacSChristoph Hellwig 10018f000cacSChristoph Hellwig if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0) 10028f000cacSChristoph Hellwig return NVME_RDMA_CM_INVALID_RECFMT; 10038f000cacSChristoph Hellwig 10048f000cacSChristoph Hellwig queue->host_qid = le16_to_cpu(req->qid); 10058f000cacSChristoph Hellwig 10068f000cacSChristoph Hellwig /* 1007b825b44cSJay Freyensee * req->hsqsize corresponds to our recv queue size plus 1 10088f000cacSChristoph Hellwig * req->hrqsize corresponds to our send queue size 10098f000cacSChristoph Hellwig */ 1010b825b44cSJay Freyensee queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1; 10118f000cacSChristoph Hellwig queue->send_queue_size = le16_to_cpu(req->hrqsize); 10128f000cacSChristoph Hellwig 10138f000cacSChristoph Hellwig if (!queue->host_qid && queue->recv_queue_size > NVMF_AQ_DEPTH) 10148f000cacSChristoph Hellwig return NVME_RDMA_CM_INVALID_HSQSIZE; 10158f000cacSChristoph Hellwig 10168f000cacSChristoph Hellwig /* XXX: Should we enforce some kind of max for IO queues? */ 10178f000cacSChristoph Hellwig 10188f000cacSChristoph Hellwig return 0; 10198f000cacSChristoph Hellwig } 10208f000cacSChristoph Hellwig 10218f000cacSChristoph Hellwig static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id, 10228f000cacSChristoph Hellwig enum nvme_rdma_cm_status status) 10238f000cacSChristoph Hellwig { 10248f000cacSChristoph Hellwig struct nvme_rdma_cm_rej rej; 10258f000cacSChristoph Hellwig 10268f000cacSChristoph Hellwig rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 10278f000cacSChristoph Hellwig rej.sts = cpu_to_le16(status); 10288f000cacSChristoph Hellwig 10298f000cacSChristoph Hellwig return rdma_reject(cm_id, (void *)&rej, sizeof(rej)); 10308f000cacSChristoph Hellwig } 10318f000cacSChristoph Hellwig 10328f000cacSChristoph Hellwig static struct nvmet_rdma_queue * 10338f000cacSChristoph Hellwig nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev, 10348f000cacSChristoph Hellwig struct rdma_cm_id *cm_id, 10358f000cacSChristoph Hellwig struct rdma_cm_event *event) 10368f000cacSChristoph Hellwig { 10378f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 10388f000cacSChristoph Hellwig int ret; 10398f000cacSChristoph Hellwig 10408f000cacSChristoph Hellwig queue = kzalloc(sizeof(*queue), GFP_KERNEL); 10418f000cacSChristoph Hellwig if (!queue) { 10428f000cacSChristoph Hellwig ret = NVME_RDMA_CM_NO_RSC; 10438f000cacSChristoph Hellwig goto out_reject; 10448f000cacSChristoph Hellwig } 10458f000cacSChristoph Hellwig 10468f000cacSChristoph Hellwig ret = nvmet_sq_init(&queue->nvme_sq); 1047*70d4281cSBart Van Assche if (ret) { 1048*70d4281cSBart Van Assche ret = NVME_RDMA_CM_NO_RSC; 10498f000cacSChristoph Hellwig goto out_free_queue; 1050*70d4281cSBart Van Assche } 10518f000cacSChristoph Hellwig 10528f000cacSChristoph Hellwig ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue); 10538f000cacSChristoph Hellwig if (ret) 10548f000cacSChristoph Hellwig goto out_destroy_sq; 10558f000cacSChristoph Hellwig 10568f000cacSChristoph Hellwig /* 10578f000cacSChristoph Hellwig * Schedules the actual release because calling rdma_destroy_id from 10588f000cacSChristoph Hellwig * inside a CM callback would trigger a deadlock. (great API design..) 10598f000cacSChristoph Hellwig */ 10608f000cacSChristoph Hellwig INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work); 10618f000cacSChristoph Hellwig queue->dev = ndev; 10628f000cacSChristoph Hellwig queue->cm_id = cm_id; 10638f000cacSChristoph Hellwig 10648f000cacSChristoph Hellwig spin_lock_init(&queue->state_lock); 10658f000cacSChristoph Hellwig queue->state = NVMET_RDMA_Q_CONNECTING; 10668f000cacSChristoph Hellwig INIT_LIST_HEAD(&queue->rsp_wait_list); 10678f000cacSChristoph Hellwig INIT_LIST_HEAD(&queue->rsp_wr_wait_list); 10688f000cacSChristoph Hellwig spin_lock_init(&queue->rsp_wr_wait_lock); 10698f000cacSChristoph Hellwig INIT_LIST_HEAD(&queue->free_rsps); 10708f000cacSChristoph Hellwig spin_lock_init(&queue->rsps_lock); 10718f000cacSChristoph Hellwig 10728f000cacSChristoph Hellwig queue->idx = ida_simple_get(&nvmet_rdma_queue_ida, 0, 0, GFP_KERNEL); 10738f000cacSChristoph Hellwig if (queue->idx < 0) { 10748f000cacSChristoph Hellwig ret = NVME_RDMA_CM_NO_RSC; 10758f000cacSChristoph Hellwig goto out_free_queue; 10768f000cacSChristoph Hellwig } 10778f000cacSChristoph Hellwig 10788f000cacSChristoph Hellwig ret = nvmet_rdma_alloc_rsps(queue); 10798f000cacSChristoph Hellwig if (ret) { 10808f000cacSChristoph Hellwig ret = NVME_RDMA_CM_NO_RSC; 10818f000cacSChristoph Hellwig goto out_ida_remove; 10828f000cacSChristoph Hellwig } 10838f000cacSChristoph Hellwig 10848f000cacSChristoph Hellwig if (!ndev->srq) { 10858f000cacSChristoph Hellwig queue->cmds = nvmet_rdma_alloc_cmds(ndev, 10868f000cacSChristoph Hellwig queue->recv_queue_size, 10878f000cacSChristoph Hellwig !queue->host_qid); 10888f000cacSChristoph Hellwig if (IS_ERR(queue->cmds)) { 10898f000cacSChristoph Hellwig ret = NVME_RDMA_CM_NO_RSC; 10908f000cacSChristoph Hellwig goto out_free_responses; 10918f000cacSChristoph Hellwig } 10928f000cacSChristoph Hellwig } 10938f000cacSChristoph Hellwig 10948f000cacSChristoph Hellwig ret = nvmet_rdma_create_queue_ib(queue); 10958f000cacSChristoph Hellwig if (ret) { 10968f000cacSChristoph Hellwig pr_err("%s: creating RDMA queue failed (%d).\n", 10978f000cacSChristoph Hellwig __func__, ret); 10988f000cacSChristoph Hellwig ret = NVME_RDMA_CM_NO_RSC; 10998f000cacSChristoph Hellwig goto out_free_cmds; 11008f000cacSChristoph Hellwig } 11018f000cacSChristoph Hellwig 11028f000cacSChristoph Hellwig return queue; 11038f000cacSChristoph Hellwig 11048f000cacSChristoph Hellwig out_free_cmds: 11058f000cacSChristoph Hellwig if (!ndev->srq) { 11068f000cacSChristoph Hellwig nvmet_rdma_free_cmds(queue->dev, queue->cmds, 11078f000cacSChristoph Hellwig queue->recv_queue_size, 11088f000cacSChristoph Hellwig !queue->host_qid); 11098f000cacSChristoph Hellwig } 11108f000cacSChristoph Hellwig out_free_responses: 11118f000cacSChristoph Hellwig nvmet_rdma_free_rsps(queue); 11128f000cacSChristoph Hellwig out_ida_remove: 11138f000cacSChristoph Hellwig ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx); 11148f000cacSChristoph Hellwig out_destroy_sq: 11158f000cacSChristoph Hellwig nvmet_sq_destroy(&queue->nvme_sq); 11168f000cacSChristoph Hellwig out_free_queue: 11178f000cacSChristoph Hellwig kfree(queue); 11188f000cacSChristoph Hellwig out_reject: 1119*70d4281cSBart Van Assche pr_debug("rejecting connect request with status code %d\n", ret); 11208f000cacSChristoph Hellwig nvmet_rdma_cm_reject(cm_id, ret); 11218f000cacSChristoph Hellwig return NULL; 11228f000cacSChristoph Hellwig } 11238f000cacSChristoph Hellwig 11248f000cacSChristoph Hellwig static void nvmet_rdma_qp_event(struct ib_event *event, void *priv) 11258f000cacSChristoph Hellwig { 11268f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = priv; 11278f000cacSChristoph Hellwig 11288f000cacSChristoph Hellwig switch (event->event) { 11298f000cacSChristoph Hellwig case IB_EVENT_COMM_EST: 11308f000cacSChristoph Hellwig rdma_notify(queue->cm_id, event->event); 11318f000cacSChristoph Hellwig break; 11328f000cacSChristoph Hellwig default: 11338f000cacSChristoph Hellwig pr_err("received unrecognized IB QP event %d\n", event->event); 11348f000cacSChristoph Hellwig break; 11358f000cacSChristoph Hellwig } 11368f000cacSChristoph Hellwig } 11378f000cacSChristoph Hellwig 11388f000cacSChristoph Hellwig static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id, 11398f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue, 11408f000cacSChristoph Hellwig struct rdma_conn_param *p) 11418f000cacSChristoph Hellwig { 11428f000cacSChristoph Hellwig struct rdma_conn_param param = { }; 11438f000cacSChristoph Hellwig struct nvme_rdma_cm_rep priv = { }; 11448f000cacSChristoph Hellwig int ret = -ENOMEM; 11458f000cacSChristoph Hellwig 11468f000cacSChristoph Hellwig param.rnr_retry_count = 7; 11478f000cacSChristoph Hellwig param.flow_control = 1; 11488f000cacSChristoph Hellwig param.initiator_depth = min_t(u8, p->initiator_depth, 11498f000cacSChristoph Hellwig queue->dev->device->attrs.max_qp_init_rd_atom); 11508f000cacSChristoph Hellwig param.private_data = &priv; 11518f000cacSChristoph Hellwig param.private_data_len = sizeof(priv); 11528f000cacSChristoph Hellwig priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 11538f000cacSChristoph Hellwig priv.crqsize = cpu_to_le16(queue->recv_queue_size); 11548f000cacSChristoph Hellwig 11558f000cacSChristoph Hellwig ret = rdma_accept(cm_id, ¶m); 11568f000cacSChristoph Hellwig if (ret) 11578f000cacSChristoph Hellwig pr_err("rdma_accept failed (error code = %d)\n", ret); 11588f000cacSChristoph Hellwig 11598f000cacSChristoph Hellwig return ret; 11608f000cacSChristoph Hellwig } 11618f000cacSChristoph Hellwig 11628f000cacSChristoph Hellwig static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, 11638f000cacSChristoph Hellwig struct rdma_cm_event *event) 11648f000cacSChristoph Hellwig { 11658f000cacSChristoph Hellwig struct nvmet_rdma_device *ndev; 11668f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 11678f000cacSChristoph Hellwig int ret = -EINVAL; 11688f000cacSChristoph Hellwig 11698f000cacSChristoph Hellwig ndev = nvmet_rdma_find_get_device(cm_id); 11708f000cacSChristoph Hellwig if (!ndev) { 11718f000cacSChristoph Hellwig pr_err("no client data!\n"); 11728f000cacSChristoph Hellwig nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC); 11738f000cacSChristoph Hellwig return -ECONNREFUSED; 11748f000cacSChristoph Hellwig } 11758f000cacSChristoph Hellwig 11768f000cacSChristoph Hellwig queue = nvmet_rdma_alloc_queue(ndev, cm_id, event); 11778f000cacSChristoph Hellwig if (!queue) { 11788f000cacSChristoph Hellwig ret = -ENOMEM; 11798f000cacSChristoph Hellwig goto put_device; 11808f000cacSChristoph Hellwig } 11818f000cacSChristoph Hellwig queue->port = cm_id->context; 11828f000cacSChristoph Hellwig 11838f000cacSChristoph Hellwig ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn); 11848f000cacSChristoph Hellwig if (ret) 11858f000cacSChristoph Hellwig goto release_queue; 11868f000cacSChristoph Hellwig 11878f000cacSChristoph Hellwig mutex_lock(&nvmet_rdma_queue_mutex); 11888f000cacSChristoph Hellwig list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list); 11898f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 11908f000cacSChristoph Hellwig 11918f000cacSChristoph Hellwig return 0; 11928f000cacSChristoph Hellwig 11938f000cacSChristoph Hellwig release_queue: 11948f000cacSChristoph Hellwig nvmet_rdma_free_queue(queue); 11958f000cacSChristoph Hellwig put_device: 11968f000cacSChristoph Hellwig kref_put(&ndev->ref, nvmet_rdma_free_dev); 11978f000cacSChristoph Hellwig 11988f000cacSChristoph Hellwig return ret; 11998f000cacSChristoph Hellwig } 12008f000cacSChristoph Hellwig 12018f000cacSChristoph Hellwig static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue) 12028f000cacSChristoph Hellwig { 12038f000cacSChristoph Hellwig unsigned long flags; 12048f000cacSChristoph Hellwig 12058f000cacSChristoph Hellwig spin_lock_irqsave(&queue->state_lock, flags); 12068f000cacSChristoph Hellwig if (queue->state != NVMET_RDMA_Q_CONNECTING) { 12078f000cacSChristoph Hellwig pr_warn("trying to establish a connected queue\n"); 12088f000cacSChristoph Hellwig goto out_unlock; 12098f000cacSChristoph Hellwig } 12108f000cacSChristoph Hellwig queue->state = NVMET_RDMA_Q_LIVE; 12118f000cacSChristoph Hellwig 12128f000cacSChristoph Hellwig while (!list_empty(&queue->rsp_wait_list)) { 12138f000cacSChristoph Hellwig struct nvmet_rdma_rsp *cmd; 12148f000cacSChristoph Hellwig 12158f000cacSChristoph Hellwig cmd = list_first_entry(&queue->rsp_wait_list, 12168f000cacSChristoph Hellwig struct nvmet_rdma_rsp, wait_list); 12178f000cacSChristoph Hellwig list_del(&cmd->wait_list); 12188f000cacSChristoph Hellwig 12198f000cacSChristoph Hellwig spin_unlock_irqrestore(&queue->state_lock, flags); 12208f000cacSChristoph Hellwig nvmet_rdma_handle_command(queue, cmd); 12218f000cacSChristoph Hellwig spin_lock_irqsave(&queue->state_lock, flags); 12228f000cacSChristoph Hellwig } 12238f000cacSChristoph Hellwig 12248f000cacSChristoph Hellwig out_unlock: 12258f000cacSChristoph Hellwig spin_unlock_irqrestore(&queue->state_lock, flags); 12268f000cacSChristoph Hellwig } 12278f000cacSChristoph Hellwig 12288f000cacSChristoph Hellwig static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 12298f000cacSChristoph Hellwig { 12308f000cacSChristoph Hellwig bool disconnect = false; 12318f000cacSChristoph Hellwig unsigned long flags; 12328f000cacSChristoph Hellwig 12338f000cacSChristoph Hellwig pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state); 12348f000cacSChristoph Hellwig 12358f000cacSChristoph Hellwig spin_lock_irqsave(&queue->state_lock, flags); 12368f000cacSChristoph Hellwig switch (queue->state) { 12378f000cacSChristoph Hellwig case NVMET_RDMA_Q_CONNECTING: 12388f000cacSChristoph Hellwig case NVMET_RDMA_Q_LIVE: 12398f000cacSChristoph Hellwig queue->state = NVMET_RDMA_Q_DISCONNECTING; 1240d8f7750aSSagi Grimberg case NVMET_RDMA_IN_DEVICE_REMOVAL: 1241d8f7750aSSagi Grimberg disconnect = true; 12428f000cacSChristoph Hellwig break; 12438f000cacSChristoph Hellwig case NVMET_RDMA_Q_DISCONNECTING: 12448f000cacSChristoph Hellwig break; 12458f000cacSChristoph Hellwig } 12468f000cacSChristoph Hellwig spin_unlock_irqrestore(&queue->state_lock, flags); 12478f000cacSChristoph Hellwig 12488f000cacSChristoph Hellwig if (disconnect) { 12498f000cacSChristoph Hellwig rdma_disconnect(queue->cm_id); 12508f000cacSChristoph Hellwig ib_drain_qp(queue->cm_id->qp); 12518f000cacSChristoph Hellwig schedule_work(&queue->release_work); 12528f000cacSChristoph Hellwig } 12538f000cacSChristoph Hellwig } 12548f000cacSChristoph Hellwig 12558f000cacSChristoph Hellwig static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 12568f000cacSChristoph Hellwig { 12578f000cacSChristoph Hellwig bool disconnect = false; 12588f000cacSChristoph Hellwig 12598f000cacSChristoph Hellwig mutex_lock(&nvmet_rdma_queue_mutex); 12608f000cacSChristoph Hellwig if (!list_empty(&queue->queue_list)) { 12618f000cacSChristoph Hellwig list_del_init(&queue->queue_list); 12628f000cacSChristoph Hellwig disconnect = true; 12638f000cacSChristoph Hellwig } 12648f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 12658f000cacSChristoph Hellwig 12668f000cacSChristoph Hellwig if (disconnect) 12678f000cacSChristoph Hellwig __nvmet_rdma_queue_disconnect(queue); 12688f000cacSChristoph Hellwig } 12698f000cacSChristoph Hellwig 12708f000cacSChristoph Hellwig static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id, 12718f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue) 12728f000cacSChristoph Hellwig { 12738f000cacSChristoph Hellwig WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING); 12748f000cacSChristoph Hellwig 12758f000cacSChristoph Hellwig pr_err("failed to connect queue\n"); 12768f000cacSChristoph Hellwig schedule_work(&queue->release_work); 12778f000cacSChristoph Hellwig } 12788f000cacSChristoph Hellwig 1279d8f7750aSSagi Grimberg /** 1280d8f7750aSSagi Grimberg * nvme_rdma_device_removal() - Handle RDMA device removal 1281d8f7750aSSagi Grimberg * @queue: nvmet rdma queue (cm id qp_context) 1282d8f7750aSSagi Grimberg * @addr: nvmet address (cm_id context) 1283d8f7750aSSagi Grimberg * 1284d8f7750aSSagi Grimberg * DEVICE_REMOVAL event notifies us that the RDMA device is about 1285d8f7750aSSagi Grimberg * to unplug so we should take care of destroying our RDMA resources. 1286d8f7750aSSagi Grimberg * This event will be generated for each allocated cm_id. 1287d8f7750aSSagi Grimberg * 1288d8f7750aSSagi Grimberg * Note that this event can be generated on a normal queue cm_id 1289d8f7750aSSagi Grimberg * and/or a device bound listener cm_id (where in this case 1290d8f7750aSSagi Grimberg * queue will be null). 1291d8f7750aSSagi Grimberg * 1292d8f7750aSSagi Grimberg * we claim ownership on destroying the cm_id. For queues we move 1293d8f7750aSSagi Grimberg * the queue state to NVMET_RDMA_IN_DEVICE_REMOVAL and for port 1294d8f7750aSSagi Grimberg * we nullify the priv to prevent double cm_id destruction and destroying 1295d8f7750aSSagi Grimberg * the cm_id implicitely by returning a non-zero rc to the callout. 1296d8f7750aSSagi Grimberg */ 1297d8f7750aSSagi Grimberg static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id, 1298d8f7750aSSagi Grimberg struct nvmet_rdma_queue *queue) 1299d8f7750aSSagi Grimberg { 1300d8f7750aSSagi Grimberg unsigned long flags; 1301d8f7750aSSagi Grimberg 1302d8f7750aSSagi Grimberg if (!queue) { 1303d8f7750aSSagi Grimberg struct nvmet_port *port = cm_id->context; 1304d8f7750aSSagi Grimberg 1305d8f7750aSSagi Grimberg /* 1306d8f7750aSSagi Grimberg * This is a listener cm_id. Make sure that 1307d8f7750aSSagi Grimberg * future remove_port won't invoke a double 1308d8f7750aSSagi Grimberg * cm_id destroy. use atomic xchg to make sure 1309d8f7750aSSagi Grimberg * we don't compete with remove_port. 1310d8f7750aSSagi Grimberg */ 1311d8f7750aSSagi Grimberg if (xchg(&port->priv, NULL) != cm_id) 1312d8f7750aSSagi Grimberg return 0; 1313d8f7750aSSagi Grimberg } else { 1314d8f7750aSSagi Grimberg /* 1315d8f7750aSSagi Grimberg * This is a queue cm_id. Make sure that 1316d8f7750aSSagi Grimberg * release queue will not destroy the cm_id 1317d8f7750aSSagi Grimberg * and schedule all ctrl queues removal (only 1318d8f7750aSSagi Grimberg * if the queue is not disconnecting already). 1319d8f7750aSSagi Grimberg */ 1320d8f7750aSSagi Grimberg spin_lock_irqsave(&queue->state_lock, flags); 1321d8f7750aSSagi Grimberg if (queue->state != NVMET_RDMA_Q_DISCONNECTING) 1322d8f7750aSSagi Grimberg queue->state = NVMET_RDMA_IN_DEVICE_REMOVAL; 1323d8f7750aSSagi Grimberg spin_unlock_irqrestore(&queue->state_lock, flags); 1324d8f7750aSSagi Grimberg nvmet_rdma_queue_disconnect(queue); 1325d8f7750aSSagi Grimberg flush_scheduled_work(); 1326d8f7750aSSagi Grimberg } 1327d8f7750aSSagi Grimberg 1328d8f7750aSSagi Grimberg /* 1329d8f7750aSSagi Grimberg * We need to return 1 so that the core will destroy 1330d8f7750aSSagi Grimberg * it's own ID. What a great API design.. 1331d8f7750aSSagi Grimberg */ 1332d8f7750aSSagi Grimberg return 1; 1333d8f7750aSSagi Grimberg } 1334d8f7750aSSagi Grimberg 13358f000cacSChristoph Hellwig static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id, 13368f000cacSChristoph Hellwig struct rdma_cm_event *event) 13378f000cacSChristoph Hellwig { 13388f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue = NULL; 13398f000cacSChristoph Hellwig int ret = 0; 13408f000cacSChristoph Hellwig 13418f000cacSChristoph Hellwig if (cm_id->qp) 13428f000cacSChristoph Hellwig queue = cm_id->qp->qp_context; 13438f000cacSChristoph Hellwig 13448f000cacSChristoph Hellwig pr_debug("%s (%d): status %d id %p\n", 13458f000cacSChristoph Hellwig rdma_event_msg(event->event), event->event, 13468f000cacSChristoph Hellwig event->status, cm_id); 13478f000cacSChristoph Hellwig 13488f000cacSChristoph Hellwig switch (event->event) { 13498f000cacSChristoph Hellwig case RDMA_CM_EVENT_CONNECT_REQUEST: 13508f000cacSChristoph Hellwig ret = nvmet_rdma_queue_connect(cm_id, event); 13518f000cacSChristoph Hellwig break; 13528f000cacSChristoph Hellwig case RDMA_CM_EVENT_ESTABLISHED: 13538f000cacSChristoph Hellwig nvmet_rdma_queue_established(queue); 13548f000cacSChristoph Hellwig break; 13558f000cacSChristoph Hellwig case RDMA_CM_EVENT_ADDR_CHANGE: 13568f000cacSChristoph Hellwig case RDMA_CM_EVENT_DISCONNECTED: 13578f000cacSChristoph Hellwig case RDMA_CM_EVENT_TIMEWAIT_EXIT: 13588f000cacSChristoph Hellwig nvmet_rdma_queue_disconnect(queue); 1359d8f7750aSSagi Grimberg break; 1360d8f7750aSSagi Grimberg case RDMA_CM_EVENT_DEVICE_REMOVAL: 1361d8f7750aSSagi Grimberg ret = nvmet_rdma_device_removal(cm_id, queue); 13628f000cacSChristoph Hellwig break; 13638f000cacSChristoph Hellwig case RDMA_CM_EVENT_REJECTED: 13648f000cacSChristoph Hellwig case RDMA_CM_EVENT_UNREACHABLE: 13658f000cacSChristoph Hellwig case RDMA_CM_EVENT_CONNECT_ERROR: 13668f000cacSChristoph Hellwig nvmet_rdma_queue_connect_fail(cm_id, queue); 13678f000cacSChristoph Hellwig break; 13688f000cacSChristoph Hellwig default: 13698f000cacSChristoph Hellwig pr_err("received unrecognized RDMA CM event %d\n", 13708f000cacSChristoph Hellwig event->event); 13718f000cacSChristoph Hellwig break; 13728f000cacSChristoph Hellwig } 13738f000cacSChristoph Hellwig 13748f000cacSChristoph Hellwig return ret; 13758f000cacSChristoph Hellwig } 13768f000cacSChristoph Hellwig 13778f000cacSChristoph Hellwig static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl) 13788f000cacSChristoph Hellwig { 13798f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 13808f000cacSChristoph Hellwig 13818f000cacSChristoph Hellwig restart: 13828f000cacSChristoph Hellwig mutex_lock(&nvmet_rdma_queue_mutex); 13838f000cacSChristoph Hellwig list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) { 13848f000cacSChristoph Hellwig if (queue->nvme_sq.ctrl == ctrl) { 13858f000cacSChristoph Hellwig list_del_init(&queue->queue_list); 13868f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 13878f000cacSChristoph Hellwig 13888f000cacSChristoph Hellwig __nvmet_rdma_queue_disconnect(queue); 13898f000cacSChristoph Hellwig goto restart; 13908f000cacSChristoph Hellwig } 13918f000cacSChristoph Hellwig } 13928f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 13938f000cacSChristoph Hellwig } 13948f000cacSChristoph Hellwig 13958f000cacSChristoph Hellwig static int nvmet_rdma_add_port(struct nvmet_port *port) 13968f000cacSChristoph Hellwig { 13978f000cacSChristoph Hellwig struct rdma_cm_id *cm_id; 13988f000cacSChristoph Hellwig struct sockaddr_in addr_in; 13998f000cacSChristoph Hellwig u16 port_in; 14008f000cacSChristoph Hellwig int ret; 14018f000cacSChristoph Hellwig 14028f000cacSChristoph Hellwig switch (port->disc_addr.adrfam) { 14038f000cacSChristoph Hellwig case NVMF_ADDR_FAMILY_IP4: 14048f000cacSChristoph Hellwig break; 14058f000cacSChristoph Hellwig default: 14068f000cacSChristoph Hellwig pr_err("address family %d not supported\n", 14078f000cacSChristoph Hellwig port->disc_addr.adrfam); 14088f000cacSChristoph Hellwig return -EINVAL; 14098f000cacSChristoph Hellwig } 14108f000cacSChristoph Hellwig 14118f000cacSChristoph Hellwig ret = kstrtou16(port->disc_addr.trsvcid, 0, &port_in); 14128f000cacSChristoph Hellwig if (ret) 14138f000cacSChristoph Hellwig return ret; 14148f000cacSChristoph Hellwig 14158f000cacSChristoph Hellwig addr_in.sin_family = AF_INET; 14168f000cacSChristoph Hellwig addr_in.sin_addr.s_addr = in_aton(port->disc_addr.traddr); 14178f000cacSChristoph Hellwig addr_in.sin_port = htons(port_in); 14188f000cacSChristoph Hellwig 14198f000cacSChristoph Hellwig cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port, 14208f000cacSChristoph Hellwig RDMA_PS_TCP, IB_QPT_RC); 14218f000cacSChristoph Hellwig if (IS_ERR(cm_id)) { 14228f000cacSChristoph Hellwig pr_err("CM ID creation failed\n"); 14238f000cacSChristoph Hellwig return PTR_ERR(cm_id); 14248f000cacSChristoph Hellwig } 14258f000cacSChristoph Hellwig 14268f000cacSChristoph Hellwig ret = rdma_bind_addr(cm_id, (struct sockaddr *)&addr_in); 14278f000cacSChristoph Hellwig if (ret) { 14288f000cacSChristoph Hellwig pr_err("binding CM ID to %pISpc failed (%d)\n", &addr_in, ret); 14298f000cacSChristoph Hellwig goto out_destroy_id; 14308f000cacSChristoph Hellwig } 14318f000cacSChristoph Hellwig 14328f000cacSChristoph Hellwig ret = rdma_listen(cm_id, 128); 14338f000cacSChristoph Hellwig if (ret) { 14348f000cacSChristoph Hellwig pr_err("listening to %pISpc failed (%d)\n", &addr_in, ret); 14358f000cacSChristoph Hellwig goto out_destroy_id; 14368f000cacSChristoph Hellwig } 14378f000cacSChristoph Hellwig 14388f000cacSChristoph Hellwig pr_info("enabling port %d (%pISpc)\n", 14398f000cacSChristoph Hellwig le16_to_cpu(port->disc_addr.portid), &addr_in); 14408f000cacSChristoph Hellwig port->priv = cm_id; 14418f000cacSChristoph Hellwig return 0; 14428f000cacSChristoph Hellwig 14438f000cacSChristoph Hellwig out_destroy_id: 14448f000cacSChristoph Hellwig rdma_destroy_id(cm_id); 14458f000cacSChristoph Hellwig return ret; 14468f000cacSChristoph Hellwig } 14478f000cacSChristoph Hellwig 14488f000cacSChristoph Hellwig static void nvmet_rdma_remove_port(struct nvmet_port *port) 14498f000cacSChristoph Hellwig { 1450d8f7750aSSagi Grimberg struct rdma_cm_id *cm_id = xchg(&port->priv, NULL); 14518f000cacSChristoph Hellwig 1452d8f7750aSSagi Grimberg if (cm_id) 14538f000cacSChristoph Hellwig rdma_destroy_id(cm_id); 14548f000cacSChristoph Hellwig } 14558f000cacSChristoph Hellwig 14568f000cacSChristoph Hellwig static struct nvmet_fabrics_ops nvmet_rdma_ops = { 14578f000cacSChristoph Hellwig .owner = THIS_MODULE, 14588f000cacSChristoph Hellwig .type = NVMF_TRTYPE_RDMA, 14598f000cacSChristoph Hellwig .sqe_inline_size = NVMET_RDMA_INLINE_DATA_SIZE, 14608f000cacSChristoph Hellwig .msdbd = 1, 14618f000cacSChristoph Hellwig .has_keyed_sgls = 1, 14628f000cacSChristoph Hellwig .add_port = nvmet_rdma_add_port, 14638f000cacSChristoph Hellwig .remove_port = nvmet_rdma_remove_port, 14648f000cacSChristoph Hellwig .queue_response = nvmet_rdma_queue_response, 14658f000cacSChristoph Hellwig .delete_ctrl = nvmet_rdma_delete_ctrl, 14668f000cacSChristoph Hellwig }; 14678f000cacSChristoph Hellwig 14688f000cacSChristoph Hellwig static int __init nvmet_rdma_init(void) 14698f000cacSChristoph Hellwig { 14708f000cacSChristoph Hellwig return nvmet_register_transport(&nvmet_rdma_ops); 14718f000cacSChristoph Hellwig } 14728f000cacSChristoph Hellwig 14738f000cacSChristoph Hellwig static void __exit nvmet_rdma_exit(void) 14748f000cacSChristoph Hellwig { 14758f000cacSChristoph Hellwig struct nvmet_rdma_queue *queue; 14768f000cacSChristoph Hellwig 14778f000cacSChristoph Hellwig nvmet_unregister_transport(&nvmet_rdma_ops); 14788f000cacSChristoph Hellwig 14798f000cacSChristoph Hellwig flush_scheduled_work(); 14808f000cacSChristoph Hellwig 14818f000cacSChristoph Hellwig mutex_lock(&nvmet_rdma_queue_mutex); 14828f000cacSChristoph Hellwig while ((queue = list_first_entry_or_null(&nvmet_rdma_queue_list, 14838f000cacSChristoph Hellwig struct nvmet_rdma_queue, queue_list))) { 14848f000cacSChristoph Hellwig list_del_init(&queue->queue_list); 14858f000cacSChristoph Hellwig 14868f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 14878f000cacSChristoph Hellwig __nvmet_rdma_queue_disconnect(queue); 14888f000cacSChristoph Hellwig mutex_lock(&nvmet_rdma_queue_mutex); 14898f000cacSChristoph Hellwig } 14908f000cacSChristoph Hellwig mutex_unlock(&nvmet_rdma_queue_mutex); 14918f000cacSChristoph Hellwig 14928f000cacSChristoph Hellwig flush_scheduled_work(); 14938f000cacSChristoph Hellwig ida_destroy(&nvmet_rdma_queue_ida); 14948f000cacSChristoph Hellwig } 14958f000cacSChristoph Hellwig 14968f000cacSChristoph Hellwig module_init(nvmet_rdma_init); 14978f000cacSChristoph Hellwig module_exit(nvmet_rdma_exit); 14988f000cacSChristoph Hellwig 14998f000cacSChristoph Hellwig MODULE_LICENSE("GPL v2"); 15008f000cacSChristoph Hellwig MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */ 1501