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> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/string.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 20 | #include <linux/atomic.h> |
| 21 | #include <linux/blk-mq.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/scatterlist.h> |
| 26 | #include <linux/nvme.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
| 28 | |
| 29 | #include <rdma/ib_verbs.h> |
| 30 | #include <rdma/rdma_cm.h> |
| 31 | #include <rdma/ib_cm.h> |
| 32 | #include <linux/nvme-rdma.h> |
| 33 | |
| 34 | #include "nvme.h" |
| 35 | #include "fabrics.h" |
| 36 | |
| 37 | |
| 38 | #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */ |
| 39 | |
| 40 | #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */ |
| 41 | |
| 42 | #define NVME_RDMA_MAX_SEGMENTS 256 |
| 43 | |
| 44 | #define NVME_RDMA_MAX_INLINE_SEGMENTS 1 |
| 45 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 46 | /* |
| 47 | * We handle AEN commands ourselves and don't even let the |
| 48 | * block layer know about them. |
| 49 | */ |
| 50 | #define NVME_RDMA_NR_AEN_COMMANDS 1 |
| 51 | #define NVME_RDMA_AQ_BLKMQ_DEPTH \ |
| 52 | (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS) |
| 53 | |
| 54 | struct nvme_rdma_device { |
| 55 | struct ib_device *dev; |
| 56 | struct ib_pd *pd; |
| 57 | struct ib_mr *mr; |
| 58 | struct kref ref; |
| 59 | struct list_head entry; |
| 60 | }; |
| 61 | |
| 62 | struct nvme_rdma_qe { |
| 63 | struct ib_cqe cqe; |
| 64 | void *data; |
| 65 | u64 dma; |
| 66 | }; |
| 67 | |
| 68 | struct nvme_rdma_queue; |
| 69 | struct nvme_rdma_request { |
| 70 | struct ib_mr *mr; |
| 71 | struct nvme_rdma_qe sqe; |
| 72 | struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; |
| 73 | u32 num_sge; |
| 74 | int nents; |
| 75 | bool inline_data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 76 | struct ib_reg_wr reg_wr; |
| 77 | struct ib_cqe reg_cqe; |
| 78 | struct nvme_rdma_queue *queue; |
| 79 | struct sg_table sg_table; |
| 80 | struct scatterlist first_sgl[]; |
| 81 | }; |
| 82 | |
| 83 | enum nvme_rdma_queue_flags { |
| 84 | NVME_RDMA_Q_CONNECTED = (1 << 0), |
| 85 | }; |
| 86 | |
| 87 | struct nvme_rdma_queue { |
| 88 | struct nvme_rdma_qe *rsp_ring; |
| 89 | u8 sig_count; |
| 90 | int queue_size; |
| 91 | size_t cmnd_capsule_len; |
| 92 | struct nvme_rdma_ctrl *ctrl; |
| 93 | struct nvme_rdma_device *device; |
| 94 | struct ib_cq *ib_cq; |
| 95 | struct ib_qp *qp; |
| 96 | |
| 97 | unsigned long flags; |
| 98 | struct rdma_cm_id *cm_id; |
| 99 | int cm_error; |
| 100 | struct completion cm_done; |
| 101 | }; |
| 102 | |
| 103 | struct nvme_rdma_ctrl { |
| 104 | /* read and written in the hot path */ |
| 105 | spinlock_t lock; |
| 106 | |
| 107 | /* read only in the hot path */ |
| 108 | struct nvme_rdma_queue *queues; |
| 109 | u32 queue_count; |
| 110 | |
| 111 | /* other member variables */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 112 | struct blk_mq_tag_set tag_set; |
| 113 | struct work_struct delete_work; |
| 114 | struct work_struct reset_work; |
| 115 | struct work_struct err_work; |
| 116 | |
| 117 | struct nvme_rdma_qe async_event_sqe; |
| 118 | |
| 119 | int reconnect_delay; |
| 120 | struct delayed_work reconnect_work; |
| 121 | |
| 122 | struct list_head list; |
| 123 | |
| 124 | struct blk_mq_tag_set admin_tag_set; |
| 125 | struct nvme_rdma_device *device; |
| 126 | |
| 127 | u64 cap; |
| 128 | u32 max_fr_pages; |
| 129 | |
| 130 | union { |
| 131 | struct sockaddr addr; |
| 132 | struct sockaddr_in addr_in; |
| 133 | }; |
| 134 | |
| 135 | struct nvme_ctrl ctrl; |
| 136 | }; |
| 137 | |
| 138 | static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) |
| 139 | { |
| 140 | return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); |
| 141 | } |
| 142 | |
| 143 | static LIST_HEAD(device_list); |
| 144 | static DEFINE_MUTEX(device_list_mutex); |
| 145 | |
| 146 | static LIST_HEAD(nvme_rdma_ctrl_list); |
| 147 | static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); |
| 148 | |
| 149 | static struct workqueue_struct *nvme_rdma_wq; |
| 150 | |
| 151 | /* |
| 152 | * Disabling this option makes small I/O goes faster, but is fundamentally |
| 153 | * unsafe. With it turned off we will have to register a global rkey that |
| 154 | * allows read and write access to all physical memory. |
| 155 | */ |
| 156 | static bool register_always = true; |
| 157 | module_param(register_always, bool, 0444); |
| 158 | MODULE_PARM_DESC(register_always, |
| 159 | "Use memory registration even for contiguous memory regions"); |
| 160 | |
| 161 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 162 | struct rdma_cm_event *event); |
| 163 | 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] | 164 | |
| 165 | /* XXX: really should move to a generic header sooner or later.. */ |
| 166 | static inline void put_unaligned_le24(u32 val, u8 *p) |
| 167 | { |
| 168 | *p++ = val; |
| 169 | *p++ = val >> 8; |
| 170 | *p++ = val >> 16; |
| 171 | } |
| 172 | |
| 173 | static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) |
| 174 | { |
| 175 | return queue - queue->ctrl->queues; |
| 176 | } |
| 177 | |
| 178 | static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) |
| 179 | { |
| 180 | return queue->cmnd_capsule_len - sizeof(struct nvme_command); |
| 181 | } |
| 182 | |
| 183 | static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 184 | size_t capsule_size, enum dma_data_direction dir) |
| 185 | { |
| 186 | ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); |
| 187 | kfree(qe->data); |
| 188 | } |
| 189 | |
| 190 | static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 191 | size_t capsule_size, enum dma_data_direction dir) |
| 192 | { |
| 193 | qe->data = kzalloc(capsule_size, GFP_KERNEL); |
| 194 | if (!qe->data) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); |
| 198 | if (ib_dma_mapping_error(ibdev, qe->dma)) { |
| 199 | kfree(qe->data); |
| 200 | return -ENOMEM; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void nvme_rdma_free_ring(struct ib_device *ibdev, |
| 207 | struct nvme_rdma_qe *ring, size_t ib_queue_size, |
| 208 | size_t capsule_size, enum dma_data_direction dir) |
| 209 | { |
| 210 | int i; |
| 211 | |
| 212 | for (i = 0; i < ib_queue_size; i++) |
| 213 | nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); |
| 214 | kfree(ring); |
| 215 | } |
| 216 | |
| 217 | static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, |
| 218 | size_t ib_queue_size, size_t capsule_size, |
| 219 | enum dma_data_direction dir) |
| 220 | { |
| 221 | struct nvme_rdma_qe *ring; |
| 222 | int i; |
| 223 | |
| 224 | ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); |
| 225 | if (!ring) |
| 226 | return NULL; |
| 227 | |
| 228 | for (i = 0; i < ib_queue_size; i++) { |
| 229 | if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) |
| 230 | goto out_free_ring; |
| 231 | } |
| 232 | |
| 233 | return ring; |
| 234 | |
| 235 | out_free_ring: |
| 236 | nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static void nvme_rdma_qp_event(struct ib_event *event, void *context) |
| 241 | { |
| 242 | pr_debug("QP event %d\n", event->event); |
| 243 | } |
| 244 | |
| 245 | static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) |
| 246 | { |
| 247 | wait_for_completion_interruptible_timeout(&queue->cm_done, |
| 248 | msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); |
| 249 | return queue->cm_error; |
| 250 | } |
| 251 | |
| 252 | static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) |
| 253 | { |
| 254 | struct nvme_rdma_device *dev = queue->device; |
| 255 | struct ib_qp_init_attr init_attr; |
| 256 | int ret; |
| 257 | |
| 258 | memset(&init_attr, 0, sizeof(init_attr)); |
| 259 | init_attr.event_handler = nvme_rdma_qp_event; |
| 260 | /* +1 for drain */ |
| 261 | init_attr.cap.max_send_wr = factor * queue->queue_size + 1; |
| 262 | /* +1 for drain */ |
| 263 | init_attr.cap.max_recv_wr = queue->queue_size + 1; |
| 264 | init_attr.cap.max_recv_sge = 1; |
| 265 | init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS; |
| 266 | init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 267 | init_attr.qp_type = IB_QPT_RC; |
| 268 | init_attr.send_cq = queue->ib_cq; |
| 269 | init_attr.recv_cq = queue->ib_cq; |
| 270 | |
| 271 | ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); |
| 272 | |
| 273 | queue->qp = queue->cm_id->qp; |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static int nvme_rdma_reinit_request(void *data, struct request *rq) |
| 278 | { |
| 279 | struct nvme_rdma_ctrl *ctrl = data; |
| 280 | struct nvme_rdma_device *dev = ctrl->device; |
| 281 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 282 | int ret = 0; |
| 283 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 284 | if (!req->mr->need_inval) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 285 | goto out; |
| 286 | |
| 287 | ib_dereg_mr(req->mr); |
| 288 | |
| 289 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 290 | ctrl->max_fr_pages); |
| 291 | if (IS_ERR(req->mr)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 292 | ret = PTR_ERR(req->mr); |
Wei Yongjun | 458a963 | 2016-07-12 11:06:17 +0000 | [diff] [blame] | 293 | req->mr = NULL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 296 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 297 | |
| 298 | out: |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl, |
| 303 | struct request *rq, unsigned int queue_idx) |
| 304 | { |
| 305 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 306 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 307 | struct nvme_rdma_device *dev = queue->device; |
| 308 | |
| 309 | if (req->mr) |
| 310 | ib_dereg_mr(req->mr); |
| 311 | |
| 312 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 313 | DMA_TO_DEVICE); |
| 314 | } |
| 315 | |
| 316 | static void nvme_rdma_exit_request(void *data, struct request *rq, |
| 317 | unsigned int hctx_idx, unsigned int rq_idx) |
| 318 | { |
| 319 | return __nvme_rdma_exit_request(data, rq, hctx_idx + 1); |
| 320 | } |
| 321 | |
| 322 | static void nvme_rdma_exit_admin_request(void *data, struct request *rq, |
| 323 | unsigned int hctx_idx, unsigned int rq_idx) |
| 324 | { |
| 325 | return __nvme_rdma_exit_request(data, rq, 0); |
| 326 | } |
| 327 | |
| 328 | static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl, |
| 329 | struct request *rq, unsigned int queue_idx) |
| 330 | { |
| 331 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 332 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 333 | struct nvme_rdma_device *dev = queue->device; |
| 334 | struct ib_device *ibdev = dev->dev; |
| 335 | int ret; |
| 336 | |
| 337 | BUG_ON(queue_idx >= ctrl->queue_count); |
| 338 | |
| 339 | ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command), |
| 340 | DMA_TO_DEVICE); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 345 | ctrl->max_fr_pages); |
| 346 | if (IS_ERR(req->mr)) { |
| 347 | ret = PTR_ERR(req->mr); |
| 348 | goto out_free_qe; |
| 349 | } |
| 350 | |
| 351 | req->queue = queue; |
| 352 | |
| 353 | return 0; |
| 354 | |
| 355 | out_free_qe: |
| 356 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 357 | DMA_TO_DEVICE); |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
| 361 | static int nvme_rdma_init_request(void *data, struct request *rq, |
| 362 | unsigned int hctx_idx, unsigned int rq_idx, |
| 363 | unsigned int numa_node) |
| 364 | { |
| 365 | return __nvme_rdma_init_request(data, rq, hctx_idx + 1); |
| 366 | } |
| 367 | |
| 368 | static int nvme_rdma_init_admin_request(void *data, struct request *rq, |
| 369 | unsigned int hctx_idx, unsigned int rq_idx, |
| 370 | unsigned int numa_node) |
| 371 | { |
| 372 | return __nvme_rdma_init_request(data, rq, 0); |
| 373 | } |
| 374 | |
| 375 | static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 376 | unsigned int hctx_idx) |
| 377 | { |
| 378 | struct nvme_rdma_ctrl *ctrl = data; |
| 379 | struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 380 | |
| 381 | BUG_ON(hctx_idx >= ctrl->queue_count); |
| 382 | |
| 383 | hctx->driver_data = queue; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 388 | unsigned int hctx_idx) |
| 389 | { |
| 390 | struct nvme_rdma_ctrl *ctrl = data; |
| 391 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 392 | |
| 393 | BUG_ON(hctx_idx != 0); |
| 394 | |
| 395 | hctx->driver_data = queue; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static void nvme_rdma_free_dev(struct kref *ref) |
| 400 | { |
| 401 | struct nvme_rdma_device *ndev = |
| 402 | container_of(ref, struct nvme_rdma_device, ref); |
| 403 | |
| 404 | mutex_lock(&device_list_mutex); |
| 405 | list_del(&ndev->entry); |
| 406 | mutex_unlock(&device_list_mutex); |
| 407 | |
| 408 | if (!register_always) |
| 409 | ib_dereg_mr(ndev->mr); |
| 410 | ib_dealloc_pd(ndev->pd); |
| 411 | |
| 412 | kfree(ndev); |
| 413 | } |
| 414 | |
| 415 | static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) |
| 416 | { |
| 417 | kref_put(&dev->ref, nvme_rdma_free_dev); |
| 418 | } |
| 419 | |
| 420 | static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) |
| 421 | { |
| 422 | return kref_get_unless_zero(&dev->ref); |
| 423 | } |
| 424 | |
| 425 | static struct nvme_rdma_device * |
| 426 | nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) |
| 427 | { |
| 428 | struct nvme_rdma_device *ndev; |
| 429 | |
| 430 | mutex_lock(&device_list_mutex); |
| 431 | list_for_each_entry(ndev, &device_list, entry) { |
| 432 | if (ndev->dev->node_guid == cm_id->device->node_guid && |
| 433 | nvme_rdma_dev_get(ndev)) |
| 434 | goto out_unlock; |
| 435 | } |
| 436 | |
| 437 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); |
| 438 | if (!ndev) |
| 439 | goto out_err; |
| 440 | |
| 441 | ndev->dev = cm_id->device; |
| 442 | kref_init(&ndev->ref); |
| 443 | |
| 444 | ndev->pd = ib_alloc_pd(ndev->dev); |
| 445 | if (IS_ERR(ndev->pd)) |
| 446 | goto out_free_dev; |
| 447 | |
| 448 | if (!register_always) { |
| 449 | ndev->mr = ib_get_dma_mr(ndev->pd, |
| 450 | IB_ACCESS_LOCAL_WRITE | |
| 451 | IB_ACCESS_REMOTE_READ | |
| 452 | IB_ACCESS_REMOTE_WRITE); |
| 453 | if (IS_ERR(ndev->mr)) |
| 454 | goto out_free_pd; |
| 455 | } |
| 456 | |
| 457 | if (!(ndev->dev->attrs.device_cap_flags & |
| 458 | IB_DEVICE_MEM_MGT_EXTENSIONS)) { |
| 459 | dev_err(&ndev->dev->dev, |
| 460 | "Memory registrations not supported.\n"); |
| 461 | goto out_free_mr; |
| 462 | } |
| 463 | |
| 464 | list_add(&ndev->entry, &device_list); |
| 465 | out_unlock: |
| 466 | mutex_unlock(&device_list_mutex); |
| 467 | return ndev; |
| 468 | |
| 469 | out_free_mr: |
| 470 | if (!register_always) |
| 471 | ib_dereg_mr(ndev->mr); |
| 472 | out_free_pd: |
| 473 | ib_dealloc_pd(ndev->pd); |
| 474 | out_free_dev: |
| 475 | kfree(ndev); |
| 476 | out_err: |
| 477 | mutex_unlock(&device_list_mutex); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) |
| 482 | { |
| 483 | struct nvme_rdma_device *dev = queue->device; |
| 484 | struct ib_device *ibdev = dev->dev; |
| 485 | |
| 486 | rdma_destroy_qp(queue->cm_id); |
| 487 | ib_free_cq(queue->ib_cq); |
| 488 | |
| 489 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, |
| 490 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 491 | |
| 492 | nvme_rdma_dev_put(dev); |
| 493 | } |
| 494 | |
| 495 | static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue, |
| 496 | struct nvme_rdma_device *dev) |
| 497 | { |
| 498 | struct ib_device *ibdev = dev->dev; |
| 499 | const int send_wr_factor = 3; /* MR, SEND, INV */ |
| 500 | const int cq_factor = send_wr_factor + 1; /* + RECV */ |
| 501 | int comp_vector, idx = nvme_rdma_queue_idx(queue); |
| 502 | |
| 503 | int ret; |
| 504 | |
| 505 | queue->device = dev; |
| 506 | |
| 507 | /* |
| 508 | * The admin queue is barely used once the controller is live, so don't |
| 509 | * bother to spread it out. |
| 510 | */ |
| 511 | if (idx == 0) |
| 512 | comp_vector = 0; |
| 513 | else |
| 514 | comp_vector = idx % ibdev->num_comp_vectors; |
| 515 | |
| 516 | |
| 517 | /* +1 for ib_stop_cq */ |
| 518 | queue->ib_cq = ib_alloc_cq(dev->dev, queue, |
| 519 | cq_factor * queue->queue_size + 1, comp_vector, |
| 520 | IB_POLL_SOFTIRQ); |
| 521 | if (IS_ERR(queue->ib_cq)) { |
| 522 | ret = PTR_ERR(queue->ib_cq); |
| 523 | goto out; |
| 524 | } |
| 525 | |
| 526 | ret = nvme_rdma_create_qp(queue, send_wr_factor); |
| 527 | if (ret) |
| 528 | goto out_destroy_ib_cq; |
| 529 | |
| 530 | queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, |
| 531 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 532 | if (!queue->rsp_ring) { |
| 533 | ret = -ENOMEM; |
| 534 | goto out_destroy_qp; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | |
| 539 | out_destroy_qp: |
| 540 | ib_destroy_qp(queue->qp); |
| 541 | out_destroy_ib_cq: |
| 542 | ib_free_cq(queue->ib_cq); |
| 543 | out: |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl, |
| 548 | int idx, size_t queue_size) |
| 549 | { |
| 550 | struct nvme_rdma_queue *queue; |
| 551 | int ret; |
| 552 | |
| 553 | queue = &ctrl->queues[idx]; |
| 554 | queue->ctrl = ctrl; |
| 555 | init_completion(&queue->cm_done); |
| 556 | |
| 557 | if (idx > 0) |
| 558 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; |
| 559 | else |
| 560 | queue->cmnd_capsule_len = sizeof(struct nvme_command); |
| 561 | |
| 562 | queue->queue_size = queue_size; |
| 563 | |
| 564 | queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, |
| 565 | RDMA_PS_TCP, IB_QPT_RC); |
| 566 | if (IS_ERR(queue->cm_id)) { |
| 567 | dev_info(ctrl->ctrl.device, |
| 568 | "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); |
| 569 | return PTR_ERR(queue->cm_id); |
| 570 | } |
| 571 | |
| 572 | queue->cm_error = -ETIMEDOUT; |
| 573 | ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr, |
| 574 | NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 575 | if (ret) { |
| 576 | dev_info(ctrl->ctrl.device, |
| 577 | "rdma_resolve_addr failed (%d).\n", ret); |
| 578 | goto out_destroy_cm_id; |
| 579 | } |
| 580 | |
| 581 | ret = nvme_rdma_wait_for_cm(queue); |
| 582 | if (ret) { |
| 583 | dev_info(ctrl->ctrl.device, |
| 584 | "rdma_resolve_addr wait failed (%d).\n", ret); |
| 585 | goto out_destroy_cm_id; |
| 586 | } |
| 587 | |
| 588 | set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags); |
| 589 | |
| 590 | return 0; |
| 591 | |
| 592 | out_destroy_cm_id: |
| 593 | rdma_destroy_id(queue->cm_id); |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) |
| 598 | { |
| 599 | rdma_disconnect(queue->cm_id); |
| 600 | ib_drain_qp(queue->qp); |
| 601 | } |
| 602 | |
| 603 | static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) |
| 604 | { |
| 605 | nvme_rdma_destroy_queue_ib(queue); |
| 606 | rdma_destroy_id(queue->cm_id); |
| 607 | } |
| 608 | |
| 609 | static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue) |
| 610 | { |
| 611 | if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags)) |
| 612 | return; |
| 613 | nvme_rdma_stop_queue(queue); |
| 614 | nvme_rdma_free_queue(queue); |
| 615 | } |
| 616 | |
| 617 | static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 618 | { |
| 619 | int i; |
| 620 | |
| 621 | for (i = 1; i < ctrl->queue_count; i++) |
| 622 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); |
| 623 | } |
| 624 | |
| 625 | static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 626 | { |
| 627 | int i, ret = 0; |
| 628 | |
| 629 | for (i = 1; i < ctrl->queue_count; i++) { |
| 630 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i); |
| 631 | if (ret) |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 639 | { |
| 640 | int i, ret; |
| 641 | |
| 642 | for (i = 1; i < ctrl->queue_count; i++) { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 643 | ret = nvme_rdma_init_queue(ctrl, i, |
| 644 | ctrl->ctrl.opts->queue_size); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 645 | if (ret) { |
| 646 | dev_info(ctrl->ctrl.device, |
| 647 | "failed to initialize i/o queue: %d\n", ret); |
| 648 | goto out_free_queues; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | out_free_queues: |
| 655 | for (; i >= 1; i--) |
| 656 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); |
| 657 | |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) |
| 662 | { |
| 663 | nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe, |
| 664 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 665 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); |
| 666 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 667 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 668 | nvme_rdma_dev_put(ctrl->device); |
| 669 | } |
| 670 | |
| 671 | static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) |
| 672 | { |
| 673 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 674 | |
| 675 | if (list_empty(&ctrl->list)) |
| 676 | goto free_ctrl; |
| 677 | |
| 678 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 679 | list_del(&ctrl->list); |
| 680 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 681 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 682 | kfree(ctrl->queues); |
| 683 | nvmf_free_options(nctrl->opts); |
| 684 | free_ctrl: |
| 685 | kfree(ctrl); |
| 686 | } |
| 687 | |
| 688 | static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) |
| 689 | { |
| 690 | struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), |
| 691 | struct nvme_rdma_ctrl, reconnect_work); |
| 692 | bool changed; |
| 693 | int ret; |
| 694 | |
| 695 | if (ctrl->queue_count > 1) { |
| 696 | nvme_rdma_free_io_queues(ctrl); |
| 697 | |
| 698 | ret = blk_mq_reinit_tagset(&ctrl->tag_set); |
| 699 | if (ret) |
| 700 | goto requeue; |
| 701 | } |
| 702 | |
| 703 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); |
| 704 | |
| 705 | ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set); |
| 706 | if (ret) |
| 707 | goto requeue; |
| 708 | |
| 709 | ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); |
| 710 | if (ret) |
| 711 | goto requeue; |
| 712 | |
| 713 | blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true); |
| 714 | |
| 715 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 716 | if (ret) |
| 717 | goto stop_admin_q; |
| 718 | |
| 719 | ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); |
| 720 | if (ret) |
| 721 | goto stop_admin_q; |
| 722 | |
| 723 | nvme_start_keep_alive(&ctrl->ctrl); |
| 724 | |
| 725 | if (ctrl->queue_count > 1) { |
| 726 | ret = nvme_rdma_init_io_queues(ctrl); |
| 727 | if (ret) |
| 728 | goto stop_admin_q; |
| 729 | |
| 730 | ret = nvme_rdma_connect_io_queues(ctrl); |
| 731 | if (ret) |
| 732 | goto stop_admin_q; |
| 733 | } |
| 734 | |
| 735 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 736 | WARN_ON_ONCE(!changed); |
| 737 | |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 738 | if (ctrl->queue_count > 1) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 739 | nvme_start_queues(&ctrl->ctrl); |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 740 | nvme_queue_scan(&ctrl->ctrl); |
Sagi Grimberg | 3ef1b4b | 2016-08-04 13:46:19 +0300 | [diff] [blame] | 741 | nvme_queue_async_events(&ctrl->ctrl); |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 742 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 743 | |
| 744 | dev_info(ctrl->ctrl.device, "Successfully reconnected\n"); |
| 745 | |
| 746 | return; |
| 747 | |
| 748 | stop_admin_q: |
| 749 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); |
| 750 | requeue: |
| 751 | /* Make sure we are not resetting/deleting */ |
| 752 | if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) { |
| 753 | dev_info(ctrl->ctrl.device, |
| 754 | "Failed reconnect attempt, requeueing...\n"); |
| 755 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, |
| 756 | ctrl->reconnect_delay * HZ); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | static void nvme_rdma_error_recovery_work(struct work_struct *work) |
| 761 | { |
| 762 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 763 | struct nvme_rdma_ctrl, err_work); |
| 764 | |
| 765 | nvme_stop_keep_alive(&ctrl->ctrl); |
| 766 | if (ctrl->queue_count > 1) |
| 767 | nvme_stop_queues(&ctrl->ctrl); |
| 768 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); |
| 769 | |
| 770 | /* We must take care of fastfail/requeue all our inflight requests */ |
| 771 | if (ctrl->queue_count > 1) |
| 772 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 773 | nvme_cancel_request, &ctrl->ctrl); |
| 774 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 775 | nvme_cancel_request, &ctrl->ctrl); |
| 776 | |
| 777 | dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n", |
| 778 | ctrl->reconnect_delay); |
| 779 | |
| 780 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, |
| 781 | ctrl->reconnect_delay * HZ); |
| 782 | } |
| 783 | |
| 784 | static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) |
| 785 | { |
| 786 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) |
| 787 | return; |
| 788 | |
| 789 | queue_work(nvme_rdma_wq, &ctrl->err_work); |
| 790 | } |
| 791 | |
| 792 | static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, |
| 793 | const char *op) |
| 794 | { |
| 795 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 796 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 797 | |
| 798 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) |
| 799 | dev_info(ctrl->ctrl.device, |
| 800 | "%s for CQE 0x%p failed with status %s (%d)\n", |
| 801 | op, wc->wr_cqe, |
| 802 | ib_wc_status_msg(wc->status), wc->status); |
| 803 | nvme_rdma_error_recovery(ctrl); |
| 804 | } |
| 805 | |
| 806 | static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) |
| 807 | { |
| 808 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 809 | nvme_rdma_wr_error(cq, wc, "MEMREG"); |
| 810 | } |
| 811 | |
| 812 | static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) |
| 813 | { |
| 814 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 815 | nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); |
| 816 | } |
| 817 | |
| 818 | static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, |
| 819 | struct nvme_rdma_request *req) |
| 820 | { |
| 821 | struct ib_send_wr *bad_wr; |
| 822 | struct ib_send_wr wr = { |
| 823 | .opcode = IB_WR_LOCAL_INV, |
| 824 | .next = NULL, |
| 825 | .num_sge = 0, |
| 826 | .send_flags = 0, |
| 827 | .ex.invalidate_rkey = req->mr->rkey, |
| 828 | }; |
| 829 | |
| 830 | req->reg_cqe.done = nvme_rdma_inv_rkey_done; |
| 831 | wr.wr_cqe = &req->reg_cqe; |
| 832 | |
| 833 | return ib_post_send(queue->qp, &wr, &bad_wr); |
| 834 | } |
| 835 | |
| 836 | static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, |
| 837 | struct request *rq) |
| 838 | { |
| 839 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 840 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 841 | struct nvme_rdma_device *dev = queue->device; |
| 842 | struct ib_device *ibdev = dev->dev; |
| 843 | int res; |
| 844 | |
| 845 | if (!blk_rq_bytes(rq)) |
| 846 | return; |
| 847 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 848 | if (req->mr->need_inval) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 849 | res = nvme_rdma_inv_rkey(queue, req); |
| 850 | if (res < 0) { |
| 851 | dev_err(ctrl->ctrl.device, |
| 852 | "Queueing INV WR for rkey %#x failed (%d)\n", |
| 853 | req->mr->rkey, res); |
| 854 | nvme_rdma_error_recovery(queue->ctrl); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | ib_dma_unmap_sg(ibdev, req->sg_table.sgl, |
| 859 | req->nents, rq_data_dir(rq) == |
| 860 | WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 861 | |
| 862 | nvme_cleanup_cmd(rq); |
| 863 | sg_free_table_chained(&req->sg_table, true); |
| 864 | } |
| 865 | |
| 866 | static int nvme_rdma_set_sg_null(struct nvme_command *c) |
| 867 | { |
| 868 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 869 | |
| 870 | sg->addr = 0; |
| 871 | put_unaligned_le24(0, sg->length); |
| 872 | put_unaligned_le32(0, sg->key); |
| 873 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, |
| 878 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 879 | { |
| 880 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 881 | |
| 882 | req->sge[1].addr = sg_dma_address(req->sg_table.sgl); |
| 883 | req->sge[1].length = sg_dma_len(req->sg_table.sgl); |
| 884 | req->sge[1].lkey = queue->device->pd->local_dma_lkey; |
| 885 | |
| 886 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); |
| 887 | sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); |
| 888 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; |
| 889 | |
| 890 | req->inline_data = true; |
| 891 | req->num_sge++; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, |
| 896 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 897 | { |
| 898 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 899 | |
| 900 | sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); |
| 901 | put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); |
| 902 | put_unaligned_le32(queue->device->mr->rkey, sg->key); |
| 903 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, |
| 908 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 909 | int count) |
| 910 | { |
| 911 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 912 | int nr; |
| 913 | |
| 914 | nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE); |
| 915 | if (nr < count) { |
| 916 | if (nr < 0) |
| 917 | return nr; |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | |
| 921 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); |
| 922 | |
| 923 | req->reg_cqe.done = nvme_rdma_memreg_done; |
| 924 | memset(&req->reg_wr, 0, sizeof(req->reg_wr)); |
| 925 | req->reg_wr.wr.opcode = IB_WR_REG_MR; |
| 926 | req->reg_wr.wr.wr_cqe = &req->reg_cqe; |
| 927 | req->reg_wr.wr.num_sge = 0; |
| 928 | req->reg_wr.mr = req->mr; |
| 929 | req->reg_wr.key = req->mr->rkey; |
| 930 | req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | |
| 931 | IB_ACCESS_REMOTE_READ | |
| 932 | IB_ACCESS_REMOTE_WRITE; |
| 933 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 934 | req->mr->need_inval = true; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 935 | |
| 936 | sg->addr = cpu_to_le64(req->mr->iova); |
| 937 | put_unaligned_le24(req->mr->length, sg->length); |
| 938 | put_unaligned_le32(req->mr->rkey, sg->key); |
| 939 | sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | |
| 940 | NVME_SGL_FMT_INVALIDATE; |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, |
| 946 | struct request *rq, unsigned int map_len, |
| 947 | struct nvme_command *c) |
| 948 | { |
| 949 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 950 | struct nvme_rdma_device *dev = queue->device; |
| 951 | struct ib_device *ibdev = dev->dev; |
| 952 | int nents, count; |
| 953 | int ret; |
| 954 | |
| 955 | req->num_sge = 1; |
| 956 | req->inline_data = false; |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 957 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 958 | |
| 959 | c->common.flags |= NVME_CMD_SGL_METABUF; |
| 960 | |
| 961 | if (!blk_rq_bytes(rq)) |
| 962 | return nvme_rdma_set_sg_null(c); |
| 963 | |
| 964 | req->sg_table.sgl = req->first_sgl; |
| 965 | ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments, |
| 966 | req->sg_table.sgl); |
| 967 | if (ret) |
| 968 | return -ENOMEM; |
| 969 | |
| 970 | nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); |
| 971 | BUG_ON(nents > rq->nr_phys_segments); |
| 972 | req->nents = nents; |
| 973 | |
| 974 | count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents, |
| 975 | rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 976 | if (unlikely(count <= 0)) { |
| 977 | sg_free_table_chained(&req->sg_table, true); |
| 978 | return -EIO; |
| 979 | } |
| 980 | |
| 981 | if (count == 1) { |
| 982 | if (rq_data_dir(rq) == WRITE && |
| 983 | map_len <= nvme_rdma_inline_data_size(queue) && |
| 984 | nvme_rdma_queue_idx(queue)) |
| 985 | return nvme_rdma_map_sg_inline(queue, req, c); |
| 986 | |
| 987 | if (!register_always) |
| 988 | return nvme_rdma_map_sg_single(queue, req, c); |
| 989 | } |
| 990 | |
| 991 | return nvme_rdma_map_sg_fr(queue, req, c, count); |
| 992 | } |
| 993 | |
| 994 | static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) |
| 995 | { |
| 996 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 997 | nvme_rdma_wr_error(cq, wc, "SEND"); |
| 998 | } |
| 999 | |
| 1000 | static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, |
| 1001 | struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, |
| 1002 | struct ib_send_wr *first, bool flush) |
| 1003 | { |
| 1004 | struct ib_send_wr wr, *bad_wr; |
| 1005 | int ret; |
| 1006 | |
| 1007 | sge->addr = qe->dma; |
| 1008 | sge->length = sizeof(struct nvme_command), |
| 1009 | sge->lkey = queue->device->pd->local_dma_lkey; |
| 1010 | |
| 1011 | qe->cqe.done = nvme_rdma_send_done; |
| 1012 | |
| 1013 | wr.next = NULL; |
| 1014 | wr.wr_cqe = &qe->cqe; |
| 1015 | wr.sg_list = sge; |
| 1016 | wr.num_sge = num_sge; |
| 1017 | wr.opcode = IB_WR_SEND; |
| 1018 | wr.send_flags = 0; |
| 1019 | |
| 1020 | /* |
| 1021 | * Unsignalled send completions are another giant desaster in the |
| 1022 | * IB Verbs spec: If we don't regularly post signalled sends |
| 1023 | * the send queue will fill up and only a QP reset will rescue us. |
| 1024 | * Would have been way to obvious to handle this in hardware or |
| 1025 | * at least the RDMA stack.. |
| 1026 | * |
| 1027 | * This messy and racy code sniplet is copy and pasted from the iSER |
| 1028 | * initiator, and the magic '32' comes from there as well. |
| 1029 | * |
| 1030 | * Always signal the flushes. The magic request used for the flush |
| 1031 | * sequencer is not allocated in our driver's tagset and it's |
| 1032 | * triggered to be freed by blk_cleanup_queue(). So we need to |
| 1033 | * always mark it as signaled to ensure that the "wr_cqe", which is |
| 1034 | * embeded in request's payload, is not freed when __ib_process_cq() |
| 1035 | * calls wr_cqe->done(). |
| 1036 | */ |
| 1037 | if ((++queue->sig_count % 32) == 0 || flush) |
| 1038 | wr.send_flags |= IB_SEND_SIGNALED; |
| 1039 | |
| 1040 | if (first) |
| 1041 | first->next = ≀ |
| 1042 | else |
| 1043 | first = ≀ |
| 1044 | |
| 1045 | ret = ib_post_send(queue->qp, first, &bad_wr); |
| 1046 | if (ret) { |
| 1047 | dev_err(queue->ctrl->ctrl.device, |
| 1048 | "%s failed with error code %d\n", __func__, ret); |
| 1049 | } |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, |
| 1054 | struct nvme_rdma_qe *qe) |
| 1055 | { |
| 1056 | struct ib_recv_wr wr, *bad_wr; |
| 1057 | struct ib_sge list; |
| 1058 | int ret; |
| 1059 | |
| 1060 | list.addr = qe->dma; |
| 1061 | list.length = sizeof(struct nvme_completion); |
| 1062 | list.lkey = queue->device->pd->local_dma_lkey; |
| 1063 | |
| 1064 | qe->cqe.done = nvme_rdma_recv_done; |
| 1065 | |
| 1066 | wr.next = NULL; |
| 1067 | wr.wr_cqe = &qe->cqe; |
| 1068 | wr.sg_list = &list; |
| 1069 | wr.num_sge = 1; |
| 1070 | |
| 1071 | ret = ib_post_recv(queue->qp, &wr, &bad_wr); |
| 1072 | if (ret) { |
| 1073 | dev_err(queue->ctrl->ctrl.device, |
| 1074 | "%s failed with error code %d\n", __func__, ret); |
| 1075 | } |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
| 1079 | static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) |
| 1080 | { |
| 1081 | u32 queue_idx = nvme_rdma_queue_idx(queue); |
| 1082 | |
| 1083 | if (queue_idx == 0) |
| 1084 | return queue->ctrl->admin_tag_set.tags[queue_idx]; |
| 1085 | return queue->ctrl->tag_set.tags[queue_idx - 1]; |
| 1086 | } |
| 1087 | |
| 1088 | static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) |
| 1089 | { |
| 1090 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); |
| 1091 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 1092 | struct ib_device *dev = queue->device->dev; |
| 1093 | struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; |
| 1094 | struct nvme_command *cmd = sqe->data; |
| 1095 | struct ib_sge sge; |
| 1096 | int ret; |
| 1097 | |
| 1098 | if (WARN_ON_ONCE(aer_idx != 0)) |
| 1099 | return; |
| 1100 | |
| 1101 | ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); |
| 1102 | |
| 1103 | memset(cmd, 0, sizeof(*cmd)); |
| 1104 | cmd->common.opcode = nvme_admin_async_event; |
| 1105 | cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; |
| 1106 | cmd->common.flags |= NVME_CMD_SGL_METABUF; |
| 1107 | nvme_rdma_set_sg_null(cmd); |
| 1108 | |
| 1109 | ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), |
| 1110 | DMA_TO_DEVICE); |
| 1111 | |
| 1112 | ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); |
| 1113 | WARN_ON_ONCE(ret); |
| 1114 | } |
| 1115 | |
| 1116 | static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, |
| 1117 | struct nvme_completion *cqe, struct ib_wc *wc, int tag) |
| 1118 | { |
| 1119 | u16 status = le16_to_cpu(cqe->status); |
| 1120 | struct request *rq; |
| 1121 | struct nvme_rdma_request *req; |
| 1122 | int ret = 0; |
| 1123 | |
| 1124 | status >>= 1; |
| 1125 | |
| 1126 | rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); |
| 1127 | if (!rq) { |
| 1128 | dev_err(queue->ctrl->ctrl.device, |
| 1129 | "tag 0x%x on QP %#x not found\n", |
| 1130 | cqe->command_id, queue->qp->qp_num); |
| 1131 | nvme_rdma_error_recovery(queue->ctrl); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | req = blk_mq_rq_to_pdu(rq); |
| 1135 | |
| 1136 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special) |
| 1137 | memcpy(rq->special, cqe, sizeof(*cqe)); |
| 1138 | |
| 1139 | if (rq->tag == tag) |
| 1140 | ret = 1; |
| 1141 | |
| 1142 | if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && |
| 1143 | wc->ex.invalidate_rkey == req->mr->rkey) |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1144 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1145 | |
| 1146 | blk_mq_complete_request(rq, status); |
| 1147 | |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) |
| 1152 | { |
| 1153 | struct nvme_rdma_qe *qe = |
| 1154 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); |
| 1155 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 1156 | struct ib_device *ibdev = queue->device->dev; |
| 1157 | struct nvme_completion *cqe = qe->data; |
| 1158 | const size_t len = sizeof(struct nvme_completion); |
| 1159 | int ret = 0; |
| 1160 | |
| 1161 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1162 | nvme_rdma_wr_error(cq, wc, "RECV"); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1167 | /* |
| 1168 | * AEN requests are special as they don't time out and can |
| 1169 | * survive any kind of queue freeze and often don't respond to |
| 1170 | * aborts. We don't even bother to allocate a struct request |
| 1171 | * for them but rather special case them here. |
| 1172 | */ |
| 1173 | if (unlikely(nvme_rdma_queue_idx(queue) == 0 && |
| 1174 | cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) |
| 1175 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe); |
| 1176 | else |
| 1177 | ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); |
| 1178 | ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1179 | |
| 1180 | nvme_rdma_post_recv(queue, qe); |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1185 | { |
| 1186 | __nvme_rdma_recv_done(cq, wc, -1); |
| 1187 | } |
| 1188 | |
| 1189 | static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) |
| 1190 | { |
| 1191 | int ret, i; |
| 1192 | |
| 1193 | for (i = 0; i < queue->queue_size; i++) { |
| 1194 | ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); |
| 1195 | if (ret) |
| 1196 | goto out_destroy_queue_ib; |
| 1197 | } |
| 1198 | |
| 1199 | return 0; |
| 1200 | |
| 1201 | out_destroy_queue_ib: |
| 1202 | nvme_rdma_destroy_queue_ib(queue); |
| 1203 | return ret; |
| 1204 | } |
| 1205 | |
| 1206 | static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, |
| 1207 | struct rdma_cm_event *ev) |
| 1208 | { |
| 1209 | if (ev->param.conn.private_data_len) { |
| 1210 | struct nvme_rdma_cm_rej *rej = |
| 1211 | (struct nvme_rdma_cm_rej *)ev->param.conn.private_data; |
| 1212 | |
| 1213 | dev_err(queue->ctrl->ctrl.device, |
| 1214 | "Connect rejected, status %d.", le16_to_cpu(rej->sts)); |
| 1215 | /* XXX: Think of something clever to do here... */ |
| 1216 | } else { |
| 1217 | dev_err(queue->ctrl->ctrl.device, |
| 1218 | "Connect rejected, no private data.\n"); |
| 1219 | } |
| 1220 | |
| 1221 | return -ECONNRESET; |
| 1222 | } |
| 1223 | |
| 1224 | static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) |
| 1225 | { |
| 1226 | struct nvme_rdma_device *dev; |
| 1227 | int ret; |
| 1228 | |
| 1229 | dev = nvme_rdma_find_get_device(queue->cm_id); |
| 1230 | if (!dev) { |
| 1231 | dev_err(queue->cm_id->device->dma_device, |
| 1232 | "no client data found!\n"); |
| 1233 | return -ECONNREFUSED; |
| 1234 | } |
| 1235 | |
| 1236 | ret = nvme_rdma_create_queue_ib(queue, dev); |
| 1237 | if (ret) { |
| 1238 | nvme_rdma_dev_put(dev); |
| 1239 | goto out; |
| 1240 | } |
| 1241 | |
| 1242 | ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 1243 | if (ret) { |
| 1244 | dev_err(queue->ctrl->ctrl.device, |
| 1245 | "rdma_resolve_route failed (%d).\n", |
| 1246 | queue->cm_error); |
| 1247 | goto out_destroy_queue; |
| 1248 | } |
| 1249 | |
| 1250 | return 0; |
| 1251 | |
| 1252 | out_destroy_queue: |
| 1253 | nvme_rdma_destroy_queue_ib(queue); |
| 1254 | out: |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) |
| 1259 | { |
| 1260 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1261 | struct rdma_conn_param param = { }; |
Roland Dreier | 0b857b4 | 2016-07-31 00:27:39 -0700 | [diff] [blame] | 1262 | struct nvme_rdma_cm_req priv = { }; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1263 | int ret; |
| 1264 | |
| 1265 | param.qp_num = queue->qp->qp_num; |
| 1266 | param.flow_control = 1; |
| 1267 | |
| 1268 | param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 1269 | /* maximum retry count */ |
| 1270 | param.retry_count = 7; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1271 | param.rnr_retry_count = 7; |
| 1272 | param.private_data = &priv; |
| 1273 | param.private_data_len = sizeof(priv); |
| 1274 | |
| 1275 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1276 | priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * set the admin queue depth to the minimum size |
| 1279 | * specified by the Fabrics standard. |
| 1280 | */ |
| 1281 | if (priv.qid == 0) { |
| 1282 | priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH); |
| 1283 | priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1); |
| 1284 | } else { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * current interpretation of the fabrics spec |
| 1287 | * is at minimum you make hrqsize sqsize+1, or a |
| 1288 | * 1's based representation of sqsize. |
| 1289 | */ |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1290 | priv.hrqsize = cpu_to_le16(queue->queue_size); |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1291 | priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1292 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1293 | |
| 1294 | ret = rdma_connect(queue->cm_id, ¶m); |
| 1295 | if (ret) { |
| 1296 | dev_err(ctrl->ctrl.device, |
| 1297 | "rdma_connect failed (%d).\n", ret); |
| 1298 | goto out_destroy_queue_ib; |
| 1299 | } |
| 1300 | |
| 1301 | return 0; |
| 1302 | |
| 1303 | out_destroy_queue_ib: |
| 1304 | nvme_rdma_destroy_queue_ib(queue); |
| 1305 | return ret; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * nvme_rdma_device_unplug() - Handle RDMA device unplug |
| 1310 | * @queue: Queue that owns the cm_id that caught the event |
| 1311 | * |
| 1312 | * DEVICE_REMOVAL event notifies us that the RDMA device is about |
| 1313 | * to unplug so we should take care of destroying our RDMA resources. |
| 1314 | * This event will be generated for each allocated cm_id. |
| 1315 | * |
| 1316 | * In our case, the RDMA resources are managed per controller and not |
| 1317 | * only per queue. So the way we handle this is we trigger an implicit |
| 1318 | * controller deletion upon the first DEVICE_REMOVAL event we see, and |
| 1319 | * hold the event inflight until the controller deletion is completed. |
| 1320 | * |
| 1321 | * One exception that we need to handle is the destruction of the cm_id |
| 1322 | * that caught the event. Since we hold the callout until the controller |
| 1323 | * deletion is completed, we'll deadlock if the controller deletion will |
| 1324 | * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership |
Sagi Grimberg | 57de5a0 | 2016-07-14 17:39:47 +0300 | [diff] [blame] | 1325 | * of destroying this queue before-hand, destroy the queue resources, |
| 1326 | * then queue the controller deletion which won't destroy this queue and |
| 1327 | * we destroy the cm_id implicitely by returning a non-zero rc to the callout. |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1328 | */ |
| 1329 | static int nvme_rdma_device_unplug(struct nvme_rdma_queue *queue) |
| 1330 | { |
| 1331 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
Colin Ian King | 39bbee4 | 2016-08-16 09:24:39 +0100 | [diff] [blame] | 1332 | int ret = 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1333 | |
Sagi Grimberg | 57de5a0 | 2016-07-14 17:39:47 +0300 | [diff] [blame] | 1334 | /* Own the controller deletion */ |
| 1335 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) |
| 1336 | return 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1337 | |
Sagi Grimberg | 57de5a0 | 2016-07-14 17:39:47 +0300 | [diff] [blame] | 1338 | dev_warn(ctrl->ctrl.device, |
| 1339 | "Got rdma device removal event, deleting ctrl\n"); |
| 1340 | |
| 1341 | /* Get rid of reconnect work if its running */ |
| 1342 | cancel_delayed_work_sync(&ctrl->reconnect_work); |
| 1343 | |
| 1344 | /* Disable the queue so ctrl delete won't free it */ |
| 1345 | if (test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags)) { |
| 1346 | /* Free this queue ourselves */ |
| 1347 | nvme_rdma_stop_queue(queue); |
| 1348 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1349 | |
| 1350 | /* Return non-zero so the cm_id will destroy implicitly */ |
Sagi Grimberg | 57de5a0 | 2016-07-14 17:39:47 +0300 | [diff] [blame] | 1351 | ret = 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1352 | } |
| 1353 | |
Sagi Grimberg | 57de5a0 | 2016-07-14 17:39:47 +0300 | [diff] [blame] | 1354 | /* Queue controller deletion */ |
| 1355 | queue_work(nvme_rdma_wq, &ctrl->delete_work); |
| 1356 | flush_work(&ctrl->delete_work); |
| 1357 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 1361 | struct rdma_cm_event *ev) |
| 1362 | { |
| 1363 | struct nvme_rdma_queue *queue = cm_id->context; |
| 1364 | int cm_error = 0; |
| 1365 | |
| 1366 | dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", |
| 1367 | rdma_event_msg(ev->event), ev->event, |
| 1368 | ev->status, cm_id); |
| 1369 | |
| 1370 | switch (ev->event) { |
| 1371 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 1372 | cm_error = nvme_rdma_addr_resolved(queue); |
| 1373 | break; |
| 1374 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
| 1375 | cm_error = nvme_rdma_route_resolved(queue); |
| 1376 | break; |
| 1377 | case RDMA_CM_EVENT_ESTABLISHED: |
| 1378 | queue->cm_error = nvme_rdma_conn_established(queue); |
| 1379 | /* complete cm_done regardless of success/failure */ |
| 1380 | complete(&queue->cm_done); |
| 1381 | return 0; |
| 1382 | case RDMA_CM_EVENT_REJECTED: |
| 1383 | cm_error = nvme_rdma_conn_rejected(queue, ev); |
| 1384 | break; |
| 1385 | case RDMA_CM_EVENT_ADDR_ERROR: |
| 1386 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 1387 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 1388 | case RDMA_CM_EVENT_UNREACHABLE: |
| 1389 | dev_dbg(queue->ctrl->ctrl.device, |
| 1390 | "CM error event %d\n", ev->event); |
| 1391 | cm_error = -ECONNRESET; |
| 1392 | break; |
| 1393 | case RDMA_CM_EVENT_DISCONNECTED: |
| 1394 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 1395 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 1396 | dev_dbg(queue->ctrl->ctrl.device, |
| 1397 | "disconnect received - connection closed\n"); |
| 1398 | nvme_rdma_error_recovery(queue->ctrl); |
| 1399 | break; |
| 1400 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
| 1401 | /* return 1 means impliciy CM ID destroy */ |
| 1402 | return nvme_rdma_device_unplug(queue); |
| 1403 | default: |
| 1404 | dev_err(queue->ctrl->ctrl.device, |
| 1405 | "Unexpected RDMA CM event (%d)\n", ev->event); |
| 1406 | nvme_rdma_error_recovery(queue->ctrl); |
| 1407 | break; |
| 1408 | } |
| 1409 | |
| 1410 | if (cm_error) { |
| 1411 | queue->cm_error = cm_error; |
| 1412 | complete(&queue->cm_done); |
| 1413 | } |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | static enum blk_eh_timer_return |
| 1419 | nvme_rdma_timeout(struct request *rq, bool reserved) |
| 1420 | { |
| 1421 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1422 | |
| 1423 | /* queue error recovery */ |
| 1424 | nvme_rdma_error_recovery(req->queue->ctrl); |
| 1425 | |
| 1426 | /* fail with DNR on cmd timeout */ |
| 1427 | rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR; |
| 1428 | |
| 1429 | return BLK_EH_HANDLED; |
| 1430 | } |
| 1431 | |
| 1432 | static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1433 | const struct blk_mq_queue_data *bd) |
| 1434 | { |
| 1435 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 1436 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1437 | struct request *rq = bd->rq; |
| 1438 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1439 | struct nvme_rdma_qe *sqe = &req->sqe; |
| 1440 | struct nvme_command *c = sqe->data; |
| 1441 | bool flush = false; |
| 1442 | struct ib_device *dev; |
| 1443 | unsigned int map_len; |
| 1444 | int ret; |
| 1445 | |
| 1446 | WARN_ON_ONCE(rq->tag < 0); |
| 1447 | |
| 1448 | dev = queue->device->dev; |
| 1449 | ib_dma_sync_single_for_cpu(dev, sqe->dma, |
| 1450 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1451 | |
| 1452 | ret = nvme_setup_cmd(ns, rq, c); |
| 1453 | if (ret) |
| 1454 | return ret; |
| 1455 | |
| 1456 | c->common.command_id = rq->tag; |
| 1457 | blk_mq_start_request(rq); |
| 1458 | |
| 1459 | map_len = nvme_map_len(rq); |
| 1460 | ret = nvme_rdma_map_data(queue, rq, map_len, c); |
| 1461 | if (ret < 0) { |
| 1462 | dev_err(queue->ctrl->ctrl.device, |
| 1463 | "Failed to map data (%d)\n", ret); |
| 1464 | nvme_cleanup_cmd(rq); |
| 1465 | goto err; |
| 1466 | } |
| 1467 | |
| 1468 | ib_dma_sync_single_for_device(dev, sqe->dma, |
| 1469 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1470 | |
| 1471 | if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH) |
| 1472 | flush = true; |
| 1473 | ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1474 | req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1475 | if (ret) { |
| 1476 | nvme_rdma_unmap_data(queue, rq); |
| 1477 | goto err; |
| 1478 | } |
| 1479 | |
| 1480 | return BLK_MQ_RQ_QUEUE_OK; |
| 1481 | err: |
| 1482 | return (ret == -ENOMEM || ret == -EAGAIN) ? |
| 1483 | BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; |
| 1484 | } |
| 1485 | |
| 1486 | static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) |
| 1487 | { |
| 1488 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1489 | struct ib_cq *cq = queue->ib_cq; |
| 1490 | struct ib_wc wc; |
| 1491 | int found = 0; |
| 1492 | |
| 1493 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 1494 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 1495 | struct ib_cqe *cqe = wc.wr_cqe; |
| 1496 | |
| 1497 | if (cqe) { |
| 1498 | if (cqe->done == nvme_rdma_recv_done) |
| 1499 | found |= __nvme_rdma_recv_done(cq, &wc, tag); |
| 1500 | else |
| 1501 | cqe->done(cq, &wc); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | return found; |
| 1506 | } |
| 1507 | |
| 1508 | static void nvme_rdma_complete_rq(struct request *rq) |
| 1509 | { |
| 1510 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1511 | struct nvme_rdma_queue *queue = req->queue; |
| 1512 | int error = 0; |
| 1513 | |
| 1514 | nvme_rdma_unmap_data(queue, rq); |
| 1515 | |
| 1516 | if (unlikely(rq->errors)) { |
| 1517 | if (nvme_req_needs_retry(rq, rq->errors)) { |
| 1518 | nvme_requeue_req(rq); |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV) |
| 1523 | error = rq->errors; |
| 1524 | else |
| 1525 | error = nvme_error_status(rq->errors); |
| 1526 | } |
| 1527 | |
| 1528 | blk_mq_end_request(rq, error); |
| 1529 | } |
| 1530 | |
| 1531 | static struct blk_mq_ops nvme_rdma_mq_ops = { |
| 1532 | .queue_rq = nvme_rdma_queue_rq, |
| 1533 | .complete = nvme_rdma_complete_rq, |
| 1534 | .map_queue = blk_mq_map_queue, |
| 1535 | .init_request = nvme_rdma_init_request, |
| 1536 | .exit_request = nvme_rdma_exit_request, |
| 1537 | .reinit_request = nvme_rdma_reinit_request, |
| 1538 | .init_hctx = nvme_rdma_init_hctx, |
| 1539 | .poll = nvme_rdma_poll, |
| 1540 | .timeout = nvme_rdma_timeout, |
| 1541 | }; |
| 1542 | |
| 1543 | static struct blk_mq_ops nvme_rdma_admin_mq_ops = { |
| 1544 | .queue_rq = nvme_rdma_queue_rq, |
| 1545 | .complete = nvme_rdma_complete_rq, |
| 1546 | .map_queue = blk_mq_map_queue, |
| 1547 | .init_request = nvme_rdma_init_admin_request, |
| 1548 | .exit_request = nvme_rdma_exit_admin_request, |
| 1549 | .reinit_request = nvme_rdma_reinit_request, |
| 1550 | .init_hctx = nvme_rdma_init_admin_hctx, |
| 1551 | .timeout = nvme_rdma_timeout, |
| 1552 | }; |
| 1553 | |
| 1554 | static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl) |
| 1555 | { |
| 1556 | int error; |
| 1557 | |
| 1558 | error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); |
| 1559 | if (error) |
| 1560 | return error; |
| 1561 | |
| 1562 | ctrl->device = ctrl->queues[0].device; |
| 1563 | |
| 1564 | /* |
| 1565 | * We need a reference on the device as long as the tag_set is alive, |
| 1566 | * as the MRs in the request structures need a valid ib_device. |
| 1567 | */ |
| 1568 | error = -EINVAL; |
| 1569 | if (!nvme_rdma_dev_get(ctrl->device)) |
| 1570 | goto out_free_queue; |
| 1571 | |
| 1572 | ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS, |
| 1573 | ctrl->device->dev->attrs.max_fast_reg_page_list_len); |
| 1574 | |
| 1575 | memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); |
| 1576 | ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops; |
| 1577 | ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH; |
| 1578 | ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ |
| 1579 | ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; |
| 1580 | ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) + |
| 1581 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 1582 | ctrl->admin_tag_set.driver_data = ctrl; |
| 1583 | ctrl->admin_tag_set.nr_hw_queues = 1; |
| 1584 | ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; |
| 1585 | |
| 1586 | error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); |
| 1587 | if (error) |
| 1588 | goto out_put_dev; |
| 1589 | |
| 1590 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 1591 | if (IS_ERR(ctrl->ctrl.admin_q)) { |
| 1592 | error = PTR_ERR(ctrl->ctrl.admin_q); |
| 1593 | goto out_free_tagset; |
| 1594 | } |
| 1595 | |
| 1596 | error = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 1597 | if (error) |
| 1598 | goto out_cleanup_queue; |
| 1599 | |
| 1600 | error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap); |
| 1601 | if (error) { |
| 1602 | dev_err(ctrl->ctrl.device, |
| 1603 | "prop_get NVME_REG_CAP failed\n"); |
| 1604 | goto out_cleanup_queue; |
| 1605 | } |
| 1606 | |
| 1607 | ctrl->ctrl.sqsize = |
| 1608 | min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize); |
| 1609 | |
| 1610 | error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); |
| 1611 | if (error) |
| 1612 | goto out_cleanup_queue; |
| 1613 | |
| 1614 | ctrl->ctrl.max_hw_sectors = |
| 1615 | (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9); |
| 1616 | |
| 1617 | error = nvme_init_identify(&ctrl->ctrl); |
| 1618 | if (error) |
| 1619 | goto out_cleanup_queue; |
| 1620 | |
| 1621 | error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev, |
| 1622 | &ctrl->async_event_sqe, sizeof(struct nvme_command), |
| 1623 | DMA_TO_DEVICE); |
| 1624 | if (error) |
| 1625 | goto out_cleanup_queue; |
| 1626 | |
| 1627 | nvme_start_keep_alive(&ctrl->ctrl); |
| 1628 | |
| 1629 | return 0; |
| 1630 | |
| 1631 | out_cleanup_queue: |
| 1632 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 1633 | out_free_tagset: |
| 1634 | /* disconnect and drain the queue before freeing the tagset */ |
| 1635 | nvme_rdma_stop_queue(&ctrl->queues[0]); |
| 1636 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 1637 | out_put_dev: |
| 1638 | nvme_rdma_dev_put(ctrl->device); |
| 1639 | out_free_queue: |
| 1640 | nvme_rdma_free_queue(&ctrl->queues[0]); |
| 1641 | return error; |
| 1642 | } |
| 1643 | |
| 1644 | static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl) |
| 1645 | { |
| 1646 | nvme_stop_keep_alive(&ctrl->ctrl); |
| 1647 | cancel_work_sync(&ctrl->err_work); |
| 1648 | cancel_delayed_work_sync(&ctrl->reconnect_work); |
| 1649 | |
| 1650 | if (ctrl->queue_count > 1) { |
| 1651 | nvme_stop_queues(&ctrl->ctrl); |
| 1652 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 1653 | nvme_cancel_request, &ctrl->ctrl); |
| 1654 | nvme_rdma_free_io_queues(ctrl); |
| 1655 | } |
| 1656 | |
Sagi Grimberg | 45862eb | 2016-07-24 09:26:16 +0300 | [diff] [blame] | 1657 | if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1658 | nvme_shutdown_ctrl(&ctrl->ctrl); |
| 1659 | |
| 1660 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); |
| 1661 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 1662 | nvme_cancel_request, &ctrl->ctrl); |
| 1663 | nvme_rdma_destroy_admin_queue(ctrl); |
| 1664 | } |
| 1665 | |
Sagi Grimberg | 2461a8d | 2016-07-24 09:29:51 +0300 | [diff] [blame] | 1666 | static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) |
| 1667 | { |
| 1668 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 1669 | if (shutdown) |
| 1670 | nvme_rdma_shutdown_ctrl(ctrl); |
Sagi Grimberg | a34ca17 | 2016-07-24 09:22:19 +0300 | [diff] [blame] | 1671 | |
| 1672 | if (ctrl->ctrl.tagset) { |
| 1673 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 1674 | blk_mq_free_tag_set(&ctrl->tag_set); |
| 1675 | nvme_rdma_dev_put(ctrl->device); |
| 1676 | } |
| 1677 | |
Sagi Grimberg | 2461a8d | 2016-07-24 09:29:51 +0300 | [diff] [blame] | 1678 | nvme_put_ctrl(&ctrl->ctrl); |
| 1679 | } |
| 1680 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1681 | static void nvme_rdma_del_ctrl_work(struct work_struct *work) |
| 1682 | { |
| 1683 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 1684 | struct nvme_rdma_ctrl, delete_work); |
| 1685 | |
Sagi Grimberg | 2461a8d | 2016-07-24 09:29:51 +0300 | [diff] [blame] | 1686 | __nvme_rdma_remove_ctrl(ctrl, true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl) |
| 1690 | { |
| 1691 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) |
| 1692 | return -EBUSY; |
| 1693 | |
| 1694 | if (!queue_work(nvme_rdma_wq, &ctrl->delete_work)) |
| 1695 | return -EBUSY; |
| 1696 | |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
| 1700 | static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl) |
| 1701 | { |
| 1702 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 1703 | int ret; |
| 1704 | |
| 1705 | ret = __nvme_rdma_del_ctrl(ctrl); |
| 1706 | if (ret) |
| 1707 | return ret; |
| 1708 | |
| 1709 | flush_work(&ctrl->delete_work); |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
| 1714 | static void nvme_rdma_remove_ctrl_work(struct work_struct *work) |
| 1715 | { |
| 1716 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 1717 | struct nvme_rdma_ctrl, delete_work); |
| 1718 | |
Sagi Grimberg | 2461a8d | 2016-07-24 09:29:51 +0300 | [diff] [blame] | 1719 | __nvme_rdma_remove_ctrl(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | static void nvme_rdma_reset_ctrl_work(struct work_struct *work) |
| 1723 | { |
| 1724 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 1725 | struct nvme_rdma_ctrl, reset_work); |
| 1726 | int ret; |
| 1727 | bool changed; |
| 1728 | |
| 1729 | nvme_rdma_shutdown_ctrl(ctrl); |
| 1730 | |
| 1731 | ret = nvme_rdma_configure_admin_queue(ctrl); |
| 1732 | if (ret) { |
| 1733 | /* ctrl is already shutdown, just remove the ctrl */ |
| 1734 | INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work); |
| 1735 | goto del_dead_ctrl; |
| 1736 | } |
| 1737 | |
| 1738 | if (ctrl->queue_count > 1) { |
| 1739 | ret = blk_mq_reinit_tagset(&ctrl->tag_set); |
| 1740 | if (ret) |
| 1741 | goto del_dead_ctrl; |
| 1742 | |
| 1743 | ret = nvme_rdma_init_io_queues(ctrl); |
| 1744 | if (ret) |
| 1745 | goto del_dead_ctrl; |
| 1746 | |
| 1747 | ret = nvme_rdma_connect_io_queues(ctrl); |
| 1748 | if (ret) |
| 1749 | goto del_dead_ctrl; |
| 1750 | } |
| 1751 | |
| 1752 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 1753 | WARN_ON_ONCE(!changed); |
| 1754 | |
| 1755 | if (ctrl->queue_count > 1) { |
| 1756 | nvme_start_queues(&ctrl->ctrl); |
| 1757 | nvme_queue_scan(&ctrl->ctrl); |
Sagi Grimberg | 3ef1b4b | 2016-08-04 13:46:19 +0300 | [diff] [blame] | 1758 | nvme_queue_async_events(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | return; |
| 1762 | |
| 1763 | del_dead_ctrl: |
| 1764 | /* Deleting this dead controller... */ |
| 1765 | dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); |
| 1766 | WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work)); |
| 1767 | } |
| 1768 | |
| 1769 | static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl) |
| 1770 | { |
| 1771 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 1772 | |
| 1773 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) |
| 1774 | return -EBUSY; |
| 1775 | |
| 1776 | if (!queue_work(nvme_rdma_wq, &ctrl->reset_work)) |
| 1777 | return -EBUSY; |
| 1778 | |
| 1779 | flush_work(&ctrl->reset_work); |
| 1780 | |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { |
| 1785 | .name = "rdma", |
| 1786 | .module = THIS_MODULE, |
| 1787 | .is_fabrics = true, |
| 1788 | .reg_read32 = nvmf_reg_read32, |
| 1789 | .reg_read64 = nvmf_reg_read64, |
| 1790 | .reg_write32 = nvmf_reg_write32, |
| 1791 | .reset_ctrl = nvme_rdma_reset_ctrl, |
| 1792 | .free_ctrl = nvme_rdma_free_ctrl, |
| 1793 | .submit_async_event = nvme_rdma_submit_async_event, |
| 1794 | .delete_ctrl = nvme_rdma_del_ctrl, |
| 1795 | .get_subsysnqn = nvmf_get_subsysnqn, |
| 1796 | .get_address = nvmf_get_address, |
| 1797 | }; |
| 1798 | |
| 1799 | static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 1800 | { |
| 1801 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
| 1802 | int ret; |
| 1803 | |
| 1804 | ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues); |
| 1805 | if (ret) |
| 1806 | return ret; |
| 1807 | |
| 1808 | ctrl->queue_count = opts->nr_io_queues + 1; |
| 1809 | if (ctrl->queue_count < 2) |
| 1810 | return 0; |
| 1811 | |
| 1812 | dev_info(ctrl->ctrl.device, |
| 1813 | "creating %d I/O queues.\n", opts->nr_io_queues); |
| 1814 | |
| 1815 | ret = nvme_rdma_init_io_queues(ctrl); |
| 1816 | if (ret) |
| 1817 | return ret; |
| 1818 | |
| 1819 | /* |
| 1820 | * We need a reference on the device as long as the tag_set is alive, |
| 1821 | * as the MRs in the request structures need a valid ib_device. |
| 1822 | */ |
| 1823 | ret = -EINVAL; |
| 1824 | if (!nvme_rdma_dev_get(ctrl->device)) |
| 1825 | goto out_free_io_queues; |
| 1826 | |
| 1827 | memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); |
| 1828 | ctrl->tag_set.ops = &nvme_rdma_mq_ops; |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1829 | ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1830 | ctrl->tag_set.reserved_tags = 1; /* fabric connect */ |
| 1831 | ctrl->tag_set.numa_node = NUMA_NO_NODE; |
| 1832 | ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 1833 | ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) + |
| 1834 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 1835 | ctrl->tag_set.driver_data = ctrl; |
| 1836 | ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1; |
| 1837 | ctrl->tag_set.timeout = NVME_IO_TIMEOUT; |
| 1838 | |
| 1839 | ret = blk_mq_alloc_tag_set(&ctrl->tag_set); |
| 1840 | if (ret) |
| 1841 | goto out_put_dev; |
| 1842 | ctrl->ctrl.tagset = &ctrl->tag_set; |
| 1843 | |
| 1844 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); |
| 1845 | if (IS_ERR(ctrl->ctrl.connect_q)) { |
| 1846 | ret = PTR_ERR(ctrl->ctrl.connect_q); |
| 1847 | goto out_free_tag_set; |
| 1848 | } |
| 1849 | |
| 1850 | ret = nvme_rdma_connect_io_queues(ctrl); |
| 1851 | if (ret) |
| 1852 | goto out_cleanup_connect_q; |
| 1853 | |
| 1854 | return 0; |
| 1855 | |
| 1856 | out_cleanup_connect_q: |
| 1857 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 1858 | out_free_tag_set: |
| 1859 | blk_mq_free_tag_set(&ctrl->tag_set); |
| 1860 | out_put_dev: |
| 1861 | nvme_rdma_dev_put(ctrl->device); |
| 1862 | out_free_io_queues: |
| 1863 | nvme_rdma_free_io_queues(ctrl); |
| 1864 | return ret; |
| 1865 | } |
| 1866 | |
| 1867 | static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p) |
| 1868 | { |
| 1869 | u8 *addr = (u8 *)&in_addr->sin_addr.s_addr; |
| 1870 | size_t buflen = strlen(p); |
| 1871 | |
| 1872 | /* XXX: handle IPv6 addresses */ |
| 1873 | |
| 1874 | if (buflen > INET_ADDRSTRLEN) |
| 1875 | return -EINVAL; |
| 1876 | if (in4_pton(p, buflen, addr, '\0', NULL) == 0) |
| 1877 | return -EINVAL; |
| 1878 | in_addr->sin_family = AF_INET; |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
| 1882 | static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, |
| 1883 | struct nvmf_ctrl_options *opts) |
| 1884 | { |
| 1885 | struct nvme_rdma_ctrl *ctrl; |
| 1886 | int ret; |
| 1887 | bool changed; |
| 1888 | |
| 1889 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 1890 | if (!ctrl) |
| 1891 | return ERR_PTR(-ENOMEM); |
| 1892 | ctrl->ctrl.opts = opts; |
| 1893 | INIT_LIST_HEAD(&ctrl->list); |
| 1894 | |
| 1895 | ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr); |
| 1896 | if (ret) { |
| 1897 | pr_err("malformed IP address passed: %s\n", opts->traddr); |
| 1898 | goto out_free_ctrl; |
| 1899 | } |
| 1900 | |
| 1901 | if (opts->mask & NVMF_OPT_TRSVCID) { |
| 1902 | u16 port; |
| 1903 | |
| 1904 | ret = kstrtou16(opts->trsvcid, 0, &port); |
| 1905 | if (ret) |
| 1906 | goto out_free_ctrl; |
| 1907 | |
| 1908 | ctrl->addr_in.sin_port = cpu_to_be16(port); |
| 1909 | } else { |
| 1910 | ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT); |
| 1911 | } |
| 1912 | |
| 1913 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, |
| 1914 | 0 /* no quirks, we're perfect! */); |
| 1915 | if (ret) |
| 1916 | goto out_free_ctrl; |
| 1917 | |
| 1918 | ctrl->reconnect_delay = opts->reconnect_delay; |
| 1919 | INIT_DELAYED_WORK(&ctrl->reconnect_work, |
| 1920 | nvme_rdma_reconnect_ctrl_work); |
| 1921 | INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); |
| 1922 | INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work); |
| 1923 | INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work); |
| 1924 | spin_lock_init(&ctrl->lock); |
| 1925 | |
| 1926 | ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1927 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1928 | ctrl->ctrl.kato = opts->kato; |
| 1929 | |
| 1930 | ret = -ENOMEM; |
| 1931 | ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues), |
| 1932 | GFP_KERNEL); |
| 1933 | if (!ctrl->queues) |
| 1934 | goto out_uninit_ctrl; |
| 1935 | |
| 1936 | ret = nvme_rdma_configure_admin_queue(ctrl); |
| 1937 | if (ret) |
| 1938 | goto out_kfree_queues; |
| 1939 | |
| 1940 | /* sanity check icdoff */ |
| 1941 | if (ctrl->ctrl.icdoff) { |
| 1942 | dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); |
| 1943 | goto out_remove_admin_queue; |
| 1944 | } |
| 1945 | |
| 1946 | /* sanity check keyed sgls */ |
| 1947 | if (!(ctrl->ctrl.sgls & (1 << 20))) { |
| 1948 | dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); |
| 1949 | goto out_remove_admin_queue; |
| 1950 | } |
| 1951 | |
| 1952 | if (opts->queue_size > ctrl->ctrl.maxcmd) { |
| 1953 | /* warn if maxcmd is lower than queue_size */ |
| 1954 | dev_warn(ctrl->ctrl.device, |
| 1955 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", |
| 1956 | opts->queue_size, ctrl->ctrl.maxcmd); |
| 1957 | opts->queue_size = ctrl->ctrl.maxcmd; |
| 1958 | } |
| 1959 | |
| 1960 | if (opts->nr_io_queues) { |
| 1961 | ret = nvme_rdma_create_io_queues(ctrl); |
| 1962 | if (ret) |
| 1963 | goto out_remove_admin_queue; |
| 1964 | } |
| 1965 | |
| 1966 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 1967 | WARN_ON_ONCE(!changed); |
| 1968 | |
| 1969 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n", |
| 1970 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
| 1971 | |
| 1972 | kref_get(&ctrl->ctrl.kref); |
| 1973 | |
| 1974 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 1975 | list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); |
| 1976 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 1977 | |
| 1978 | if (opts->nr_io_queues) { |
| 1979 | nvme_queue_scan(&ctrl->ctrl); |
| 1980 | nvme_queue_async_events(&ctrl->ctrl); |
| 1981 | } |
| 1982 | |
| 1983 | return &ctrl->ctrl; |
| 1984 | |
| 1985 | out_remove_admin_queue: |
| 1986 | nvme_stop_keep_alive(&ctrl->ctrl); |
| 1987 | nvme_rdma_destroy_admin_queue(ctrl); |
| 1988 | out_kfree_queues: |
| 1989 | kfree(ctrl->queues); |
| 1990 | out_uninit_ctrl: |
| 1991 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 1992 | nvme_put_ctrl(&ctrl->ctrl); |
| 1993 | if (ret > 0) |
| 1994 | ret = -EIO; |
| 1995 | return ERR_PTR(ret); |
| 1996 | out_free_ctrl: |
| 1997 | kfree(ctrl); |
| 1998 | return ERR_PTR(ret); |
| 1999 | } |
| 2000 | |
| 2001 | static struct nvmf_transport_ops nvme_rdma_transport = { |
| 2002 | .name = "rdma", |
| 2003 | .required_opts = NVMF_OPT_TRADDR, |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 2004 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2005 | .create_ctrl = nvme_rdma_create_ctrl, |
| 2006 | }; |
| 2007 | |
| 2008 | static int __init nvme_rdma_init_module(void) |
| 2009 | { |
| 2010 | nvme_rdma_wq = create_workqueue("nvme_rdma_wq"); |
| 2011 | if (!nvme_rdma_wq) |
| 2012 | return -ENOMEM; |
| 2013 | |
| 2014 | nvmf_register_transport(&nvme_rdma_transport); |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | static void __exit nvme_rdma_cleanup_module(void) |
| 2019 | { |
| 2020 | struct nvme_rdma_ctrl *ctrl; |
| 2021 | |
| 2022 | nvmf_unregister_transport(&nvme_rdma_transport); |
| 2023 | |
| 2024 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2025 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) |
| 2026 | __nvme_rdma_del_ctrl(ctrl); |
| 2027 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2028 | |
| 2029 | destroy_workqueue(nvme_rdma_wq); |
| 2030 | } |
| 2031 | |
| 2032 | module_init(nvme_rdma_init_module); |
| 2033 | module_exit(nvme_rdma_cleanup_module); |
| 2034 | |
| 2035 | MODULE_LICENSE("GPL v2"); |