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