Christoph Hellwig | 5d8762d | 2019-02-18 09:34:21 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2 | /* |
| 3 | * NVMe over Fabrics RDMA host code. |
| 4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 10 | #include <rdma/mr_pool.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/string.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 13 | #include <linux/atomic.h> |
| 14 | #include <linux/blk-mq.h> |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 15 | #include <linux/blk-mq-rdma.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/scatterlist.h> |
| 20 | #include <linux/nvme.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
| 22 | |
| 23 | #include <rdma/ib_verbs.h> |
| 24 | #include <rdma/rdma_cm.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 25 | #include <linux/nvme-rdma.h> |
| 26 | |
| 27 | #include "nvme.h" |
| 28 | #include "fabrics.h" |
| 29 | |
| 30 | |
Sagi Grimberg | 782d820 | 2017-03-21 16:32:38 +0200 | [diff] [blame] | 31 | #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 32 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 33 | #define NVME_RDMA_MAX_SEGMENTS 256 |
| 34 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 35 | #define NVME_RDMA_MAX_INLINE_SEGMENTS 4 |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 36 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 37 | #define NVME_RDMA_DATA_SGL_SIZE \ |
| 38 | (sizeof(struct scatterlist) * NVME_INLINE_SG_CNT) |
| 39 | #define NVME_RDMA_METADATA_SGL_SIZE \ |
| 40 | (sizeof(struct scatterlist) * NVME_INLINE_METADATA_SG_CNT) |
| 41 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 42 | struct nvme_rdma_device { |
Max Gurtovoy | f87c89a | 2017-10-23 12:59:27 +0300 | [diff] [blame] | 43 | struct ib_device *dev; |
| 44 | struct ib_pd *pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 45 | struct kref ref; |
| 46 | struct list_head entry; |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 47 | unsigned int num_inline_segments; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct nvme_rdma_qe { |
| 51 | struct ib_cqe cqe; |
| 52 | void *data; |
| 53 | u64 dma; |
| 54 | }; |
| 55 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 56 | struct nvme_rdma_sgl { |
| 57 | int nents; |
| 58 | struct sg_table sg_table; |
| 59 | }; |
| 60 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 61 | struct nvme_rdma_queue; |
| 62 | struct nvme_rdma_request { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 63 | struct nvme_request req; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 64 | struct ib_mr *mr; |
| 65 | struct nvme_rdma_qe sqe; |
Sagi Grimberg | 4af7f7f | 2017-11-23 17:35:22 +0200 | [diff] [blame] | 66 | union nvme_result result; |
| 67 | __le16 status; |
| 68 | refcount_t ref; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 69 | struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; |
| 70 | u32 num_sge; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 71 | struct ib_reg_wr reg_wr; |
| 72 | struct ib_cqe reg_cqe; |
| 73 | struct nvme_rdma_queue *queue; |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 74 | struct nvme_rdma_sgl data_sgl; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 75 | struct nvme_rdma_sgl *metadata_sgl; |
| 76 | bool use_sig_mr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | enum nvme_rdma_queue_flags { |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 80 | NVME_RDMA_Q_ALLOCATED = 0, |
| 81 | NVME_RDMA_Q_LIVE = 1, |
Max Gurtovoy | eb1bd24 | 2017-11-28 18:28:44 +0200 | [diff] [blame] | 82 | NVME_RDMA_Q_TR_READY = 2, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct nvme_rdma_queue { |
| 86 | struct nvme_rdma_qe *rsp_ring; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 87 | int queue_size; |
| 88 | size_t cmnd_capsule_len; |
| 89 | struct nvme_rdma_ctrl *ctrl; |
| 90 | struct nvme_rdma_device *device; |
| 91 | struct ib_cq *ib_cq; |
| 92 | struct ib_qp *qp; |
| 93 | |
| 94 | unsigned long flags; |
| 95 | struct rdma_cm_id *cm_id; |
| 96 | int cm_error; |
| 97 | struct completion cm_done; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 98 | bool pi_support; |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 99 | int cq_size; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct nvme_rdma_ctrl { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 103 | /* read only in the hot path */ |
| 104 | struct nvme_rdma_queue *queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 105 | |
| 106 | /* other member variables */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 107 | struct blk_mq_tag_set tag_set; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 108 | struct work_struct err_work; |
| 109 | |
| 110 | struct nvme_rdma_qe async_event_sqe; |
| 111 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 112 | struct delayed_work reconnect_work; |
| 113 | |
| 114 | struct list_head list; |
| 115 | |
| 116 | struct blk_mq_tag_set admin_tag_set; |
| 117 | struct nvme_rdma_device *device; |
| 118 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 119 | u32 max_fr_pages; |
| 120 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 121 | struct sockaddr_storage addr; |
| 122 | struct sockaddr_storage src_addr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 123 | |
| 124 | struct nvme_ctrl ctrl; |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 125 | struct mutex teardown_lock; |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 126 | bool use_inline_data; |
Sagi Grimberg | b1064d3 | 2019-01-18 16:43:24 -0800 | [diff] [blame] | 127 | u32 io_queues[HCTX_MAX_TYPES]; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) |
| 131 | { |
| 132 | return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); |
| 133 | } |
| 134 | |
| 135 | static LIST_HEAD(device_list); |
| 136 | static DEFINE_MUTEX(device_list_mutex); |
| 137 | |
| 138 | static LIST_HEAD(nvme_rdma_ctrl_list); |
| 139 | static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); |
| 140 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 141 | /* |
| 142 | * Disabling this option makes small I/O goes faster, but is fundamentally |
| 143 | * unsafe. With it turned off we will have to register a global rkey that |
| 144 | * allows read and write access to all physical memory. |
| 145 | */ |
| 146 | static bool register_always = true; |
| 147 | module_param(register_always, bool, 0444); |
| 148 | MODULE_PARM_DESC(register_always, |
| 149 | "Use memory registration even for contiguous memory regions"); |
| 150 | |
| 151 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 152 | struct rdma_cm_event *event); |
| 153 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); |
Christoph Hellwig | ff02945 | 2020-06-11 08:44:52 +0200 | [diff] [blame] | 154 | static void nvme_rdma_complete_rq(struct request *rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 155 | |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 156 | static const struct blk_mq_ops nvme_rdma_mq_ops; |
| 157 | static const struct blk_mq_ops nvme_rdma_admin_mq_ops; |
| 158 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 159 | static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) |
| 160 | { |
| 161 | return queue - queue->ctrl->queues; |
| 162 | } |
| 163 | |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 164 | static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue) |
| 165 | { |
| 166 | return nvme_rdma_queue_idx(queue) > |
Sagi Grimberg | b1064d3 | 2019-01-18 16:43:24 -0800 | [diff] [blame] | 167 | queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] + |
| 168 | queue->ctrl->io_queues[HCTX_TYPE_READ]; |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 171 | static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) |
| 172 | { |
| 173 | return queue->cmnd_capsule_len - sizeof(struct nvme_command); |
| 174 | } |
| 175 | |
| 176 | static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 177 | size_t capsule_size, enum dma_data_direction dir) |
| 178 | { |
| 179 | ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); |
| 180 | kfree(qe->data); |
| 181 | } |
| 182 | |
| 183 | static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 184 | size_t capsule_size, enum dma_data_direction dir) |
| 185 | { |
| 186 | qe->data = kzalloc(capsule_size, GFP_KERNEL); |
| 187 | if (!qe->data) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); |
| 191 | if (ib_dma_mapping_error(ibdev, qe->dma)) { |
| 192 | kfree(qe->data); |
Prabhath Sajeepa | 6344d02 | 2018-11-28 11:11:29 -0700 | [diff] [blame] | 193 | qe->data = NULL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 194 | return -ENOMEM; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static void nvme_rdma_free_ring(struct ib_device *ibdev, |
| 201 | struct nvme_rdma_qe *ring, size_t ib_queue_size, |
| 202 | size_t capsule_size, enum dma_data_direction dir) |
| 203 | { |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < ib_queue_size; i++) |
| 207 | nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); |
| 208 | kfree(ring); |
| 209 | } |
| 210 | |
| 211 | static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, |
| 212 | size_t ib_queue_size, size_t capsule_size, |
| 213 | enum dma_data_direction dir) |
| 214 | { |
| 215 | struct nvme_rdma_qe *ring; |
| 216 | int i; |
| 217 | |
| 218 | ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); |
| 219 | if (!ring) |
| 220 | return NULL; |
| 221 | |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 222 | /* |
| 223 | * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue |
| 224 | * lifetime. It's safe, since any chage in the underlying RDMA device |
| 225 | * will issue error recovery and queue re-creation. |
| 226 | */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 227 | for (i = 0; i < ib_queue_size; i++) { |
| 228 | if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) |
| 229 | goto out_free_ring; |
| 230 | } |
| 231 | |
| 232 | return ring; |
| 233 | |
| 234 | out_free_ring: |
| 235 | nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | static void nvme_rdma_qp_event(struct ib_event *event, void *context) |
| 240 | { |
Max Gurtovoy | 27a4bee | 2016-11-23 11:38:48 +0200 | [diff] [blame] | 241 | pr_debug("QP event %s (%d)\n", |
| 242 | ib_event_msg(event->event), event->event); |
| 243 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) |
| 247 | { |
Bart Van Assche | 35da77d | 2018-10-08 14:28:54 -0700 | [diff] [blame] | 248 | int ret; |
| 249 | |
| 250 | ret = wait_for_completion_interruptible_timeout(&queue->cm_done, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 251 | msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); |
Bart Van Assche | 35da77d | 2018-10-08 14:28:54 -0700 | [diff] [blame] | 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | if (ret == 0) |
| 255 | return -ETIMEDOUT; |
| 256 | WARN_ON_ONCE(queue->cm_error > 0); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 257 | return queue->cm_error; |
| 258 | } |
| 259 | |
| 260 | static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) |
| 261 | { |
| 262 | struct nvme_rdma_device *dev = queue->device; |
| 263 | struct ib_qp_init_attr init_attr; |
| 264 | int ret; |
| 265 | |
| 266 | memset(&init_attr, 0, sizeof(init_attr)); |
| 267 | init_attr.event_handler = nvme_rdma_qp_event; |
| 268 | /* +1 for drain */ |
| 269 | init_attr.cap.max_send_wr = factor * queue->queue_size + 1; |
| 270 | /* +1 for drain */ |
| 271 | init_attr.cap.max_recv_wr = queue->queue_size + 1; |
| 272 | init_attr.cap.max_recv_sge = 1; |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 273 | init_attr.cap.max_send_sge = 1 + dev->num_inline_segments; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 274 | init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 275 | init_attr.qp_type = IB_QPT_RC; |
| 276 | init_attr.send_cq = queue->ib_cq; |
| 277 | init_attr.recv_cq = queue->ib_cq; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 278 | if (queue->pi_support) |
| 279 | init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 280 | init_attr.qp_context = queue; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 281 | |
| 282 | ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); |
| 283 | |
| 284 | queue->qp = queue->cm_id->qp; |
| 285 | return ret; |
| 286 | } |
| 287 | |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 288 | static void nvme_rdma_exit_request(struct blk_mq_tag_set *set, |
| 289 | struct request *rq, unsigned int hctx_idx) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 290 | { |
| 291 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 292 | |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 293 | kfree(req->sqe.data); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 296 | static int nvme_rdma_init_request(struct blk_mq_tag_set *set, |
| 297 | struct request *rq, unsigned int hctx_idx, |
| 298 | unsigned int numa_node) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 299 | { |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 300 | struct nvme_rdma_ctrl *ctrl = set->driver_data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 301 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 302 | int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 303 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 304 | |
Sagi Grimberg | 59e29ce | 2018-06-29 16:50:00 -0600 | [diff] [blame] | 305 | nvme_req(rq)->ctrl = &ctrl->ctrl; |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 306 | req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL); |
| 307 | if (!req->sqe.data) |
| 308 | return -ENOMEM; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 309 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 310 | /* metadata nvme_rdma_sgl struct is located after command's data SGL */ |
| 311 | if (queue->pi_support) |
| 312 | req->metadata_sgl = (void *)nvme_req(rq) + |
| 313 | sizeof(struct nvme_rdma_request) + |
| 314 | NVME_RDMA_DATA_SGL_SIZE; |
| 315 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 316 | req->queue = queue; |
| 317 | |
| 318 | return 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 321 | static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 322 | unsigned int hctx_idx) |
| 323 | { |
| 324 | struct nvme_rdma_ctrl *ctrl = data; |
| 325 | struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 326 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 327 | BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 328 | |
| 329 | hctx->driver_data = queue; |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 334 | unsigned int hctx_idx) |
| 335 | { |
| 336 | struct nvme_rdma_ctrl *ctrl = data; |
| 337 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 338 | |
| 339 | BUG_ON(hctx_idx != 0); |
| 340 | |
| 341 | hctx->driver_data = queue; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static void nvme_rdma_free_dev(struct kref *ref) |
| 346 | { |
| 347 | struct nvme_rdma_device *ndev = |
| 348 | container_of(ref, struct nvme_rdma_device, ref); |
| 349 | |
| 350 | mutex_lock(&device_list_mutex); |
| 351 | list_del(&ndev->entry); |
| 352 | mutex_unlock(&device_list_mutex); |
| 353 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 354 | ib_dealloc_pd(ndev->pd); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 355 | kfree(ndev); |
| 356 | } |
| 357 | |
| 358 | static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) |
| 359 | { |
| 360 | kref_put(&dev->ref, nvme_rdma_free_dev); |
| 361 | } |
| 362 | |
| 363 | static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) |
| 364 | { |
| 365 | return kref_get_unless_zero(&dev->ref); |
| 366 | } |
| 367 | |
| 368 | static struct nvme_rdma_device * |
| 369 | nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) |
| 370 | { |
| 371 | struct nvme_rdma_device *ndev; |
| 372 | |
| 373 | mutex_lock(&device_list_mutex); |
| 374 | list_for_each_entry(ndev, &device_list, entry) { |
| 375 | if (ndev->dev->node_guid == cm_id->device->node_guid && |
| 376 | nvme_rdma_dev_get(ndev)) |
| 377 | goto out_unlock; |
| 378 | } |
| 379 | |
| 380 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); |
| 381 | if (!ndev) |
| 382 | goto out_err; |
| 383 | |
| 384 | ndev->dev = cm_id->device; |
| 385 | kref_init(&ndev->ref); |
| 386 | |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 387 | ndev->pd = ib_alloc_pd(ndev->dev, |
| 388 | register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 389 | if (IS_ERR(ndev->pd)) |
| 390 | goto out_free_dev; |
| 391 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 392 | if (!(ndev->dev->attrs.device_cap_flags & |
| 393 | IB_DEVICE_MEM_MGT_EXTENSIONS)) { |
| 394 | dev_err(&ndev->dev->dev, |
| 395 | "Memory registrations not supported.\n"); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 396 | goto out_free_pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 397 | } |
| 398 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 399 | ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS, |
Jason Gunthorpe | 0a3173a | 2018-08-16 14:13:03 -0600 | [diff] [blame] | 400 | ndev->dev->attrs.max_send_sge - 1); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 401 | list_add(&ndev->entry, &device_list); |
| 402 | out_unlock: |
| 403 | mutex_unlock(&device_list_mutex); |
| 404 | return ndev; |
| 405 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 406 | out_free_pd: |
| 407 | ib_dealloc_pd(ndev->pd); |
| 408 | out_free_dev: |
| 409 | kfree(ndev); |
| 410 | out_err: |
| 411 | mutex_unlock(&device_list_mutex); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 415 | static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue) |
| 416 | { |
| 417 | if (nvme_rdma_poll_queue(queue)) |
| 418 | ib_free_cq(queue->ib_cq); |
| 419 | else |
| 420 | ib_cq_pool_put(queue->ib_cq, queue->cq_size); |
| 421 | } |
| 422 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 423 | static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) |
| 424 | { |
Max Gurtovoy | eb1bd24 | 2017-11-28 18:28:44 +0200 | [diff] [blame] | 425 | struct nvme_rdma_device *dev; |
| 426 | struct ib_device *ibdev; |
| 427 | |
| 428 | if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags)) |
| 429 | return; |
| 430 | |
| 431 | dev = queue->device; |
| 432 | ibdev = dev->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 433 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 434 | if (queue->pi_support) |
| 435 | ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 436 | ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); |
| 437 | |
Max Gurtovoy | eb1bd24 | 2017-11-28 18:28:44 +0200 | [diff] [blame] | 438 | /* |
| 439 | * The cm_id object might have been destroyed during RDMA connection |
| 440 | * establishment error flow to avoid getting other cma events, thus |
| 441 | * the destruction of the QP shouldn't use rdma_cm API. |
| 442 | */ |
| 443 | ib_destroy_qp(queue->qp); |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 444 | nvme_rdma_free_cq(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 445 | |
| 446 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, |
| 447 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 448 | |
| 449 | nvme_rdma_dev_put(dev); |
| 450 | } |
| 451 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 452 | static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support) |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 453 | { |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 454 | u32 max_page_list_len; |
| 455 | |
| 456 | if (pi_support) |
| 457 | max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len; |
| 458 | else |
| 459 | max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len; |
| 460 | |
| 461 | return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 464 | static int nvme_rdma_create_cq(struct ib_device *ibdev, |
| 465 | struct nvme_rdma_queue *queue) |
| 466 | { |
| 467 | int ret, comp_vector, idx = nvme_rdma_queue_idx(queue); |
| 468 | enum ib_poll_context poll_ctx; |
| 469 | |
| 470 | /* |
| 471 | * Spread I/O queues completion vectors according their queue index. |
| 472 | * Admin queues can always go on completion vector 0. |
| 473 | */ |
| 474 | comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors; |
| 475 | |
| 476 | /* Polling queues need direct cq polling context */ |
| 477 | if (nvme_rdma_poll_queue(queue)) { |
| 478 | poll_ctx = IB_POLL_DIRECT; |
| 479 | queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size, |
| 480 | comp_vector, poll_ctx); |
| 481 | } else { |
| 482 | poll_ctx = IB_POLL_SOFTIRQ; |
| 483 | queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size, |
| 484 | comp_vector, poll_ctx); |
| 485 | } |
| 486 | |
| 487 | if (IS_ERR(queue->ib_cq)) { |
| 488 | ret = PTR_ERR(queue->ib_cq); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 495 | static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 496 | { |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 497 | struct ib_device *ibdev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 498 | const int send_wr_factor = 3; /* MR, SEND, INV */ |
| 499 | const int cq_factor = send_wr_factor + 1; /* + RECV */ |
Max Gurtovoy | ff13c1b | 2019-09-21 23:58:19 +0300 | [diff] [blame] | 500 | int ret, pages_per_mr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 501 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 502 | queue->device = nvme_rdma_find_get_device(queue->cm_id); |
| 503 | if (!queue->device) { |
| 504 | dev_err(queue->cm_id->device->dev.parent, |
| 505 | "no client data found!\n"); |
| 506 | return -ECONNREFUSED; |
| 507 | } |
| 508 | ibdev = queue->device->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 509 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 510 | /* +1 for ib_stop_cq */ |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 511 | queue->cq_size = cq_factor * queue->queue_size + 1; |
| 512 | |
| 513 | ret = nvme_rdma_create_cq(ibdev, queue); |
| 514 | if (ret) |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 515 | goto out_put_dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 516 | |
| 517 | ret = nvme_rdma_create_qp(queue, send_wr_factor); |
| 518 | if (ret) |
| 519 | goto out_destroy_ib_cq; |
| 520 | |
| 521 | queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, |
| 522 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 523 | if (!queue->rsp_ring) { |
| 524 | ret = -ENOMEM; |
| 525 | goto out_destroy_qp; |
| 526 | } |
| 527 | |
Max Gurtovoy | ff13c1b | 2019-09-21 23:58:19 +0300 | [diff] [blame] | 528 | /* |
| 529 | * Currently we don't use SG_GAPS MR's so if the first entry is |
| 530 | * misaligned we'll end up using two entries for a single data page, |
| 531 | * so one additional entry is required. |
| 532 | */ |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 533 | pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1; |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 534 | ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs, |
| 535 | queue->queue_size, |
| 536 | IB_MR_TYPE_MEM_REG, |
Max Gurtovoy | ff13c1b | 2019-09-21 23:58:19 +0300 | [diff] [blame] | 537 | pages_per_mr, 0); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 538 | if (ret) { |
| 539 | dev_err(queue->ctrl->ctrl.device, |
| 540 | "failed to initialize MR pool sized %d for QID %d\n", |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 541 | queue->queue_size, nvme_rdma_queue_idx(queue)); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 542 | goto out_destroy_ring; |
| 543 | } |
| 544 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 545 | if (queue->pi_support) { |
| 546 | ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs, |
| 547 | queue->queue_size, IB_MR_TYPE_INTEGRITY, |
| 548 | pages_per_mr, pages_per_mr); |
| 549 | if (ret) { |
| 550 | dev_err(queue->ctrl->ctrl.device, |
| 551 | "failed to initialize PI MR pool sized %d for QID %d\n", |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 552 | queue->queue_size, nvme_rdma_queue_idx(queue)); |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 553 | goto out_destroy_mr_pool; |
| 554 | } |
| 555 | } |
| 556 | |
Max Gurtovoy | eb1bd24 | 2017-11-28 18:28:44 +0200 | [diff] [blame] | 557 | set_bit(NVME_RDMA_Q_TR_READY, &queue->flags); |
| 558 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 559 | return 0; |
| 560 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 561 | out_destroy_mr_pool: |
| 562 | ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 563 | out_destroy_ring: |
| 564 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, |
| 565 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 566 | out_destroy_qp: |
Max Gurtovoy | 1f61def | 2017-11-06 16:18:51 +0200 | [diff] [blame] | 567 | rdma_destroy_qp(queue->cm_id); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 568 | out_destroy_ib_cq: |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 569 | nvme_rdma_free_cq(queue); |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 570 | out_put_dev: |
| 571 | nvme_rdma_dev_put(queue->device); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 572 | return ret; |
| 573 | } |
| 574 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 575 | static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 576 | int idx, size_t queue_size) |
| 577 | { |
| 578 | struct nvme_rdma_queue *queue; |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 579 | struct sockaddr *src_addr = NULL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 580 | int ret; |
| 581 | |
| 582 | queue = &ctrl->queues[idx]; |
| 583 | queue->ctrl = ctrl; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 584 | if (idx && ctrl->ctrl.max_integrity_segments) |
| 585 | queue->pi_support = true; |
| 586 | else |
| 587 | queue->pi_support = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 588 | init_completion(&queue->cm_done); |
| 589 | |
| 590 | if (idx > 0) |
| 591 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; |
| 592 | else |
| 593 | queue->cmnd_capsule_len = sizeof(struct nvme_command); |
| 594 | |
| 595 | queue->queue_size = queue_size; |
| 596 | |
| 597 | queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, |
| 598 | RDMA_PS_TCP, IB_QPT_RC); |
| 599 | if (IS_ERR(queue->cm_id)) { |
| 600 | dev_info(ctrl->ctrl.device, |
| 601 | "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); |
| 602 | return PTR_ERR(queue->cm_id); |
| 603 | } |
| 604 | |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 605 | if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 606 | src_addr = (struct sockaddr *)&ctrl->src_addr; |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 607 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 608 | queue->cm_error = -ETIMEDOUT; |
| 609 | ret = rdma_resolve_addr(queue->cm_id, src_addr, |
| 610 | (struct sockaddr *)&ctrl->addr, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 611 | NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 612 | if (ret) { |
| 613 | dev_info(ctrl->ctrl.device, |
| 614 | "rdma_resolve_addr failed (%d).\n", ret); |
| 615 | goto out_destroy_cm_id; |
| 616 | } |
| 617 | |
| 618 | ret = nvme_rdma_wait_for_cm(queue); |
| 619 | if (ret) { |
| 620 | dev_info(ctrl->ctrl.device, |
Sagi Grimberg | d8bfcee | 2017-10-11 15:29:07 +0300 | [diff] [blame] | 621 | "rdma connection establishment failed (%d)\n", ret); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 622 | goto out_destroy_cm_id; |
| 623 | } |
| 624 | |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 625 | set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 626 | |
| 627 | return 0; |
| 628 | |
| 629 | out_destroy_cm_id: |
| 630 | rdma_destroy_id(queue->cm_id); |
Max Gurtovoy | eb1bd24 | 2017-11-28 18:28:44 +0200 | [diff] [blame] | 631 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 632 | return ret; |
| 633 | } |
| 634 | |
Sagi Grimberg | d94211b | 2019-07-26 10:29:49 -0700 | [diff] [blame] | 635 | static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) |
| 636 | { |
| 637 | rdma_disconnect(queue->cm_id); |
| 638 | ib_drain_qp(queue->qp); |
| 639 | } |
| 640 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 641 | static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) |
| 642 | { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 643 | if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags)) |
| 644 | return; |
Sagi Grimberg | d94211b | 2019-07-26 10:29:49 -0700 | [diff] [blame] | 645 | __nvme_rdma_stop_queue(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) |
| 649 | { |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 650 | if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 651 | return; |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 652 | |
| 653 | nvme_rdma_destroy_queue_ib(queue); |
| 654 | rdma_destroy_id(queue->cm_id); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 658 | { |
| 659 | int i; |
| 660 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 661 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 662 | nvme_rdma_free_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 663 | } |
| 664 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 665 | static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 666 | { |
| 667 | int i; |
| 668 | |
| 669 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
| 670 | nvme_rdma_stop_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 671 | } |
| 672 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 673 | static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx) |
| 674 | { |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 675 | struct nvme_rdma_queue *queue = &ctrl->queues[idx]; |
| 676 | bool poll = nvme_rdma_poll_queue(queue); |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 677 | int ret; |
| 678 | |
| 679 | if (idx) |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 680 | ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll); |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 681 | else |
| 682 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 683 | |
Sagi Grimberg | d94211b | 2019-07-26 10:29:49 -0700 | [diff] [blame] | 684 | if (!ret) { |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 685 | set_bit(NVME_RDMA_Q_LIVE, &queue->flags); |
Sagi Grimberg | d94211b | 2019-07-26 10:29:49 -0700 | [diff] [blame] | 686 | } else { |
Sagi Grimberg | 67b483d | 2019-09-24 11:27:05 -0700 | [diff] [blame] | 687 | if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) |
| 688 | __nvme_rdma_stop_queue(queue); |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 689 | dev_info(ctrl->ctrl.device, |
| 690 | "failed to connect queue: %d ret=%d\n", idx, ret); |
Sagi Grimberg | d94211b | 2019-07-26 10:29:49 -0700 | [diff] [blame] | 691 | } |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 696 | { |
| 697 | int i, ret = 0; |
| 698 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 699 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 700 | ret = nvme_rdma_start_queue(ctrl, i); |
| 701 | if (ret) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 702 | goto out_stop_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 703 | } |
| 704 | |
Steve Wise | c8dbc37 | 2016-11-08 09:16:02 -0800 | [diff] [blame] | 705 | return 0; |
| 706 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 707 | out_stop_queues: |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 708 | for (i--; i >= 1; i--) |
| 709 | nvme_rdma_stop_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 713 | static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 714 | { |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 715 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 716 | struct ib_device *ibdev = ctrl->device->dev; |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 717 | unsigned int nr_io_queues, nr_default_queues; |
| 718 | unsigned int nr_read_queues, nr_poll_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 719 | int i, ret; |
| 720 | |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 721 | nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors, |
| 722 | min(opts->nr_io_queues, num_online_cpus())); |
| 723 | nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors, |
| 724 | min(opts->nr_write_queues, num_online_cpus())); |
| 725 | nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus()); |
| 726 | nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues; |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 727 | |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 728 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
| 729 | if (ret) |
| 730 | return ret; |
| 731 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 732 | ctrl->ctrl.queue_count = nr_io_queues + 1; |
| 733 | if (ctrl->ctrl.queue_count < 2) |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 734 | return 0; |
| 735 | |
| 736 | dev_info(ctrl->ctrl.device, |
| 737 | "creating %d I/O queues.\n", nr_io_queues); |
| 738 | |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 739 | if (opts->nr_write_queues && nr_read_queues < nr_io_queues) { |
| 740 | /* |
| 741 | * separate read/write queues |
| 742 | * hand out dedicated default queues only after we have |
| 743 | * sufficient read queues. |
| 744 | */ |
| 745 | ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues; |
| 746 | nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ]; |
| 747 | ctrl->io_queues[HCTX_TYPE_DEFAULT] = |
| 748 | min(nr_default_queues, nr_io_queues); |
| 749 | nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
| 750 | } else { |
| 751 | /* |
| 752 | * shared read/write queues |
| 753 | * either no write queues were requested, or we don't have |
| 754 | * sufficient queue count to have dedicated default queues. |
| 755 | */ |
| 756 | ctrl->io_queues[HCTX_TYPE_DEFAULT] = |
| 757 | min(nr_read_queues, nr_io_queues); |
| 758 | nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
| 759 | } |
| 760 | |
| 761 | if (opts->nr_poll_queues && nr_io_queues) { |
| 762 | /* map dedicated poll queues only if we have queues left */ |
| 763 | ctrl->io_queues[HCTX_TYPE_POLL] = |
| 764 | min(nr_poll_queues, nr_io_queues); |
| 765 | } |
| 766 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 767 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 768 | ret = nvme_rdma_alloc_queue(ctrl, i, |
| 769 | ctrl->ctrl.sqsize + 1); |
| 770 | if (ret) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 771 | goto out_free_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | |
| 776 | out_free_queues: |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 777 | for (i--; i >= 1; i--) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 778 | nvme_rdma_free_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 783 | static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl, |
| 784 | bool admin) |
| 785 | { |
| 786 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 787 | struct blk_mq_tag_set *set; |
| 788 | int ret; |
| 789 | |
| 790 | if (admin) { |
| 791 | set = &ctrl->admin_tag_set; |
| 792 | memset(set, 0, sizeof(*set)); |
| 793 | set->ops = &nvme_rdma_admin_mq_ops; |
Keith Busch | 38dabe2 | 2017-11-07 15:13:10 -0700 | [diff] [blame] | 794 | set->queue_depth = NVME_AQ_MQ_TAG_DEPTH; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 795 | set->reserved_tags = 2; /* connect + keep-alive */ |
Hannes Reinecke | 103e515 | 2018-11-16 09:22:29 +0100 | [diff] [blame] | 796 | set->numa_node = nctrl->numa_node; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 797 | set->cmd_size = sizeof(struct nvme_rdma_request) + |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 798 | NVME_RDMA_DATA_SGL_SIZE; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 799 | set->driver_data = ctrl; |
| 800 | set->nr_hw_queues = 1; |
| 801 | set->timeout = ADMIN_TIMEOUT; |
Israel Rukshin | 94f29d4 | 2017-10-18 12:38:24 +0000 | [diff] [blame] | 802 | set->flags = BLK_MQ_F_NO_SCHED; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 803 | } else { |
| 804 | set = &ctrl->tag_set; |
| 805 | memset(set, 0, sizeof(*set)); |
| 806 | set->ops = &nvme_rdma_mq_ops; |
Sagi Grimberg | 5e77d61 | 2018-06-19 15:34:13 +0300 | [diff] [blame] | 807 | set->queue_depth = nctrl->sqsize + 1; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 808 | set->reserved_tags = 1; /* fabric connect */ |
Hannes Reinecke | 103e515 | 2018-11-16 09:22:29 +0100 | [diff] [blame] | 809 | set->numa_node = nctrl->numa_node; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 810 | set->flags = BLK_MQ_F_SHOULD_MERGE; |
| 811 | set->cmd_size = sizeof(struct nvme_rdma_request) + |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 812 | NVME_RDMA_DATA_SGL_SIZE; |
| 813 | if (nctrl->max_integrity_segments) |
| 814 | set->cmd_size += sizeof(struct nvme_rdma_sgl) + |
| 815 | NVME_RDMA_METADATA_SGL_SIZE; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 816 | set->driver_data = ctrl; |
| 817 | set->nr_hw_queues = nctrl->queue_count - 1; |
| 818 | set->timeout = NVME_IO_TIMEOUT; |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 819 | set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | ret = blk_mq_alloc_tag_set(set); |
| 823 | if (ret) |
Max Gurtovoy | 87fd125 | 2019-05-06 13:47:55 +0300 | [diff] [blame] | 824 | return ERR_PTR(ret); |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 825 | |
| 826 | return set; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 827 | } |
| 828 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 829 | static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl, |
| 830 | bool remove) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 831 | { |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 832 | if (remove) { |
| 833 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 834 | blk_cleanup_queue(ctrl->ctrl.fabrics_q); |
Max Gurtovoy | 87fd125 | 2019-05-06 13:47:55 +0300 | [diff] [blame] | 835 | blk_mq_free_tag_set(ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 836 | } |
Sagi Grimberg | 682630f | 2018-06-25 20:58:17 +0300 | [diff] [blame] | 837 | if (ctrl->async_event_sqe.data) { |
David Milburn | 925dd04 | 2020-09-02 17:42:52 -0500 | [diff] [blame] | 838 | cancel_work_sync(&ctrl->ctrl.async_event_work); |
Sagi Grimberg | 682630f | 2018-06-25 20:58:17 +0300 | [diff] [blame] | 839 | nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, |
| 840 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 841 | ctrl->async_event_sqe.data = NULL; |
| 842 | } |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 843 | nvme_rdma_free_queue(&ctrl->queues[0]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 844 | } |
| 845 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 846 | static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, |
| 847 | bool new) |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 848 | { |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 849 | bool pi_capable = false; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 850 | int error; |
| 851 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 852 | error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 853 | if (error) |
| 854 | return error; |
| 855 | |
| 856 | ctrl->device = ctrl->queues[0].device; |
Hannes Reinecke | 103e515 | 2018-11-16 09:22:29 +0100 | [diff] [blame] | 857 | ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 858 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 859 | /* T10-PI support */ |
| 860 | if (ctrl->device->dev->attrs.device_cap_flags & |
| 861 | IB_DEVICE_INTEGRITY_HANDOVER) |
| 862 | pi_capable = true; |
| 863 | |
| 864 | ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev, |
| 865 | pi_capable); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 866 | |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 867 | /* |
| 868 | * Bind the async event SQE DMA mapping to the admin queue lifetime. |
| 869 | * It's safe, since any chage in the underlying RDMA device will issue |
| 870 | * error recovery and queue re-creation. |
| 871 | */ |
Sagi Grimberg | 94e4221 | 2018-06-19 15:34:10 +0300 | [diff] [blame] | 872 | error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe, |
| 873 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 874 | if (error) |
| 875 | goto out_free_queue; |
| 876 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 877 | if (new) { |
| 878 | ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true); |
Sagi Grimberg | f04b9cc | 2017-10-19 18:10:53 +0300 | [diff] [blame] | 879 | if (IS_ERR(ctrl->ctrl.admin_tagset)) { |
| 880 | error = PTR_ERR(ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 94e4221 | 2018-06-19 15:34:10 +0300 | [diff] [blame] | 881 | goto out_free_async_qe; |
Sagi Grimberg | f04b9cc | 2017-10-19 18:10:53 +0300 | [diff] [blame] | 882 | } |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 883 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 884 | ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 885 | if (IS_ERR(ctrl->ctrl.fabrics_q)) { |
| 886 | error = PTR_ERR(ctrl->ctrl.fabrics_q); |
| 887 | goto out_free_tagset; |
| 888 | } |
| 889 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 890 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 891 | if (IS_ERR(ctrl->ctrl.admin_q)) { |
| 892 | error = PTR_ERR(ctrl->ctrl.admin_q); |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 893 | goto out_cleanup_fabrics_q; |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 894 | } |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 895 | } |
| 896 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 897 | error = nvme_rdma_start_queue(ctrl, 0); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 898 | if (error) |
| 899 | goto out_cleanup_queue; |
| 900 | |
Sagi Grimberg | c0f2f45 | 2019-07-22 17:06:53 -0700 | [diff] [blame] | 901 | error = nvme_enable_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 902 | if (error) |
Jianchao Wang | 2e050f0 | 2018-05-24 09:27:38 +0800 | [diff] [blame] | 903 | goto out_stop_queue; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 904 | |
Max Gurtovoy | ff13c1b | 2019-09-21 23:58:19 +0300 | [diff] [blame] | 905 | ctrl->ctrl.max_segments = ctrl->max_fr_pages; |
| 906 | ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9); |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 907 | if (pi_capable) |
| 908 | ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages; |
| 909 | else |
| 910 | ctrl->ctrl.max_integrity_segments = 0; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 911 | |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 912 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
| 913 | |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 914 | error = nvme_init_identify(&ctrl->ctrl); |
| 915 | if (error) |
Jianchao Wang | 2e050f0 | 2018-05-24 09:27:38 +0800 | [diff] [blame] | 916 | goto out_stop_queue; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 917 | |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 918 | return 0; |
| 919 | |
Jianchao Wang | 2e050f0 | 2018-05-24 09:27:38 +0800 | [diff] [blame] | 920 | out_stop_queue: |
| 921 | nvme_rdma_stop_queue(&ctrl->queues[0]); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 922 | out_cleanup_queue: |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 923 | if (new) |
| 924 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 925 | out_cleanup_fabrics_q: |
| 926 | if (new) |
| 927 | blk_cleanup_queue(ctrl->ctrl.fabrics_q); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 928 | out_free_tagset: |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 929 | if (new) |
Max Gurtovoy | 87fd125 | 2019-05-06 13:47:55 +0300 | [diff] [blame] | 930 | blk_mq_free_tag_set(ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 94e4221 | 2018-06-19 15:34:10 +0300 | [diff] [blame] | 931 | out_free_async_qe: |
Prabhath Sajeepa | 9134ae2 | 2020-03-09 15:07:53 -0600 | [diff] [blame] | 932 | if (ctrl->async_event_sqe.data) { |
| 933 | nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, |
| 934 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 935 | ctrl->async_event_sqe.data = NULL; |
| 936 | } |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 937 | out_free_queue: |
| 938 | nvme_rdma_free_queue(&ctrl->queues[0]); |
| 939 | return error; |
| 940 | } |
| 941 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 942 | static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl, |
| 943 | bool remove) |
| 944 | { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 945 | if (remove) { |
| 946 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
Max Gurtovoy | 87fd125 | 2019-05-06 13:47:55 +0300 | [diff] [blame] | 947 | blk_mq_free_tag_set(ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 948 | } |
| 949 | nvme_rdma_free_io_queues(ctrl); |
| 950 | } |
| 951 | |
| 952 | static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new) |
| 953 | { |
| 954 | int ret; |
| 955 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 956 | ret = nvme_rdma_alloc_io_queues(ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 957 | if (ret) |
| 958 | return ret; |
| 959 | |
| 960 | if (new) { |
| 961 | ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false); |
Sagi Grimberg | f04b9cc | 2017-10-19 18:10:53 +0300 | [diff] [blame] | 962 | if (IS_ERR(ctrl->ctrl.tagset)) { |
| 963 | ret = PTR_ERR(ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 964 | goto out_free_io_queues; |
Sagi Grimberg | f04b9cc | 2017-10-19 18:10:53 +0300 | [diff] [blame] | 965 | } |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 966 | |
| 967 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); |
| 968 | if (IS_ERR(ctrl->ctrl.connect_q)) { |
| 969 | ret = PTR_ERR(ctrl->ctrl.connect_q); |
| 970 | goto out_free_tag_set; |
| 971 | } |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 972 | } |
| 973 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 974 | ret = nvme_rdma_start_io_queues(ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 975 | if (ret) |
| 976 | goto out_cleanup_connect_q; |
| 977 | |
Sagi Grimberg | 9f98772 | 2020-07-27 17:32:09 -0700 | [diff] [blame] | 978 | if (!new) { |
| 979 | nvme_start_queues(&ctrl->ctrl); |
Sagi Grimberg | 2362acb | 2020-07-30 13:42:42 -0700 | [diff] [blame] | 980 | if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) { |
| 981 | /* |
| 982 | * If we timed out waiting for freeze we are likely to |
| 983 | * be stuck. Fail the controller initialization just |
| 984 | * to be safe. |
| 985 | */ |
| 986 | ret = -ENODEV; |
| 987 | goto out_wait_freeze_timed_out; |
| 988 | } |
Sagi Grimberg | 9f98772 | 2020-07-27 17:32:09 -0700 | [diff] [blame] | 989 | blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset, |
| 990 | ctrl->ctrl.queue_count - 1); |
| 991 | nvme_unfreeze(&ctrl->ctrl); |
| 992 | } |
| 993 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 994 | return 0; |
| 995 | |
Sagi Grimberg | 2362acb | 2020-07-30 13:42:42 -0700 | [diff] [blame] | 996 | out_wait_freeze_timed_out: |
| 997 | nvme_stop_queues(&ctrl->ctrl); |
| 998 | nvme_rdma_stop_io_queues(ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 999 | out_cleanup_connect_q: |
| 1000 | if (new) |
| 1001 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 1002 | out_free_tag_set: |
| 1003 | if (new) |
Max Gurtovoy | 87fd125 | 2019-05-06 13:47:55 +0300 | [diff] [blame] | 1004 | blk_mq_free_tag_set(ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 1005 | out_free_io_queues: |
| 1006 | nvme_rdma_free_io_queues(ctrl); |
| 1007 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1008 | } |
| 1009 | |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1010 | static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl, |
| 1011 | bool remove) |
| 1012 | { |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 1013 | mutex_lock(&ctrl->teardown_lock); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1014 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
| 1015 | nvme_rdma_stop_queue(&ctrl->queues[0]); |
Ming Lei | 622b8b6 | 2019-07-24 11:48:42 +0800 | [diff] [blame] | 1016 | if (ctrl->ctrl.admin_tagset) { |
Sagi Grimberg | 1007709 | 2019-04-24 11:53:18 -0700 | [diff] [blame] | 1017 | blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset, |
| 1018 | nvme_cancel_request, &ctrl->ctrl); |
Ming Lei | 622b8b6 | 2019-07-24 11:48:42 +0800 | [diff] [blame] | 1019 | blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset); |
| 1020 | } |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 1021 | if (remove) |
| 1022 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1023 | nvme_rdma_destroy_admin_queue(ctrl, remove); |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 1024 | mutex_unlock(&ctrl->teardown_lock); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl, |
| 1028 | bool remove) |
| 1029 | { |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 1030 | mutex_lock(&ctrl->teardown_lock); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1031 | if (ctrl->ctrl.queue_count > 1) { |
Sagi Grimberg | 9f98772 | 2020-07-27 17:32:09 -0700 | [diff] [blame] | 1032 | nvme_start_freeze(&ctrl->ctrl); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1033 | nvme_stop_queues(&ctrl->ctrl); |
| 1034 | nvme_rdma_stop_io_queues(ctrl); |
Ming Lei | 622b8b6 | 2019-07-24 11:48:42 +0800 | [diff] [blame] | 1035 | if (ctrl->ctrl.tagset) { |
Sagi Grimberg | 1007709 | 2019-04-24 11:53:18 -0700 | [diff] [blame] | 1036 | blk_mq_tagset_busy_iter(ctrl->ctrl.tagset, |
| 1037 | nvme_cancel_request, &ctrl->ctrl); |
Ming Lei | 622b8b6 | 2019-07-24 11:48:42 +0800 | [diff] [blame] | 1038 | blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset); |
| 1039 | } |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1040 | if (remove) |
| 1041 | nvme_start_queues(&ctrl->ctrl); |
| 1042 | nvme_rdma_destroy_io_queues(ctrl, remove); |
| 1043 | } |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 1044 | mutex_unlock(&ctrl->teardown_lock); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1045 | } |
| 1046 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1047 | static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) |
| 1048 | { |
| 1049 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 1050 | |
| 1051 | if (list_empty(&ctrl->list)) |
| 1052 | goto free_ctrl; |
| 1053 | |
| 1054 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 1055 | list_del(&ctrl->list); |
| 1056 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 1057 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1058 | nvmf_free_options(nctrl->opts); |
| 1059 | free_ctrl: |
Sagi Grimberg | 3d06410 | 2018-06-19 15:34:09 +0300 | [diff] [blame] | 1060 | kfree(ctrl->queues); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1061 | kfree(ctrl); |
| 1062 | } |
| 1063 | |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1064 | static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl) |
| 1065 | { |
| 1066 | /* If we are resetting/deleting then do nothing */ |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 1067 | if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) { |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1068 | WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW || |
| 1069 | ctrl->ctrl.state == NVME_CTRL_LIVE); |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | if (nvmf_should_reconnect(&ctrl->ctrl)) { |
| 1074 | dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n", |
| 1075 | ctrl->ctrl.opts->reconnect_delay); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 1076 | queue_delayed_work(nvme_wq, &ctrl->reconnect_work, |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1077 | ctrl->ctrl.opts->reconnect_delay * HZ); |
| 1078 | } else { |
Sagi Grimberg | 12fa130 | 2017-10-29 14:21:01 +0200 | [diff] [blame] | 1079 | nvme_delete_ctrl(&ctrl->ctrl); |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1083 | static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1084 | { |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1085 | int ret = -EINVAL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1086 | bool changed; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1087 | |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1088 | ret = nvme_rdma_configure_admin_queue(ctrl, new); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1089 | if (ret) |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1090 | return ret; |
| 1091 | |
| 1092 | if (ctrl->ctrl.icdoff) { |
| 1093 | dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); |
| 1094 | goto destroy_admin; |
| 1095 | } |
| 1096 | |
| 1097 | if (!(ctrl->ctrl.sgls & (1 << 2))) { |
| 1098 | dev_err(ctrl->ctrl.device, |
| 1099 | "Mandatory keyed sgls are not supported!\n"); |
| 1100 | goto destroy_admin; |
| 1101 | } |
| 1102 | |
| 1103 | if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) { |
| 1104 | dev_warn(ctrl->ctrl.device, |
| 1105 | "queue_size %zu > ctrl sqsize %u, clamping down\n", |
| 1106 | ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1); |
| 1107 | } |
| 1108 | |
| 1109 | if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) { |
| 1110 | dev_warn(ctrl->ctrl.device, |
| 1111 | "sqsize %u > ctrl maxcmd %u, clamping down\n", |
| 1112 | ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd); |
| 1113 | ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1; |
| 1114 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1115 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1116 | if (ctrl->ctrl.sgls & (1 << 20)) |
| 1117 | ctrl->use_inline_data = true; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1118 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1119 | if (ctrl->ctrl.queue_count > 1) { |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1120 | ret = nvme_rdma_configure_io_queues(ctrl, new); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1121 | if (ret) |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 1122 | goto destroy_admin; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
Sagi Grimberg | 0a960afd | 2017-09-21 17:01:37 +0300 | [diff] [blame] | 1126 | if (!changed) { |
Israel Rukshin | 9613586 | 2020-03-24 17:29:44 +0200 | [diff] [blame] | 1127 | /* |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 1128 | * state change failure is ok if we started ctrl delete, |
Israel Rukshin | 9613586 | 2020-03-24 17:29:44 +0200 | [diff] [blame] | 1129 | * unless we're during creation of a new controller to |
| 1130 | * avoid races with teardown flow. |
| 1131 | */ |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 1132 | WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING && |
| 1133 | ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO); |
Israel Rukshin | 9613586 | 2020-03-24 17:29:44 +0200 | [diff] [blame] | 1134 | WARN_ON_ONCE(new); |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1135 | ret = -EINVAL; |
| 1136 | goto destroy_io; |
Sagi Grimberg | 0a960afd | 2017-09-21 17:01:37 +0300 | [diff] [blame] | 1137 | } |
| 1138 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 1139 | nvme_start_ctrl(&ctrl->ctrl); |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 1140 | return 0; |
| 1141 | |
| 1142 | destroy_io: |
| 1143 | if (ctrl->ctrl.queue_count > 1) |
| 1144 | nvme_rdma_destroy_io_queues(ctrl, new); |
| 1145 | destroy_admin: |
| 1146 | nvme_rdma_stop_queue(&ctrl->queues[0]); |
| 1147 | nvme_rdma_destroy_admin_queue(ctrl, new); |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) |
| 1152 | { |
| 1153 | struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), |
| 1154 | struct nvme_rdma_ctrl, reconnect_work); |
| 1155 | |
| 1156 | ++ctrl->ctrl.nr_reconnects; |
| 1157 | |
| 1158 | if (nvme_rdma_setup_ctrl(ctrl, false)) |
| 1159 | goto requeue; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1160 | |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 1161 | dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n", |
| 1162 | ctrl->ctrl.nr_reconnects); |
| 1163 | |
| 1164 | ctrl->ctrl.nr_reconnects = 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1165 | |
| 1166 | return; |
| 1167 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1168 | requeue: |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1169 | dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 1170 | ctrl->ctrl.nr_reconnects); |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1171 | nvme_rdma_reconnect_or_remove(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | static void nvme_rdma_error_recovery_work(struct work_struct *work) |
| 1175 | { |
| 1176 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 1177 | struct nvme_rdma_ctrl, err_work); |
| 1178 | |
Sagi Grimberg | e4d753d | 2017-09-21 17:01:38 +0300 | [diff] [blame] | 1179 | nvme_stop_keep_alive(&ctrl->ctrl); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1180 | nvme_rdma_teardown_io_queues(ctrl, false); |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1181 | nvme_start_queues(&ctrl->ctrl); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 1182 | nvme_rdma_teardown_admin_queue(ctrl, false); |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 1183 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1184 | |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 1185 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 1186 | /* state change failure is ok if we started ctrl delete */ |
| 1187 | WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING && |
| 1188 | ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO); |
Sagi Grimberg | d5bf4b7 | 2017-12-21 14:54:15 +0200 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 1192 | nvme_rdma_reconnect_or_remove(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) |
| 1196 | { |
Sagi Grimberg | d5bf4b7 | 2017-12-21 14:54:15 +0200 | [diff] [blame] | 1197 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1198 | return; |
| 1199 | |
Sagi Grimberg | 0475a8d | 2020-07-29 02:36:03 -0700 | [diff] [blame] | 1200 | dev_warn(ctrl->ctrl.device, "starting error recovery\n"); |
Nigel Kirkland | 97b2512 | 2020-02-10 16:01:45 -0800 | [diff] [blame] | 1201 | queue_work(nvme_reset_wq, &ctrl->err_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1202 | } |
| 1203 | |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1204 | static void nvme_rdma_end_request(struct nvme_rdma_request *req) |
| 1205 | { |
| 1206 | struct request *rq = blk_mq_rq_from_pdu(req); |
| 1207 | |
| 1208 | if (!refcount_dec_and_test(&req->ref)) |
| 1209 | return; |
Christoph Hellwig | 2eb81a3 | 2020-08-18 09:11:29 +0200 | [diff] [blame] | 1210 | if (!nvme_try_complete_req(rq, req->status, req->result)) |
Christoph Hellwig | ff02945 | 2020-06-11 08:44:52 +0200 | [diff] [blame] | 1211 | nvme_rdma_complete_rq(rq); |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1214 | static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, |
| 1215 | const char *op) |
| 1216 | { |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 1217 | struct nvme_rdma_queue *queue = wc->qp->qp_context; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1218 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1219 | |
| 1220 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) |
| 1221 | dev_info(ctrl->ctrl.device, |
| 1222 | "%s for CQE 0x%p failed with status %s (%d)\n", |
| 1223 | op, wc->wr_cqe, |
| 1224 | ib_wc_status_msg(wc->status), wc->status); |
| 1225 | nvme_rdma_error_recovery(ctrl); |
| 1226 | } |
| 1227 | |
| 1228 | static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1229 | { |
| 1230 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1231 | nvme_rdma_wr_error(cq, wc, "MEMREG"); |
| 1232 | } |
| 1233 | |
| 1234 | static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1235 | { |
Sagi Grimberg | 2f122e4 | 2017-11-23 17:35:23 +0200 | [diff] [blame] | 1236 | struct nvme_rdma_request *req = |
| 1237 | container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe); |
Sagi Grimberg | 2f122e4 | 2017-11-23 17:35:23 +0200 | [diff] [blame] | 1238 | |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1239 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1240 | nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1241 | else |
| 1242 | nvme_rdma_end_request(req); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, |
| 1246 | struct nvme_rdma_request *req) |
| 1247 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1248 | struct ib_send_wr wr = { |
| 1249 | .opcode = IB_WR_LOCAL_INV, |
| 1250 | .next = NULL, |
| 1251 | .num_sge = 0, |
Sagi Grimberg | 2f122e4 | 2017-11-23 17:35:23 +0200 | [diff] [blame] | 1252 | .send_flags = IB_SEND_SIGNALED, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1253 | .ex.invalidate_rkey = req->mr->rkey, |
| 1254 | }; |
| 1255 | |
| 1256 | req->reg_cqe.done = nvme_rdma_inv_rkey_done; |
| 1257 | wr.wr_cqe = &req->reg_cqe; |
| 1258 | |
Bart Van Assche | 45e3cc1a | 2018-07-18 09:25:23 -0700 | [diff] [blame] | 1259 | return ib_post_send(queue->qp, &wr, NULL); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, |
| 1263 | struct request *rq) |
| 1264 | { |
| 1265 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1266 | struct nvme_rdma_device *dev = queue->device; |
| 1267 | struct ib_device *ibdev = dev->dev; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1268 | struct list_head *pool = &queue->qp->rdma_mrs; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1269 | |
Chaitanya Kulkarni | 34e0819 | 2019-02-20 20:13:34 -0800 | [diff] [blame] | 1270 | if (!blk_rq_nr_phys_segments(rq)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1271 | return; |
| 1272 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1273 | if (blk_integrity_rq(rq)) { |
| 1274 | ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl, |
| 1275 | req->metadata_sgl->nents, rq_dma_dir(rq)); |
| 1276 | sg_free_table_chained(&req->metadata_sgl->sg_table, |
| 1277 | NVME_INLINE_METADATA_SG_CNT); |
| 1278 | } |
| 1279 | |
| 1280 | if (req->use_sig_mr) |
| 1281 | pool = &queue->qp->sig_mrs; |
| 1282 | |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 1283 | if (req->mr) { |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1284 | ib_mr_pool_put(queue->qp, pool, req->mr); |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 1285 | req->mr = NULL; |
| 1286 | } |
| 1287 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1288 | ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents, |
| 1289 | rq_dma_dir(rq)); |
| 1290 | sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | static int nvme_rdma_set_sg_null(struct nvme_command *c) |
| 1294 | { |
| 1295 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1296 | |
| 1297 | sg->addr = 0; |
| 1298 | put_unaligned_le24(0, sg->length); |
| 1299 | put_unaligned_le32(0, sg->key); |
| 1300 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1305 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 1306 | int count) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1307 | { |
| 1308 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1309 | struct scatterlist *sgl = req->data_sgl.sg_table.sgl; |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1310 | struct ib_sge *sge = &req->sge[1]; |
| 1311 | u32 len = 0; |
| 1312 | int i; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1313 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1314 | for (i = 0; i < count; i++, sgl++, sge++) { |
| 1315 | sge->addr = sg_dma_address(sgl); |
| 1316 | sge->length = sg_dma_len(sgl); |
| 1317 | sge->lkey = queue->device->pd->local_dma_lkey; |
| 1318 | len += sge->length; |
| 1319 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1320 | |
| 1321 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1322 | sg->length = cpu_to_le32(len); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1323 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; |
| 1324 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1325 | req->num_sge += count; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, |
| 1330 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 1331 | { |
| 1332 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1333 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1334 | sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl)); |
| 1335 | put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 1336 | put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1337 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, |
| 1342 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 1343 | int count) |
| 1344 | { |
| 1345 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1346 | int nr; |
| 1347 | |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 1348 | req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs); |
| 1349 | if (WARN_ON_ONCE(!req->mr)) |
| 1350 | return -EAGAIN; |
| 1351 | |
Max Gurtovoy | b925a2d | 2017-08-28 12:52:27 +0300 | [diff] [blame] | 1352 | /* |
| 1353 | * Align the MR to a 4K page size to match the ctrl page size and |
| 1354 | * the block virtual boundary. |
| 1355 | */ |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1356 | nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL, |
| 1357 | SZ_4K); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1358 | if (unlikely(nr < count)) { |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 1359 | ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr); |
| 1360 | req->mr = NULL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1361 | if (nr < 0) |
| 1362 | return nr; |
| 1363 | return -EINVAL; |
| 1364 | } |
| 1365 | |
| 1366 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); |
| 1367 | |
| 1368 | req->reg_cqe.done = nvme_rdma_memreg_done; |
| 1369 | memset(&req->reg_wr, 0, sizeof(req->reg_wr)); |
| 1370 | req->reg_wr.wr.opcode = IB_WR_REG_MR; |
| 1371 | req->reg_wr.wr.wr_cqe = &req->reg_cqe; |
| 1372 | req->reg_wr.wr.num_sge = 0; |
| 1373 | req->reg_wr.mr = req->mr; |
| 1374 | req->reg_wr.key = req->mr->rkey; |
| 1375 | req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | |
| 1376 | IB_ACCESS_REMOTE_READ | |
| 1377 | IB_ACCESS_REMOTE_WRITE; |
| 1378 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1379 | sg->addr = cpu_to_le64(req->mr->iova); |
| 1380 | put_unaligned_le24(req->mr->length, sg->length); |
| 1381 | put_unaligned_le32(req->mr->rkey, sg->key); |
| 1382 | sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | |
| 1383 | NVME_SGL_FMT_INVALIDATE; |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1388 | static void nvme_rdma_set_sig_domain(struct blk_integrity *bi, |
| 1389 | struct nvme_command *cmd, struct ib_sig_domain *domain, |
| 1390 | u16 control, u8 pi_type) |
| 1391 | { |
| 1392 | domain->sig_type = IB_SIG_TYPE_T10_DIF; |
| 1393 | domain->sig.dif.bg_type = IB_T10DIF_CRC; |
| 1394 | domain->sig.dif.pi_interval = 1 << bi->interval_exp; |
| 1395 | domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag); |
| 1396 | if (control & NVME_RW_PRINFO_PRCHK_REF) |
| 1397 | domain->sig.dif.ref_remap = true; |
| 1398 | |
| 1399 | domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag); |
| 1400 | domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask); |
| 1401 | domain->sig.dif.app_escape = true; |
| 1402 | if (pi_type == NVME_NS_DPS_PI_TYPE3) |
| 1403 | domain->sig.dif.ref_escape = true; |
| 1404 | } |
| 1405 | |
| 1406 | static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi, |
| 1407 | struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs, |
| 1408 | u8 pi_type) |
| 1409 | { |
| 1410 | u16 control = le16_to_cpu(cmd->rw.control); |
| 1411 | |
| 1412 | memset(sig_attrs, 0, sizeof(*sig_attrs)); |
| 1413 | if (control & NVME_RW_PRINFO_PRACT) { |
| 1414 | /* for WRITE_INSERT/READ_STRIP no memory domain */ |
| 1415 | sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE; |
| 1416 | nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control, |
| 1417 | pi_type); |
| 1418 | /* Clear the PRACT bit since HCA will generate/verify the PI */ |
| 1419 | control &= ~NVME_RW_PRINFO_PRACT; |
| 1420 | cmd->rw.control = cpu_to_le16(control); |
| 1421 | } else { |
| 1422 | /* for WRITE_PASS/READ_PASS both wire/memory domains exist */ |
| 1423 | nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control, |
| 1424 | pi_type); |
| 1425 | nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control, |
| 1426 | pi_type); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask) |
| 1431 | { |
| 1432 | *mask = 0; |
| 1433 | if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF) |
| 1434 | *mask |= IB_SIG_CHECK_REFTAG; |
| 1435 | if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD) |
| 1436 | *mask |= IB_SIG_CHECK_GUARD; |
| 1437 | } |
| 1438 | |
| 1439 | static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1440 | { |
| 1441 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1442 | nvme_rdma_wr_error(cq, wc, "SIG"); |
| 1443 | } |
| 1444 | |
| 1445 | static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue, |
| 1446 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 1447 | int count, int pi_count) |
| 1448 | { |
| 1449 | struct nvme_rdma_sgl *sgl = &req->data_sgl; |
| 1450 | struct ib_reg_wr *wr = &req->reg_wr; |
| 1451 | struct request *rq = blk_mq_rq_from_pdu(req); |
| 1452 | struct nvme_ns *ns = rq->q->queuedata; |
| 1453 | struct bio *bio = rq->bio; |
| 1454 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1455 | int nr; |
| 1456 | |
| 1457 | req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs); |
| 1458 | if (WARN_ON_ONCE(!req->mr)) |
| 1459 | return -EAGAIN; |
| 1460 | |
| 1461 | nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL, |
| 1462 | req->metadata_sgl->sg_table.sgl, pi_count, NULL, |
| 1463 | SZ_4K); |
| 1464 | if (unlikely(nr)) |
| 1465 | goto mr_put; |
| 1466 | |
| 1467 | nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_disk), c, |
| 1468 | req->mr->sig_attrs, ns->pi_type); |
| 1469 | nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask); |
| 1470 | |
| 1471 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); |
| 1472 | |
| 1473 | req->reg_cqe.done = nvme_rdma_sig_done; |
| 1474 | memset(wr, 0, sizeof(*wr)); |
| 1475 | wr->wr.opcode = IB_WR_REG_MR_INTEGRITY; |
| 1476 | wr->wr.wr_cqe = &req->reg_cqe; |
| 1477 | wr->wr.num_sge = 0; |
| 1478 | wr->wr.send_flags = 0; |
| 1479 | wr->mr = req->mr; |
| 1480 | wr->key = req->mr->rkey; |
| 1481 | wr->access = IB_ACCESS_LOCAL_WRITE | |
| 1482 | IB_ACCESS_REMOTE_READ | |
| 1483 | IB_ACCESS_REMOTE_WRITE; |
| 1484 | |
| 1485 | sg->addr = cpu_to_le64(req->mr->iova); |
| 1486 | put_unaligned_le24(req->mr->length, sg->length); |
| 1487 | put_unaligned_le32(req->mr->rkey, sg->key); |
| 1488 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 1489 | |
| 1490 | return 0; |
| 1491 | |
| 1492 | mr_put: |
| 1493 | ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr); |
| 1494 | req->mr = NULL; |
| 1495 | if (nr < 0) |
| 1496 | return nr; |
| 1497 | return -EINVAL; |
| 1498 | } |
| 1499 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1500 | static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1501 | struct request *rq, struct nvme_command *c) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1502 | { |
| 1503 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1504 | struct nvme_rdma_device *dev = queue->device; |
| 1505 | struct ib_device *ibdev = dev->dev; |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1506 | int pi_count = 0; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1507 | int count, ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1508 | |
| 1509 | req->num_sge = 1; |
Sagi Grimberg | 4af7f7f | 2017-11-23 17:35:22 +0200 | [diff] [blame] | 1510 | refcount_set(&req->ref, 2); /* send and recv completions */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1511 | |
| 1512 | c->common.flags |= NVME_CMD_SGL_METABUF; |
| 1513 | |
Chaitanya Kulkarni | 34e0819 | 2019-02-20 20:13:34 -0800 | [diff] [blame] | 1514 | if (!blk_rq_nr_phys_segments(rq)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1515 | return nvme_rdma_set_sg_null(c); |
| 1516 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1517 | req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1); |
| 1518 | ret = sg_alloc_table_chained(&req->data_sgl.sg_table, |
| 1519 | blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl, |
Israel Rukshin | 38e1800 | 2019-11-24 18:38:30 +0200 | [diff] [blame] | 1520 | NVME_INLINE_SG_CNT); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1521 | if (ret) |
| 1522 | return -ENOMEM; |
| 1523 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1524 | req->data_sgl.nents = blk_rq_map_sg(rq->q, rq, |
| 1525 | req->data_sgl.sg_table.sgl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1526 | |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1527 | count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl, |
| 1528 | req->data_sgl.nents, rq_dma_dir(rq)); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1529 | if (unlikely(count <= 0)) { |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1530 | ret = -EIO; |
| 1531 | goto out_free_table; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1532 | } |
| 1533 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1534 | if (blk_integrity_rq(rq)) { |
| 1535 | req->metadata_sgl->sg_table.sgl = |
| 1536 | (struct scatterlist *)(req->metadata_sgl + 1); |
| 1537 | ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table, |
| 1538 | blk_rq_count_integrity_sg(rq->q, rq->bio), |
| 1539 | req->metadata_sgl->sg_table.sgl, |
| 1540 | NVME_INLINE_METADATA_SG_CNT); |
| 1541 | if (unlikely(ret)) { |
| 1542 | ret = -ENOMEM; |
| 1543 | goto out_unmap_sg; |
| 1544 | } |
| 1545 | |
| 1546 | req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q, |
| 1547 | rq->bio, req->metadata_sgl->sg_table.sgl); |
| 1548 | pi_count = ib_dma_map_sg(ibdev, |
| 1549 | req->metadata_sgl->sg_table.sgl, |
| 1550 | req->metadata_sgl->nents, |
| 1551 | rq_dma_dir(rq)); |
| 1552 | if (unlikely(pi_count <= 0)) { |
| 1553 | ret = -EIO; |
| 1554 | goto out_free_pi_table; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | if (req->use_sig_mr) { |
| 1559 | ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count); |
| 1560 | goto out; |
| 1561 | } |
| 1562 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1563 | if (count <= dev->num_inline_segments) { |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1564 | if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1565 | queue->ctrl->use_inline_data && |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1566 | blk_rq_payload_bytes(rq) <= |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1567 | nvme_rdma_inline_data_size(queue)) { |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1568 | ret = nvme_rdma_map_sg_inline(queue, req, c, count); |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1569 | goto out; |
| 1570 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1571 | |
Steve Wise | 64a741c | 2018-06-20 07:15:05 -0700 | [diff] [blame] | 1572 | if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) { |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1573 | ret = nvme_rdma_map_sg_single(queue, req, c); |
| 1574 | goto out; |
| 1575 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1578 | ret = nvme_rdma_map_sg_fr(queue, req, c, count); |
| 1579 | out: |
| 1580 | if (unlikely(ret)) |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1581 | goto out_unmap_pi_sg; |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1582 | |
| 1583 | return 0; |
| 1584 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 1585 | out_unmap_pi_sg: |
| 1586 | if (blk_integrity_rq(rq)) |
| 1587 | ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl, |
| 1588 | req->metadata_sgl->nents, rq_dma_dir(rq)); |
| 1589 | out_free_pi_table: |
| 1590 | if (blk_integrity_rq(rq)) |
| 1591 | sg_free_table_chained(&req->metadata_sgl->sg_table, |
| 1592 | NVME_INLINE_METADATA_SG_CNT); |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1593 | out_unmap_sg: |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1594 | ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents, |
| 1595 | rq_dma_dir(rq)); |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1596 | out_free_table: |
Israel Rukshin | 324d9e7 | 2020-05-19 17:05:55 +0300 | [diff] [blame] | 1597 | sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT); |
Max Gurtovoy | 94423a8 | 2018-06-10 16:58:29 +0300 | [diff] [blame] | 1598 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1602 | { |
Sagi Grimberg | 4af7f7f | 2017-11-23 17:35:22 +0200 | [diff] [blame] | 1603 | struct nvme_rdma_qe *qe = |
| 1604 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); |
| 1605 | struct nvme_rdma_request *req = |
| 1606 | container_of(qe, struct nvme_rdma_request, sqe); |
Sagi Grimberg | 4af7f7f | 2017-11-23 17:35:22 +0200 | [diff] [blame] | 1607 | |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1608 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1609 | nvme_rdma_wr_error(cq, wc, "SEND"); |
Christoph Hellwig | 8446546 | 2020-06-11 08:44:51 +0200 | [diff] [blame] | 1610 | else |
| 1611 | nvme_rdma_end_request(req); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, |
| 1615 | struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 1616 | struct ib_send_wr *first) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1617 | { |
Bart Van Assche | 45e3cc1a | 2018-07-18 09:25:23 -0700 | [diff] [blame] | 1618 | struct ib_send_wr wr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1619 | int ret; |
| 1620 | |
| 1621 | sge->addr = qe->dma; |
Israel Rukshin | a62315b | 2020-03-31 15:46:33 +0300 | [diff] [blame] | 1622 | sge->length = sizeof(struct nvme_command); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1623 | sge->lkey = queue->device->pd->local_dma_lkey; |
| 1624 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1625 | wr.next = NULL; |
| 1626 | wr.wr_cqe = &qe->cqe; |
| 1627 | wr.sg_list = sge; |
| 1628 | wr.num_sge = num_sge; |
| 1629 | wr.opcode = IB_WR_SEND; |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 1630 | wr.send_flags = IB_SEND_SIGNALED; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1631 | |
| 1632 | if (first) |
| 1633 | first->next = ≀ |
| 1634 | else |
| 1635 | first = ≀ |
| 1636 | |
Bart Van Assche | 45e3cc1a | 2018-07-18 09:25:23 -0700 | [diff] [blame] | 1637 | ret = ib_post_send(queue->qp, first, NULL); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1638 | if (unlikely(ret)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1639 | dev_err(queue->ctrl->ctrl.device, |
| 1640 | "%s failed with error code %d\n", __func__, ret); |
| 1641 | } |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, |
| 1646 | struct nvme_rdma_qe *qe) |
| 1647 | { |
Bart Van Assche | 45e3cc1a | 2018-07-18 09:25:23 -0700 | [diff] [blame] | 1648 | struct ib_recv_wr wr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1649 | struct ib_sge list; |
| 1650 | int ret; |
| 1651 | |
| 1652 | list.addr = qe->dma; |
| 1653 | list.length = sizeof(struct nvme_completion); |
| 1654 | list.lkey = queue->device->pd->local_dma_lkey; |
| 1655 | |
| 1656 | qe->cqe.done = nvme_rdma_recv_done; |
| 1657 | |
| 1658 | wr.next = NULL; |
| 1659 | wr.wr_cqe = &qe->cqe; |
| 1660 | wr.sg_list = &list; |
| 1661 | wr.num_sge = 1; |
| 1662 | |
Bart Van Assche | 45e3cc1a | 2018-07-18 09:25:23 -0700 | [diff] [blame] | 1663 | ret = ib_post_recv(queue->qp, &wr, NULL); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1664 | if (unlikely(ret)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1665 | dev_err(queue->ctrl->ctrl.device, |
| 1666 | "%s failed with error code %d\n", __func__, ret); |
| 1667 | } |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) |
| 1672 | { |
| 1673 | u32 queue_idx = nvme_rdma_queue_idx(queue); |
| 1674 | |
| 1675 | if (queue_idx == 0) |
| 1676 | return queue->ctrl->admin_tag_set.tags[queue_idx]; |
| 1677 | return queue->ctrl->tag_set.tags[queue_idx - 1]; |
| 1678 | } |
| 1679 | |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 1680 | static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1681 | { |
| 1682 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1683 | nvme_rdma_wr_error(cq, wc, "ASYNC"); |
| 1684 | } |
| 1685 | |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 1686 | static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1687 | { |
| 1688 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); |
| 1689 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 1690 | struct ib_device *dev = queue->device->dev; |
| 1691 | struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; |
| 1692 | struct nvme_command *cmd = sqe->data; |
| 1693 | struct ib_sge sge; |
| 1694 | int ret; |
| 1695 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1696 | ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); |
| 1697 | |
| 1698 | memset(cmd, 0, sizeof(*cmd)); |
| 1699 | cmd->common.opcode = nvme_admin_async_event; |
Keith Busch | 38dabe2 | 2017-11-07 15:13:10 -0700 | [diff] [blame] | 1700 | cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1701 | cmd->common.flags |= NVME_CMD_SGL_METABUF; |
| 1702 | nvme_rdma_set_sg_null(cmd); |
| 1703 | |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 1704 | sqe->cqe.done = nvme_rdma_async_done; |
| 1705 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1706 | ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), |
| 1707 | DMA_TO_DEVICE); |
| 1708 | |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 1709 | ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1710 | WARN_ON_ONCE(ret); |
| 1711 | } |
| 1712 | |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1713 | static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, |
| 1714 | struct nvme_completion *cqe, struct ib_wc *wc) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1715 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1716 | struct request *rq; |
| 1717 | struct nvme_rdma_request *req; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1718 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1719 | rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); |
| 1720 | if (!rq) { |
| 1721 | dev_err(queue->ctrl->ctrl.device, |
| 1722 | "tag 0x%x on QP %#x not found\n", |
| 1723 | cqe->command_id, queue->qp->qp_num); |
| 1724 | nvme_rdma_error_recovery(queue->ctrl); |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1725 | return; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1726 | } |
| 1727 | req = blk_mq_rq_to_pdu(rq); |
| 1728 | |
Sagi Grimberg | 4af7f7f | 2017-11-23 17:35:22 +0200 | [diff] [blame] | 1729 | req->status = cqe->status; |
| 1730 | req->result = cqe->result; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1731 | |
Sagi Grimberg | 3ef0279 | 2017-11-23 17:35:24 +0200 | [diff] [blame] | 1732 | if (wc->wc_flags & IB_WC_WITH_INVALIDATE) { |
Chao Leng | a87da50 | 2020-10-12 16:55:37 +0800 | [diff] [blame] | 1733 | if (unlikely(!req->mr || |
| 1734 | wc->ex.invalidate_rkey != req->mr->rkey)) { |
Sagi Grimberg | 3ef0279 | 2017-11-23 17:35:24 +0200 | [diff] [blame] | 1735 | dev_err(queue->ctrl->ctrl.device, |
| 1736 | "Bogus remote invalidation for rkey %#x\n", |
Chao Leng | a87da50 | 2020-10-12 16:55:37 +0800 | [diff] [blame] | 1737 | req->mr ? req->mr->rkey : 0); |
Sagi Grimberg | 3ef0279 | 2017-11-23 17:35:24 +0200 | [diff] [blame] | 1738 | nvme_rdma_error_recovery(queue->ctrl); |
| 1739 | } |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 1740 | } else if (req->mr) { |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1741 | int ret; |
| 1742 | |
Sagi Grimberg | 2f122e4 | 2017-11-23 17:35:23 +0200 | [diff] [blame] | 1743 | ret = nvme_rdma_inv_rkey(queue, req); |
| 1744 | if (unlikely(ret < 0)) { |
| 1745 | dev_err(queue->ctrl->ctrl.device, |
| 1746 | "Queueing INV WR for rkey %#x failed (%d)\n", |
| 1747 | req->mr->rkey, ret); |
| 1748 | nvme_rdma_error_recovery(queue->ctrl); |
| 1749 | } |
| 1750 | /* the local invalidation completion will end the request */ |
Christoph Hellwig | 7a804c3 | 2020-06-23 18:22:39 +0200 | [diff] [blame] | 1751 | return; |
Sagi Grimberg | 2f122e4 | 2017-11-23 17:35:23 +0200 | [diff] [blame] | 1752 | } |
Christoph Hellwig | 7a804c3 | 2020-06-23 18:22:39 +0200 | [diff] [blame] | 1753 | |
| 1754 | nvme_rdma_end_request(req); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1755 | } |
| 1756 | |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1757 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1758 | { |
| 1759 | struct nvme_rdma_qe *qe = |
| 1760 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); |
Yamin Friedman | 287f329 | 2020-07-13 11:53:29 +0300 | [diff] [blame] | 1761 | struct nvme_rdma_queue *queue = wc->qp->qp_context; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1762 | struct ib_device *ibdev = queue->device->dev; |
| 1763 | struct nvme_completion *cqe = qe->data; |
| 1764 | const size_t len = sizeof(struct nvme_completion); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1765 | |
| 1766 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1767 | nvme_rdma_wr_error(cq, wc, "RECV"); |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1768 | return; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1769 | } |
| 1770 | |
zhenwei pi | 25c1ca6 | 2020-10-25 19:51:24 +0800 | [diff] [blame^] | 1771 | /* sanity checking for received data length */ |
| 1772 | if (unlikely(wc->byte_len < len)) { |
| 1773 | dev_err(queue->ctrl->ctrl.device, |
| 1774 | "Unexpected nvme completion length(%d)\n", wc->byte_len); |
| 1775 | nvme_rdma_error_recovery(queue->ctrl); |
| 1776 | return; |
| 1777 | } |
| 1778 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1779 | ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1780 | /* |
| 1781 | * AEN requests are special as they don't time out and can |
| 1782 | * survive any kind of queue freeze and often don't respond to |
| 1783 | * aborts. We don't even bother to allocate a struct request |
| 1784 | * for them but rather special case them here. |
| 1785 | */ |
Israel Rukshin | 58a8df6 | 2019-10-13 19:57:31 +0300 | [diff] [blame] | 1786 | if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue), |
| 1787 | cqe->command_id))) |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 1788 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, |
| 1789 | &cqe->result); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1790 | else |
Jens Axboe | 1052b8a | 2018-11-26 08:21:49 -0700 | [diff] [blame] | 1791 | nvme_rdma_process_nvme_rsp(queue, cqe, wc); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1792 | ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1793 | |
| 1794 | nvme_rdma_post_recv(queue, qe); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) |
| 1798 | { |
| 1799 | int ret, i; |
| 1800 | |
| 1801 | for (i = 0; i < queue->queue_size; i++) { |
| 1802 | ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); |
| 1803 | if (ret) |
| 1804 | goto out_destroy_queue_ib; |
| 1805 | } |
| 1806 | |
| 1807 | return 0; |
| 1808 | |
| 1809 | out_destroy_queue_ib: |
| 1810 | nvme_rdma_destroy_queue_ib(queue); |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, |
| 1815 | struct rdma_cm_event *ev) |
| 1816 | { |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1817 | struct rdma_cm_id *cm_id = queue->cm_id; |
| 1818 | int status = ev->status; |
| 1819 | const char *rej_msg; |
| 1820 | const struct nvme_rdma_cm_rej *rej_data; |
| 1821 | u8 rej_data_len; |
| 1822 | |
| 1823 | rej_msg = rdma_reject_msg(cm_id, status); |
| 1824 | rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); |
| 1825 | |
| 1826 | if (rej_data && rej_data_len >= sizeof(u16)) { |
| 1827 | u16 sts = le16_to_cpu(rej_data->sts); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1828 | |
| 1829 | dev_err(queue->ctrl->ctrl.device, |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1830 | "Connect rejected: status %d (%s) nvme status %d (%s).\n", |
| 1831 | status, rej_msg, sts, nvme_rdma_cm_msg(sts)); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1832 | } else { |
| 1833 | dev_err(queue->ctrl->ctrl.device, |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1834 | "Connect rejected: status %d (%s).\n", status, rej_msg); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | return -ECONNRESET; |
| 1838 | } |
| 1839 | |
| 1840 | static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) |
| 1841 | { |
Israel Rukshin | e63440d | 2019-08-18 12:08:52 +0300 | [diff] [blame] | 1842 | struct nvme_ctrl *ctrl = &queue->ctrl->ctrl; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1843 | int ret; |
| 1844 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 1845 | ret = nvme_rdma_create_queue_ib(queue); |
| 1846 | if (ret) |
| 1847 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1848 | |
Israel Rukshin | e63440d | 2019-08-18 12:08:52 +0300 | [diff] [blame] | 1849 | if (ctrl->opts->tos >= 0) |
| 1850 | rdma_set_service_type(queue->cm_id, ctrl->opts->tos); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1851 | ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 1852 | if (ret) { |
Israel Rukshin | e63440d | 2019-08-18 12:08:52 +0300 | [diff] [blame] | 1853 | dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n", |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1854 | queue->cm_error); |
| 1855 | goto out_destroy_queue; |
| 1856 | } |
| 1857 | |
| 1858 | return 0; |
| 1859 | |
| 1860 | out_destroy_queue: |
| 1861 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1862 | return ret; |
| 1863 | } |
| 1864 | |
| 1865 | static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) |
| 1866 | { |
| 1867 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1868 | struct rdma_conn_param param = { }; |
Roland Dreier | 0b857b4 | 2016-07-31 00:27:39 -0700 | [diff] [blame] | 1869 | struct nvme_rdma_cm_req priv = { }; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1870 | int ret; |
| 1871 | |
| 1872 | param.qp_num = queue->qp->qp_num; |
| 1873 | param.flow_control = 1; |
| 1874 | |
| 1875 | param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 1876 | /* maximum retry count */ |
| 1877 | param.retry_count = 7; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1878 | param.rnr_retry_count = 7; |
| 1879 | param.private_data = &priv; |
| 1880 | param.private_data_len = sizeof(priv); |
| 1881 | |
| 1882 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1883 | priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1884 | /* |
| 1885 | * set the admin queue depth to the minimum size |
| 1886 | * specified by the Fabrics standard. |
| 1887 | */ |
| 1888 | if (priv.qid == 0) { |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 1889 | priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH); |
| 1890 | priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1891 | } else { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1892 | /* |
| 1893 | * current interpretation of the fabrics spec |
| 1894 | * is at minimum you make hrqsize sqsize+1, or a |
| 1895 | * 1's based representation of sqsize. |
| 1896 | */ |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1897 | priv.hrqsize = cpu_to_le16(queue->queue_size); |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1898 | priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1899 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1900 | |
| 1901 | ret = rdma_connect(queue->cm_id, ¶m); |
| 1902 | if (ret) { |
| 1903 | dev_err(ctrl->ctrl.device, |
| 1904 | "rdma_connect failed (%d).\n", ret); |
| 1905 | goto out_destroy_queue_ib; |
| 1906 | } |
| 1907 | |
| 1908 | return 0; |
| 1909 | |
| 1910 | out_destroy_queue_ib: |
| 1911 | nvme_rdma_destroy_queue_ib(queue); |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1915 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 1916 | struct rdma_cm_event *ev) |
| 1917 | { |
| 1918 | struct nvme_rdma_queue *queue = cm_id->context; |
| 1919 | int cm_error = 0; |
| 1920 | |
| 1921 | dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", |
| 1922 | rdma_event_msg(ev->event), ev->event, |
| 1923 | ev->status, cm_id); |
| 1924 | |
| 1925 | switch (ev->event) { |
| 1926 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 1927 | cm_error = nvme_rdma_addr_resolved(queue); |
| 1928 | break; |
| 1929 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
| 1930 | cm_error = nvme_rdma_route_resolved(queue); |
| 1931 | break; |
| 1932 | case RDMA_CM_EVENT_ESTABLISHED: |
| 1933 | queue->cm_error = nvme_rdma_conn_established(queue); |
| 1934 | /* complete cm_done regardless of success/failure */ |
| 1935 | complete(&queue->cm_done); |
| 1936 | return 0; |
| 1937 | case RDMA_CM_EVENT_REJECTED: |
| 1938 | cm_error = nvme_rdma_conn_rejected(queue, ev); |
| 1939 | break; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1940 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 1941 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 1942 | case RDMA_CM_EVENT_UNREACHABLE: |
Sagi Grimberg | abf87d5 | 2017-05-04 13:33:10 +0300 | [diff] [blame] | 1943 | nvme_rdma_destroy_queue_ib(queue); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1944 | fallthrough; |
Sagi Grimberg | abf87d5 | 2017-05-04 13:33:10 +0300 | [diff] [blame] | 1945 | case RDMA_CM_EVENT_ADDR_ERROR: |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1946 | dev_dbg(queue->ctrl->ctrl.device, |
| 1947 | "CM error event %d\n", ev->event); |
| 1948 | cm_error = -ECONNRESET; |
| 1949 | break; |
| 1950 | case RDMA_CM_EVENT_DISCONNECTED: |
| 1951 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 1952 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 1953 | dev_dbg(queue->ctrl->ctrl.device, |
| 1954 | "disconnect received - connection closed\n"); |
| 1955 | nvme_rdma_error_recovery(queue->ctrl); |
| 1956 | break; |
| 1957 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 1958 | /* device removal is handled via the ib_client API */ |
| 1959 | break; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1960 | default: |
| 1961 | dev_err(queue->ctrl->ctrl.device, |
| 1962 | "Unexpected RDMA CM event (%d)\n", ev->event); |
| 1963 | nvme_rdma_error_recovery(queue->ctrl); |
| 1964 | break; |
| 1965 | } |
| 1966 | |
| 1967 | if (cm_error) { |
| 1968 | queue->cm_error = cm_error; |
| 1969 | complete(&queue->cm_done); |
| 1970 | } |
| 1971 | |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
Sagi Grimberg | 0475a8d | 2020-07-29 02:36:03 -0700 | [diff] [blame] | 1975 | static void nvme_rdma_complete_timed_out(struct request *rq) |
| 1976 | { |
| 1977 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1978 | struct nvme_rdma_queue *queue = req->queue; |
| 1979 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1980 | |
| 1981 | /* fence other contexts that may complete the command */ |
| 1982 | mutex_lock(&ctrl->teardown_lock); |
| 1983 | nvme_rdma_stop_queue(queue); |
| 1984 | if (!blk_mq_request_completed(rq)) { |
| 1985 | nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD; |
| 1986 | blk_mq_complete_request(rq); |
| 1987 | } |
| 1988 | mutex_unlock(&ctrl->teardown_lock); |
| 1989 | } |
| 1990 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1991 | static enum blk_eh_timer_return |
| 1992 | nvme_rdma_timeout(struct request *rq, bool reserved) |
| 1993 | { |
| 1994 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 1995 | struct nvme_rdma_queue *queue = req->queue; |
| 1996 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1997 | |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 1998 | dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n", |
| 1999 | rq->tag, nvme_rdma_queue_idx(queue)); |
Nitzan Carmi | e62a538 | 2017-10-22 09:37:04 +0000 | [diff] [blame] | 2000 | |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 2001 | if (ctrl->ctrl.state != NVME_CTRL_LIVE) { |
| 2002 | /* |
Sagi Grimberg | 0475a8d | 2020-07-29 02:36:03 -0700 | [diff] [blame] | 2003 | * If we are resetting, connecting or deleting we should |
| 2004 | * complete immediately because we may block controller |
| 2005 | * teardown or setup sequence |
| 2006 | * - ctrl disable/shutdown fabrics requests |
| 2007 | * - connect requests |
| 2008 | * - initialization admin requests |
| 2009 | * - I/O requests that entered after unquiescing and |
| 2010 | * the controller stopped responding |
| 2011 | * |
| 2012 | * All other requests should be cancelled by the error |
| 2013 | * recovery work, so it's fine that we fail it here. |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 2014 | */ |
Sagi Grimberg | 0475a8d | 2020-07-29 02:36:03 -0700 | [diff] [blame] | 2015 | nvme_rdma_complete_timed_out(rq); |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 2016 | return BLK_EH_DONE; |
| 2017 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2018 | |
Sagi Grimberg | 0475a8d | 2020-07-29 02:36:03 -0700 | [diff] [blame] | 2019 | /* |
| 2020 | * LIVE state should trigger the normal error recovery which will |
| 2021 | * handle completing this request. |
| 2022 | */ |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 2023 | nvme_rdma_error_recovery(ctrl); |
Sagi Grimberg | 4c174e6 | 2019-01-08 00:53:22 -0800 | [diff] [blame] | 2024 | return BLK_EH_RESET_TIMER; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2025 | } |
| 2026 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2027 | static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2028 | const struct blk_mq_queue_data *bd) |
| 2029 | { |
| 2030 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 2031 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 2032 | struct request *rq = bd->rq; |
| 2033 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 2034 | struct nvme_rdma_qe *sqe = &req->sqe; |
| 2035 | struct nvme_command *c = sqe->data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2036 | struct ib_device *dev; |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 2037 | bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2038 | blk_status_t ret; |
| 2039 | int err; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2040 | |
| 2041 | WARN_ON_ONCE(rq->tag < 0); |
| 2042 | |
Christoph Hellwig | 3bc32bb | 2018-06-11 17:34:06 +0200 | [diff] [blame] | 2043 | if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) |
James Smart | 6cdefc6 | 2018-07-20 15:49:48 -0700 | [diff] [blame] | 2044 | return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq); |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 2045 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2046 | dev = queue->device->dev; |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2047 | |
| 2048 | req->sqe.dma = ib_dma_map_single(dev, req->sqe.data, |
| 2049 | sizeof(struct nvme_command), |
| 2050 | DMA_TO_DEVICE); |
| 2051 | err = ib_dma_mapping_error(dev, req->sqe.dma); |
| 2052 | if (unlikely(err)) |
| 2053 | return BLK_STS_RESOURCE; |
| 2054 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2055 | ib_dma_sync_single_for_cpu(dev, sqe->dma, |
| 2056 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 2057 | |
| 2058 | ret = nvme_setup_cmd(ns, rq, c); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2059 | if (ret) |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2060 | goto unmap_qe; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2061 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2062 | blk_mq_start_request(rq); |
| 2063 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 2064 | if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) && |
| 2065 | queue->pi_support && |
| 2066 | (c->common.opcode == nvme_cmd_write || |
| 2067 | c->common.opcode == nvme_cmd_read) && |
| 2068 | nvme_ns_has_pi(ns)) |
| 2069 | req->use_sig_mr = true; |
| 2070 | else |
| 2071 | req->use_sig_mr = false; |
| 2072 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2073 | err = nvme_rdma_map_data(queue, rq, c); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 2074 | if (unlikely(err < 0)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2075 | dev_err(queue->ctrl->ctrl.device, |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2076 | "Failed to map data (%d)\n", err); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2077 | goto err; |
| 2078 | } |
| 2079 | |
Sagi Grimberg | b4b591c | 2017-11-23 17:35:21 +0200 | [diff] [blame] | 2080 | sqe->cqe.done = nvme_rdma_send_done; |
| 2081 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2082 | ib_dma_sync_single_for_device(dev, sqe->dma, |
| 2083 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 2084 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2085 | err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, |
Israel Rukshin | f41725b | 2017-11-26 10:40:55 +0000 | [diff] [blame] | 2086 | req->mr ? &req->reg_wr.wr : NULL); |
Max Gurtovoy | 16686f3 | 2019-10-13 19:57:36 +0300 | [diff] [blame] | 2087 | if (unlikely(err)) |
| 2088 | goto err_unmap; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2089 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2090 | return BLK_STS_OK; |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2091 | |
Max Gurtovoy | 16686f3 | 2019-10-13 19:57:36 +0300 | [diff] [blame] | 2092 | err_unmap: |
| 2093 | nvme_rdma_unmap_data(queue, rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2094 | err: |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2095 | if (err == -ENOMEM || err == -EAGAIN) |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2096 | ret = BLK_STS_RESOURCE; |
| 2097 | else |
| 2098 | ret = BLK_STS_IOERR; |
Max Gurtovoy | 16686f3 | 2019-10-13 19:57:36 +0300 | [diff] [blame] | 2099 | nvme_cleanup_cmd(rq); |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2100 | unmap_qe: |
| 2101 | ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command), |
| 2102 | DMA_TO_DEVICE); |
| 2103 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2104 | } |
| 2105 | |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2106 | static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx) |
| 2107 | { |
| 2108 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 2109 | |
| 2110 | return ib_process_cq_direct(queue->ib_cq, -1); |
| 2111 | } |
| 2112 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 2113 | static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req) |
| 2114 | { |
| 2115 | struct request *rq = blk_mq_rq_from_pdu(req); |
| 2116 | struct ib_mr_status mr_status; |
| 2117 | int ret; |
| 2118 | |
| 2119 | ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status); |
| 2120 | if (ret) { |
| 2121 | pr_err("ib_check_mr_status failed, ret %d\n", ret); |
| 2122 | nvme_req(rq)->status = NVME_SC_INVALID_PI; |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { |
| 2127 | switch (mr_status.sig_err.err_type) { |
| 2128 | case IB_SIG_BAD_GUARD: |
| 2129 | nvme_req(rq)->status = NVME_SC_GUARD_CHECK; |
| 2130 | break; |
| 2131 | case IB_SIG_BAD_REFTAG: |
| 2132 | nvme_req(rq)->status = NVME_SC_REFTAG_CHECK; |
| 2133 | break; |
| 2134 | case IB_SIG_BAD_APPTAG: |
| 2135 | nvme_req(rq)->status = NVME_SC_APPTAG_CHECK; |
| 2136 | break; |
| 2137 | } |
| 2138 | pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n", |
| 2139 | mr_status.sig_err.err_type, mr_status.sig_err.expected, |
| 2140 | mr_status.sig_err.actual); |
| 2141 | } |
| 2142 | } |
| 2143 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2144 | static void nvme_rdma_complete_rq(struct request *rq) |
| 2145 | { |
| 2146 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2147 | struct nvme_rdma_queue *queue = req->queue; |
| 2148 | struct ib_device *ibdev = queue->device->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2149 | |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 2150 | if (req->use_sig_mr) |
| 2151 | nvme_rdma_check_pi_status(req); |
| 2152 | |
Max Gurtovoy | 62f99b6 | 2019-06-06 12:27:36 +0300 | [diff] [blame] | 2153 | nvme_rdma_unmap_data(queue, rq); |
| 2154 | ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command), |
| 2155 | DMA_TO_DEVICE); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 2156 | nvme_complete_rq(rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2157 | } |
| 2158 | |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 2159 | static int nvme_rdma_map_queues(struct blk_mq_tag_set *set) |
| 2160 | { |
| 2161 | struct nvme_rdma_ctrl *ctrl = set->driver_data; |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2162 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 2163 | |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2164 | if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) { |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2165 | /* separate read/write queues */ |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2166 | set->map[HCTX_TYPE_DEFAULT].nr_queues = |
| 2167 | ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
| 2168 | set->map[HCTX_TYPE_DEFAULT].queue_offset = 0; |
| 2169 | set->map[HCTX_TYPE_READ].nr_queues = |
| 2170 | ctrl->io_queues[HCTX_TYPE_READ]; |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2171 | set->map[HCTX_TYPE_READ].queue_offset = |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2172 | ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2173 | } else { |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2174 | /* shared read/write queues */ |
| 2175 | set->map[HCTX_TYPE_DEFAULT].nr_queues = |
| 2176 | ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
| 2177 | set->map[HCTX_TYPE_DEFAULT].queue_offset = 0; |
| 2178 | set->map[HCTX_TYPE_READ].nr_queues = |
| 2179 | ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2180 | set->map[HCTX_TYPE_READ].queue_offset = 0; |
| 2181 | } |
| 2182 | blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT], |
| 2183 | ctrl->device->dev, 0); |
| 2184 | blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ], |
| 2185 | ctrl->device->dev, 0); |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2186 | |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2187 | if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) { |
| 2188 | /* map dedicated poll queues only if we have queues left */ |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2189 | set->map[HCTX_TYPE_POLL].nr_queues = |
Sagi Grimberg | b1064d3 | 2019-01-18 16:43:24 -0800 | [diff] [blame] | 2190 | ctrl->io_queues[HCTX_TYPE_POLL]; |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2191 | set->map[HCTX_TYPE_POLL].queue_offset = |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2192 | ctrl->io_queues[HCTX_TYPE_DEFAULT] + |
| 2193 | ctrl->io_queues[HCTX_TYPE_READ]; |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2194 | blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]); |
| 2195 | } |
Sagi Grimberg | 5651cd3 | 2019-05-28 22:49:04 -0700 | [diff] [blame] | 2196 | |
| 2197 | dev_info(ctrl->ctrl.device, |
| 2198 | "mapped %d/%d/%d default/read/poll queues.\n", |
| 2199 | ctrl->io_queues[HCTX_TYPE_DEFAULT], |
| 2200 | ctrl->io_queues[HCTX_TYPE_READ], |
| 2201 | ctrl->io_queues[HCTX_TYPE_POLL]); |
| 2202 | |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2203 | return 0; |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 2204 | } |
| 2205 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 2206 | static const struct blk_mq_ops nvme_rdma_mq_ops = { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2207 | .queue_rq = nvme_rdma_queue_rq, |
| 2208 | .complete = nvme_rdma_complete_rq, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2209 | .init_request = nvme_rdma_init_request, |
| 2210 | .exit_request = nvme_rdma_exit_request, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2211 | .init_hctx = nvme_rdma_init_hctx, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2212 | .timeout = nvme_rdma_timeout, |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 2213 | .map_queues = nvme_rdma_map_queues, |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2214 | .poll = nvme_rdma_poll, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2215 | }; |
| 2216 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 2217 | static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2218 | .queue_rq = nvme_rdma_queue_rq, |
| 2219 | .complete = nvme_rdma_complete_rq, |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 2220 | .init_request = nvme_rdma_init_request, |
| 2221 | .exit_request = nvme_rdma_exit_request, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2222 | .init_hctx = nvme_rdma_init_admin_hctx, |
| 2223 | .timeout = nvme_rdma_timeout, |
| 2224 | }; |
| 2225 | |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 2226 | static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2227 | { |
Sagi Grimberg | 794a4cb | 2019-01-01 00:19:30 -0800 | [diff] [blame] | 2228 | cancel_work_sync(&ctrl->err_work); |
| 2229 | cancel_delayed_work_sync(&ctrl->reconnect_work); |
| 2230 | |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 2231 | nvme_rdma_teardown_io_queues(ctrl, shutdown); |
Sagi Grimberg | e7832cb | 2019-08-02 19:33:59 -0700 | [diff] [blame] | 2232 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 2233 | if (shutdown) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2234 | nvme_shutdown_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 2235 | else |
Sagi Grimberg | b5b0504 | 2019-07-22 17:06:54 -0700 | [diff] [blame] | 2236 | nvme_disable_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 75862c7 | 2018-07-09 12:49:07 +0300 | [diff] [blame] | 2237 | nvme_rdma_teardown_admin_queue(ctrl, shutdown); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2238 | } |
| 2239 | |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2240 | static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 2461a8d | 2016-07-24 09:29:51 +0300 | [diff] [blame] | 2241 | { |
Christoph Hellwig | e9bc258 | 2017-10-29 10:44:30 +0200 | [diff] [blame] | 2242 | nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2243 | } |
| 2244 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2245 | static void nvme_rdma_reset_ctrl_work(struct work_struct *work) |
| 2246 | { |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2247 | struct nvme_rdma_ctrl *ctrl = |
| 2248 | container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2249 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 2250 | nvme_stop_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 2251 | nvme_rdma_shutdown_ctrl(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2252 | |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 2253 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { |
Sagi Grimberg | d5bf4b7 | 2017-12-21 14:54:15 +0200 | [diff] [blame] | 2254 | /* state change failure should never happen */ |
| 2255 | WARN_ON_ONCE(1); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 2259 | if (nvme_rdma_setup_ctrl(ctrl, false)) |
Sagi Grimberg | 370ae6e | 2017-07-10 09:22:38 +0300 | [diff] [blame] | 2260 | goto out_fail; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2261 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2262 | return; |
| 2263 | |
Sagi Grimberg | 370ae6e | 2017-07-10 09:22:38 +0300 | [diff] [blame] | 2264 | out_fail: |
Nitzan Carmi | 8000d1f | 2018-01-17 11:01:14 +0000 | [diff] [blame] | 2265 | ++ctrl->ctrl.nr_reconnects; |
| 2266 | nvme_rdma_reconnect_or_remove(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2267 | } |
| 2268 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2269 | static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { |
| 2270 | .name = "rdma", |
| 2271 | .module = THIS_MODULE, |
Max Gurtovoy | 5ec5d3b | 2020-05-19 17:05:56 +0300 | [diff] [blame] | 2272 | .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2273 | .reg_read32 = nvmf_reg_read32, |
| 2274 | .reg_read64 = nvmf_reg_read64, |
| 2275 | .reg_write32 = nvmf_reg_write32, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2276 | .free_ctrl = nvme_rdma_free_ctrl, |
| 2277 | .submit_async_event = nvme_rdma_submit_async_event, |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2278 | .delete_ctrl = nvme_rdma_delete_ctrl, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2279 | .get_address = nvmf_get_address, |
| 2280 | }; |
| 2281 | |
James Smart | 36e835f | 2017-10-20 16:17:09 -0700 | [diff] [blame] | 2282 | /* |
| 2283 | * Fails a connection request if it matches an existing controller |
| 2284 | * (association) with the same tuple: |
| 2285 | * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN> |
| 2286 | * |
| 2287 | * if local address is not specified in the request, it will match an |
| 2288 | * existing controller with all the other parameters the same and no |
| 2289 | * local port address specified as well. |
| 2290 | * |
| 2291 | * The ports don't need to be compared as they are intrinsically |
| 2292 | * already matched by the port pointers supplied. |
| 2293 | */ |
| 2294 | static bool |
| 2295 | nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts) |
| 2296 | { |
| 2297 | struct nvme_rdma_ctrl *ctrl; |
| 2298 | bool found = false; |
| 2299 | |
| 2300 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2301 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { |
Sagi Grimberg | b7c7be6f6 | 2018-10-18 17:40:40 -0700 | [diff] [blame] | 2302 | found = nvmf_ip_options_match(&ctrl->ctrl, opts); |
James Smart | 36e835f | 2017-10-20 16:17:09 -0700 | [diff] [blame] | 2303 | if (found) |
| 2304 | break; |
| 2305 | } |
| 2306 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2307 | |
| 2308 | return found; |
| 2309 | } |
| 2310 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2311 | static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, |
| 2312 | struct nvmf_ctrl_options *opts) |
| 2313 | { |
| 2314 | struct nvme_rdma_ctrl *ctrl; |
| 2315 | int ret; |
| 2316 | bool changed; |
| 2317 | |
| 2318 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 2319 | if (!ctrl) |
| 2320 | return ERR_PTR(-ENOMEM); |
| 2321 | ctrl->ctrl.opts = opts; |
| 2322 | INIT_LIST_HEAD(&ctrl->list); |
Sagi Grimberg | 5110f40 | 2020-08-05 18:13:58 -0700 | [diff] [blame] | 2323 | mutex_init(&ctrl->teardown_lock); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2324 | |
Sagi Grimberg | bb59b8e | 2018-10-19 00:50:29 -0700 | [diff] [blame] | 2325 | if (!(opts->mask & NVMF_OPT_TRSVCID)) { |
| 2326 | opts->trsvcid = |
| 2327 | kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL); |
| 2328 | if (!opts->trsvcid) { |
| 2329 | ret = -ENOMEM; |
| 2330 | goto out_free_ctrl; |
| 2331 | } |
| 2332 | opts->mask |= NVMF_OPT_TRSVCID; |
| 2333 | } |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 2334 | |
| 2335 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
Sagi Grimberg | bb59b8e | 2018-10-19 00:50:29 -0700 | [diff] [blame] | 2336 | opts->traddr, opts->trsvcid, &ctrl->addr); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2337 | if (ret) { |
Sagi Grimberg | bb59b8e | 2018-10-19 00:50:29 -0700 | [diff] [blame] | 2338 | pr_err("malformed address passed: %s:%s\n", |
| 2339 | opts->traddr, opts->trsvcid); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2340 | goto out_free_ctrl; |
| 2341 | } |
| 2342 | |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 2343 | if (opts->mask & NVMF_OPT_HOST_TRADDR) { |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 2344 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
| 2345 | opts->host_traddr, NULL, &ctrl->src_addr); |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 2346 | if (ret) { |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 2347 | pr_err("malformed src address passed: %s\n", |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 2348 | opts->host_traddr); |
| 2349 | goto out_free_ctrl; |
| 2350 | } |
| 2351 | } |
| 2352 | |
James Smart | 36e835f | 2017-10-20 16:17:09 -0700 | [diff] [blame] | 2353 | if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) { |
| 2354 | ret = -EALREADY; |
| 2355 | goto out_free_ctrl; |
| 2356 | } |
| 2357 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2358 | INIT_DELAYED_WORK(&ctrl->reconnect_work, |
| 2359 | nvme_rdma_reconnect_ctrl_work); |
| 2360 | INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2361 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2362 | |
Sagi Grimberg | ff8519f | 2018-12-14 11:06:10 -0800 | [diff] [blame] | 2363 | ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + |
| 2364 | opts->nr_poll_queues + 1; |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 2365 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2366 | ctrl->ctrl.kato = opts->kato; |
| 2367 | |
| 2368 | ret = -ENOMEM; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2369 | ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2370 | GFP_KERNEL); |
| 2371 | if (!ctrl->queues) |
Sagi Grimberg | 3d06410 | 2018-06-19 15:34:09 +0300 | [diff] [blame] | 2372 | goto out_free_ctrl; |
| 2373 | |
| 2374 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, |
| 2375 | 0 /* no quirks, we're perfect! */); |
| 2376 | if (ret) |
| 2377 | goto out_kfree_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2378 | |
Max Gurtovoy | b754a32 | 2018-01-31 18:31:25 +0200 | [diff] [blame] | 2379 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING); |
| 2380 | WARN_ON_ONCE(!changed); |
| 2381 | |
Sagi Grimberg | c66e299 | 2018-07-09 12:49:06 +0300 | [diff] [blame] | 2382 | ret = nvme_rdma_setup_ctrl(ctrl, true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2383 | if (ret) |
Sagi Grimberg | 3d06410 | 2018-06-19 15:34:09 +0300 | [diff] [blame] | 2384 | goto out_uninit_ctrl; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2385 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 2386 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n", |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2387 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
| 2388 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2389 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2390 | list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); |
| 2391 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2392 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2393 | return &ctrl->ctrl; |
| 2394 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2395 | out_uninit_ctrl: |
| 2396 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 2397 | nvme_put_ctrl(&ctrl->ctrl); |
| 2398 | if (ret > 0) |
| 2399 | ret = -EIO; |
| 2400 | return ERR_PTR(ret); |
Sagi Grimberg | 3d06410 | 2018-06-19 15:34:09 +0300 | [diff] [blame] | 2401 | out_kfree_queues: |
| 2402 | kfree(ctrl->queues); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2403 | out_free_ctrl: |
| 2404 | kfree(ctrl); |
| 2405 | return ERR_PTR(ret); |
| 2406 | } |
| 2407 | |
| 2408 | static struct nvmf_transport_ops nvme_rdma_transport = { |
| 2409 | .name = "rdma", |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 2410 | .module = THIS_MODULE, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2411 | .required_opts = NVMF_OPT_TRADDR, |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 2412 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | |
Sagi Grimberg | b65bb77 | 2018-12-11 23:38:58 -0800 | [diff] [blame] | 2413 | NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | |
Israel Rukshin | e63440d | 2019-08-18 12:08:52 +0300 | [diff] [blame] | 2414 | NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | |
| 2415 | NVMF_OPT_TOS, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2416 | .create_ctrl = nvme_rdma_create_ctrl, |
| 2417 | }; |
| 2418 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2419 | static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) |
| 2420 | { |
| 2421 | struct nvme_rdma_ctrl *ctrl; |
Max Gurtovoy | 9bad040 | 2018-02-28 13:12:39 +0200 | [diff] [blame] | 2422 | struct nvme_rdma_device *ndev; |
| 2423 | bool found = false; |
| 2424 | |
| 2425 | mutex_lock(&device_list_mutex); |
| 2426 | list_for_each_entry(ndev, &device_list, entry) { |
| 2427 | if (ndev->dev == ib_device) { |
| 2428 | found = true; |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | mutex_unlock(&device_list_mutex); |
| 2433 | |
| 2434 | if (!found) |
| 2435 | return; |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2436 | |
| 2437 | /* Delete all controllers using this device */ |
| 2438 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2439 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { |
| 2440 | if (ctrl->device->dev != ib_device) |
| 2441 | continue; |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2442 | nvme_delete_ctrl(&ctrl->ctrl); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2443 | } |
| 2444 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2445 | |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 2446 | flush_workqueue(nvme_delete_wq); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | static struct ib_client nvme_rdma_ib_client = { |
| 2450 | .name = "nvme_rdma", |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2451 | .remove = nvme_rdma_remove_one |
| 2452 | }; |
| 2453 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2454 | static int __init nvme_rdma_init_module(void) |
| 2455 | { |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2456 | int ret; |
| 2457 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2458 | ret = ib_register_client(&nvme_rdma_ib_client); |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2459 | if (ret) |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 2460 | return ret; |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2461 | |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2462 | ret = nvmf_register_transport(&nvme_rdma_transport); |
| 2463 | if (ret) |
| 2464 | goto err_unreg_client; |
| 2465 | |
| 2466 | return 0; |
| 2467 | |
| 2468 | err_unreg_client: |
| 2469 | ib_unregister_client(&nvme_rdma_ib_client); |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2470 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static void __exit nvme_rdma_cleanup_module(void) |
| 2474 | { |
Max Gurtovoy | 9ad9e8d6 | 2019-10-29 16:42:27 +0200 | [diff] [blame] | 2475 | struct nvme_rdma_ctrl *ctrl; |
| 2476 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2477 | nvmf_unregister_transport(&nvme_rdma_transport); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2478 | ib_unregister_client(&nvme_rdma_ib_client); |
Max Gurtovoy | 9ad9e8d6 | 2019-10-29 16:42:27 +0200 | [diff] [blame] | 2479 | |
| 2480 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2481 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) |
| 2482 | nvme_delete_ctrl(&ctrl->ctrl); |
| 2483 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2484 | flush_workqueue(nvme_delete_wq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | module_init(nvme_rdma_init_module); |
| 2488 | module_exit(nvme_rdma_cleanup_module); |
| 2489 | |
| 2490 | MODULE_LICENSE("GPL v2"); |