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