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