| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * NVMe over Fabrics RDMA target. |
| 4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. |
| 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | #include <linux/atomic.h> |
| 8 | #include <linux/blk-integrity.h> |
| 9 | #include <linux/ctype.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/nvme.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/inet.h> |
| 19 | #include <linux/unaligned.h> |
| 20 | |
| 21 | #include <rdma/ib_verbs.h> |
| 22 | #include <rdma/rdma_cm.h> |
| 23 | #include <rdma/rw.h> |
| 24 | #include <rdma/ib_cm.h> |
| 25 | |
| 26 | #include <linux/nvme-rdma.h> |
| 27 | #include "nvmet.h" |
| 28 | |
| 29 | /* |
| 30 | * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data |
| 31 | */ |
| 32 | #define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE PAGE_SIZE |
| 33 | #define NVMET_RDMA_MAX_INLINE_SGE 4 |
| 34 | #define NVMET_RDMA_MAX_INLINE_DATA_SIZE max_t(int, SZ_16K, PAGE_SIZE) |
| 35 | |
| 36 | /* Assume mpsmin == device_page_size == 4KB */ |
| 37 | #define NVMET_RDMA_MAX_MDTS 8 |
| 38 | #define NVMET_RDMA_MAX_METADATA_MDTS 5 |
| 39 | |
| 40 | #define NVMET_RDMA_BACKLOG 128 |
| 41 | |
| 42 | #define NVMET_RDMA_DISCRETE_RSP_TAG -1 |
| 43 | |
| 44 | struct nvmet_rdma_srq; |
| 45 | |
| 46 | struct nvmet_rdma_cmd { |
| 47 | struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1]; |
| 48 | struct ib_cqe cqe; |
| 49 | struct ib_recv_wr wr; |
| 50 | struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE]; |
| 51 | struct nvme_command *nvme_cmd; |
| 52 | struct nvmet_rdma_queue *queue; |
| 53 | struct nvmet_rdma_srq *nsrq; |
| 54 | }; |
| 55 | |
| 56 | enum { |
| 57 | NVMET_RDMA_REQ_INLINE_DATA = (1 << 0), |
| 58 | }; |
| 59 | |
| 60 | struct nvmet_rdma_rsp { |
| 61 | struct ib_sge send_sge; |
| 62 | struct ib_cqe send_cqe; |
| 63 | struct ib_send_wr send_wr; |
| 64 | |
| 65 | struct nvmet_rdma_cmd *cmd; |
| 66 | struct nvmet_rdma_queue *queue; |
| 67 | |
| 68 | struct ib_cqe read_cqe; |
| 69 | struct ib_cqe write_cqe; |
| 70 | struct rdma_rw_ctx rw; |
| 71 | |
| 72 | struct nvmet_req req; |
| 73 | |
| 74 | bool allocated; |
| 75 | u8 n_rdma; |
| 76 | u32 flags; |
| 77 | u32 invalidate_rkey; |
| 78 | |
| 79 | struct list_head wait_list; |
| 80 | int tag; |
| 81 | }; |
| 82 | |
| 83 | enum nvmet_rdma_queue_state { |
| 84 | NVMET_RDMA_Q_CONNECTING, |
| 85 | NVMET_RDMA_Q_LIVE, |
| 86 | NVMET_RDMA_Q_DISCONNECTING, |
| 87 | }; |
| 88 | |
| 89 | struct nvmet_rdma_queue { |
| 90 | struct rdma_cm_id *cm_id; |
| 91 | struct ib_qp *qp; |
| 92 | struct nvmet_port *port; |
| 93 | struct ib_cq *cq; |
| 94 | atomic_t sq_wr_avail; |
| 95 | struct nvmet_rdma_device *dev; |
| 96 | struct nvmet_rdma_srq *nsrq; |
| 97 | spinlock_t state_lock; |
| 98 | enum nvmet_rdma_queue_state state; |
| 99 | struct nvmet_cq nvme_cq; |
| 100 | struct nvmet_sq nvme_sq; |
| 101 | |
| 102 | struct nvmet_rdma_rsp *rsps; |
| 103 | struct sbitmap rsp_tags; |
| 104 | struct nvmet_rdma_cmd *cmds; |
| 105 | |
| 106 | struct work_struct release_work; |
| 107 | struct list_head rsp_wait_list; |
| 108 | struct list_head rsp_wr_wait_list; |
| 109 | spinlock_t rsp_wr_wait_lock; |
| 110 | |
| 111 | int idx; |
| 112 | int host_qid; |
| 113 | int comp_vector; |
| 114 | int recv_queue_size; |
| 115 | int send_queue_size; |
| 116 | |
| 117 | struct list_head queue_list; |
| 118 | }; |
| 119 | |
| 120 | struct nvmet_rdma_port { |
| 121 | struct nvmet_port *nport; |
| 122 | struct sockaddr_storage addr; |
| 123 | struct rdma_cm_id *cm_id; |
| 124 | struct delayed_work repair_work; |
| 125 | }; |
| 126 | |
| 127 | struct nvmet_rdma_srq { |
| 128 | struct ib_srq *srq; |
| 129 | struct nvmet_rdma_cmd *cmds; |
| 130 | struct nvmet_rdma_device *ndev; |
| 131 | }; |
| 132 | |
| 133 | struct nvmet_rdma_device { |
| 134 | struct ib_device *device; |
| 135 | struct ib_pd *pd; |
| 136 | struct nvmet_rdma_srq **srqs; |
| 137 | int srq_count; |
| 138 | size_t srq_size; |
| 139 | struct kref ref; |
| 140 | struct list_head entry; |
| 141 | int inline_data_size; |
| 142 | int inline_page_count; |
| 143 | }; |
| 144 | |
| 145 | static bool nvmet_rdma_use_srq; |
| 146 | module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444); |
| 147 | MODULE_PARM_DESC(use_srq, "Use shared receive queue." ); |
| 148 | |
| 149 | static int srq_size_set(const char *val, const struct kernel_param *kp); |
| 150 | static const struct kernel_param_ops srq_size_ops = { |
| 151 | .set = srq_size_set, |
| 152 | .get = param_get_int, |
| 153 | }; |
| 154 | |
| 155 | static int nvmet_rdma_srq_size = 1024; |
| 156 | module_param_cb(srq_size, &srq_size_ops, &nvmet_rdma_srq_size, 0644); |
| 157 | MODULE_PARM_DESC(srq_size, "set Shared Receive Queue (SRQ) size, should >= 256 (default: 1024)" ); |
| 158 | |
| 159 | static DEFINE_IDA(nvmet_rdma_queue_ida); |
| 160 | static LIST_HEAD(nvmet_rdma_queue_list); |
| 161 | static DEFINE_MUTEX(nvmet_rdma_queue_mutex); |
| 162 | |
| 163 | static LIST_HEAD(device_list); |
| 164 | static DEFINE_MUTEX(device_list_mutex); |
| 165 | |
| 166 | static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp); |
| 167 | static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc); |
| 168 | static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); |
| 169 | static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc); |
| 170 | static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc); |
| 171 | static void nvmet_rdma_qp_event(struct ib_event *event, void *priv); |
| 172 | static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue); |
| 173 | static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, |
| 174 | struct nvmet_rdma_rsp *r); |
| 175 | static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, |
| 176 | struct nvmet_rdma_rsp *r, |
| 177 | int tag); |
| 178 | |
| 179 | static const struct nvmet_fabrics_ops nvmet_rdma_ops; |
| 180 | |
| 181 | static int srq_size_set(const char *val, const struct kernel_param *kp) |
| 182 | { |
| 183 | int n = 0, ret; |
| 184 | |
| 185 | ret = kstrtoint(s: val, base: 10, res: &n); |
| 186 | if (ret != 0 || n < 256) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | return param_set_int(val, kp); |
| 190 | } |
| 191 | |
| 192 | static int num_pages(int len) |
| 193 | { |
| 194 | return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT); |
| 195 | } |
| 196 | |
| 197 | static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp) |
| 198 | { |
| 199 | return nvme_is_write(cmd: rsp->req.cmd) && |
| 200 | rsp->req.transfer_len && |
| 201 | !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); |
| 202 | } |
| 203 | |
| 204 | static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp) |
| 205 | { |
| 206 | return !nvme_is_write(cmd: rsp->req.cmd) && |
| 207 | rsp->req.transfer_len && |
| 208 | !rsp->req.cqe->status && |
| 209 | !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); |
| 210 | } |
| 211 | |
| 212 | static inline struct nvmet_rdma_rsp * |
| 213 | nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue) |
| 214 | { |
| 215 | struct nvmet_rdma_rsp *rsp = NULL; |
| 216 | int tag; |
| 217 | |
| 218 | tag = sbitmap_get(sb: &queue->rsp_tags); |
| 219 | if (tag >= 0) |
| 220 | rsp = &queue->rsps[tag]; |
| 221 | |
| 222 | if (unlikely(!rsp)) { |
| 223 | int ret; |
| 224 | |
| 225 | rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); |
| 226 | if (unlikely(!rsp)) |
| 227 | return NULL; |
| 228 | ret = nvmet_rdma_alloc_rsp(ndev: queue->dev, r: rsp, |
| 229 | NVMET_RDMA_DISCRETE_RSP_TAG); |
| 230 | if (unlikely(ret)) { |
| 231 | kfree(objp: rsp); |
| 232 | return NULL; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return rsp; |
| 237 | } |
| 238 | |
| 239 | static inline void |
| 240 | nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp) |
| 241 | { |
| 242 | if (unlikely(rsp->tag == NVMET_RDMA_DISCRETE_RSP_TAG)) { |
| 243 | nvmet_rdma_free_rsp(ndev: rsp->queue->dev, r: rsp); |
| 244 | kfree(objp: rsp); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | sbitmap_clear_bit(sb: &rsp->queue->rsp_tags, bitnr: rsp->tag); |
| 249 | } |
| 250 | |
| 251 | static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev, |
| 252 | struct nvmet_rdma_cmd *c) |
| 253 | { |
| 254 | struct scatterlist *sg; |
| 255 | struct ib_sge *sge; |
| 256 | int i; |
| 257 | |
| 258 | if (!ndev->inline_data_size) |
| 259 | return; |
| 260 | |
| 261 | sg = c->inline_sg; |
| 262 | sge = &c->sge[1]; |
| 263 | |
| 264 | for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { |
| 265 | if (sge->length) |
| 266 | ib_dma_unmap_page(dev: ndev->device, addr: sge->addr, |
| 267 | size: sge->length, direction: DMA_FROM_DEVICE); |
| 268 | if (sg_page(sg)) |
| 269 | __free_page(sg_page(sg)); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev, |
| 274 | struct nvmet_rdma_cmd *c) |
| 275 | { |
| 276 | struct scatterlist *sg; |
| 277 | struct ib_sge *sge; |
| 278 | struct page *pg; |
| 279 | int len; |
| 280 | int i; |
| 281 | |
| 282 | if (!ndev->inline_data_size) |
| 283 | return 0; |
| 284 | |
| 285 | sg = c->inline_sg; |
| 286 | sg_init_table(sg, ndev->inline_page_count); |
| 287 | sge = &c->sge[1]; |
| 288 | len = ndev->inline_data_size; |
| 289 | |
| 290 | for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { |
| 291 | pg = alloc_page(GFP_KERNEL); |
| 292 | if (!pg) |
| 293 | goto out_err; |
| 294 | sg_assign_page(sg, page: pg); |
| 295 | sge->addr = ib_dma_map_page(dev: ndev->device, |
| 296 | page: pg, offset: 0, PAGE_SIZE, direction: DMA_FROM_DEVICE); |
| 297 | if (ib_dma_mapping_error(dev: ndev->device, dma_addr: sge->addr)) |
| 298 | goto out_err; |
| 299 | sge->length = min_t(int, len, PAGE_SIZE); |
| 300 | sge->lkey = ndev->pd->local_dma_lkey; |
| 301 | len -= sge->length; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | out_err: |
| 306 | for (; i >= 0; i--, sg--, sge--) { |
| 307 | if (sge->length) |
| 308 | ib_dma_unmap_page(dev: ndev->device, addr: sge->addr, |
| 309 | size: sge->length, direction: DMA_FROM_DEVICE); |
| 310 | if (sg_page(sg)) |
| 311 | __free_page(sg_page(sg)); |
| 312 | } |
| 313 | return -ENOMEM; |
| 314 | } |
| 315 | |
| 316 | static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev, |
| 317 | struct nvmet_rdma_cmd *c, bool admin) |
| 318 | { |
| 319 | /* NVMe command / RDMA RECV */ |
| 320 | c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL); |
| 321 | if (!c->nvme_cmd) |
| 322 | goto out; |
| 323 | |
| 324 | c->sge[0].addr = ib_dma_map_single(dev: ndev->device, cpu_addr: c->nvme_cmd, |
| 325 | size: sizeof(*c->nvme_cmd), direction: DMA_FROM_DEVICE); |
| 326 | if (ib_dma_mapping_error(dev: ndev->device, dma_addr: c->sge[0].addr)) |
| 327 | goto out_free_cmd; |
| 328 | |
| 329 | c->sge[0].length = sizeof(*c->nvme_cmd); |
| 330 | c->sge[0].lkey = ndev->pd->local_dma_lkey; |
| 331 | |
| 332 | if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c)) |
| 333 | goto out_unmap_cmd; |
| 334 | |
| 335 | c->cqe.done = nvmet_rdma_recv_done; |
| 336 | |
| 337 | c->wr.wr_cqe = &c->cqe; |
| 338 | c->wr.sg_list = c->sge; |
| 339 | c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1; |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | out_unmap_cmd: |
| 344 | ib_dma_unmap_single(dev: ndev->device, addr: c->sge[0].addr, |
| 345 | size: sizeof(*c->nvme_cmd), direction: DMA_FROM_DEVICE); |
| 346 | out_free_cmd: |
| 347 | kfree(objp: c->nvme_cmd); |
| 348 | |
| 349 | out: |
| 350 | return -ENOMEM; |
| 351 | } |
| 352 | |
| 353 | static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev, |
| 354 | struct nvmet_rdma_cmd *c, bool admin) |
| 355 | { |
| 356 | if (!admin) |
| 357 | nvmet_rdma_free_inline_pages(ndev, c); |
| 358 | ib_dma_unmap_single(dev: ndev->device, addr: c->sge[0].addr, |
| 359 | size: sizeof(*c->nvme_cmd), direction: DMA_FROM_DEVICE); |
| 360 | kfree(objp: c->nvme_cmd); |
| 361 | } |
| 362 | |
| 363 | static struct nvmet_rdma_cmd * |
| 364 | nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev, |
| 365 | int nr_cmds, bool admin) |
| 366 | { |
| 367 | struct nvmet_rdma_cmd *cmds; |
| 368 | int ret = -EINVAL, i; |
| 369 | |
| 370 | cmds = kvcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL); |
| 371 | if (!cmds) |
| 372 | goto out; |
| 373 | |
| 374 | for (i = 0; i < nr_cmds; i++) { |
| 375 | ret = nvmet_rdma_alloc_cmd(ndev, c: cmds + i, admin); |
| 376 | if (ret) |
| 377 | goto out_free; |
| 378 | } |
| 379 | |
| 380 | return cmds; |
| 381 | |
| 382 | out_free: |
| 383 | while (--i >= 0) |
| 384 | nvmet_rdma_free_cmd(ndev, c: cmds + i, admin); |
| 385 | kvfree(addr: cmds); |
| 386 | out: |
| 387 | return ERR_PTR(error: ret); |
| 388 | } |
| 389 | |
| 390 | static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev, |
| 391 | struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin) |
| 392 | { |
| 393 | int i; |
| 394 | |
| 395 | for (i = 0; i < nr_cmds; i++) |
| 396 | nvmet_rdma_free_cmd(ndev, c: cmds + i, admin); |
| 397 | kvfree(addr: cmds); |
| 398 | } |
| 399 | |
| 400 | static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, |
| 401 | struct nvmet_rdma_rsp *r, int tag) |
| 402 | { |
| 403 | /* NVMe CQE / RDMA SEND */ |
| 404 | r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL); |
| 405 | if (!r->req.cqe) |
| 406 | goto out; |
| 407 | |
| 408 | r->send_sge.addr = ib_dma_map_single(dev: ndev->device, cpu_addr: r->req.cqe, |
| 409 | size: sizeof(*r->req.cqe), direction: DMA_TO_DEVICE); |
| 410 | if (ib_dma_mapping_error(dev: ndev->device, dma_addr: r->send_sge.addr)) |
| 411 | goto out_free_rsp; |
| 412 | |
| 413 | if (ib_dma_pci_p2p_dma_supported(dev: ndev->device)) |
| 414 | r->req.p2p_client = &ndev->device->dev; |
| 415 | r->send_sge.length = sizeof(*r->req.cqe); |
| 416 | r->send_sge.lkey = ndev->pd->local_dma_lkey; |
| 417 | |
| 418 | r->send_cqe.done = nvmet_rdma_send_done; |
| 419 | |
| 420 | r->send_wr.wr_cqe = &r->send_cqe; |
| 421 | r->send_wr.sg_list = &r->send_sge; |
| 422 | r->send_wr.num_sge = 1; |
| 423 | r->send_wr.send_flags = IB_SEND_SIGNALED; |
| 424 | |
| 425 | /* Data In / RDMA READ */ |
| 426 | r->read_cqe.done = nvmet_rdma_read_data_done; |
| 427 | /* Data Out / RDMA WRITE */ |
| 428 | r->write_cqe.done = nvmet_rdma_write_data_done; |
| 429 | r->tag = tag; |
| 430 | |
| 431 | return 0; |
| 432 | |
| 433 | out_free_rsp: |
| 434 | kfree(objp: r->req.cqe); |
| 435 | out: |
| 436 | return -ENOMEM; |
| 437 | } |
| 438 | |
| 439 | static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, |
| 440 | struct nvmet_rdma_rsp *r) |
| 441 | { |
| 442 | ib_dma_unmap_single(dev: ndev->device, addr: r->send_sge.addr, |
| 443 | size: sizeof(*r->req.cqe), direction: DMA_TO_DEVICE); |
| 444 | kfree(objp: r->req.cqe); |
| 445 | } |
| 446 | |
| 447 | static int |
| 448 | nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue) |
| 449 | { |
| 450 | struct nvmet_rdma_device *ndev = queue->dev; |
| 451 | int nr_rsps = queue->recv_queue_size * 2; |
| 452 | int ret = -ENOMEM, i; |
| 453 | |
| 454 | if (sbitmap_init_node(sb: &queue->rsp_tags, depth: nr_rsps, shift: -1, GFP_KERNEL, |
| 455 | NUMA_NO_NODE, round_robin: false, alloc_hint: true)) |
| 456 | goto out; |
| 457 | |
| 458 | queue->rsps = kvcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp), |
| 459 | GFP_KERNEL); |
| 460 | if (!queue->rsps) |
| 461 | goto out_free_sbitmap; |
| 462 | |
| 463 | for (i = 0; i < nr_rsps; i++) { |
| 464 | struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; |
| 465 | |
| 466 | ret = nvmet_rdma_alloc_rsp(ndev, r: rsp, tag: i); |
| 467 | if (ret) |
| 468 | goto out_free; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | |
| 473 | out_free: |
| 474 | while (--i >= 0) |
| 475 | nvmet_rdma_free_rsp(ndev, r: &queue->rsps[i]); |
| 476 | kvfree(addr: queue->rsps); |
| 477 | out_free_sbitmap: |
| 478 | sbitmap_free(sb: &queue->rsp_tags); |
| 479 | out: |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue) |
| 484 | { |
| 485 | struct nvmet_rdma_device *ndev = queue->dev; |
| 486 | int i, nr_rsps = queue->recv_queue_size * 2; |
| 487 | |
| 488 | for (i = 0; i < nr_rsps; i++) |
| 489 | nvmet_rdma_free_rsp(ndev, r: &queue->rsps[i]); |
| 490 | kvfree(addr: queue->rsps); |
| 491 | sbitmap_free(sb: &queue->rsp_tags); |
| 492 | } |
| 493 | |
| 494 | static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev, |
| 495 | struct nvmet_rdma_cmd *cmd) |
| 496 | { |
| 497 | int ret; |
| 498 | |
| 499 | ib_dma_sync_single_for_device(dev: ndev->device, |
| 500 | addr: cmd->sge[0].addr, size: cmd->sge[0].length, |
| 501 | dir: DMA_FROM_DEVICE); |
| 502 | |
| 503 | if (cmd->nsrq) |
| 504 | ret = ib_post_srq_recv(srq: cmd->nsrq->srq, recv_wr: &cmd->wr, NULL); |
| 505 | else |
| 506 | ret = ib_post_recv(qp: cmd->queue->qp, recv_wr: &cmd->wr, NULL); |
| 507 | |
| 508 | if (unlikely(ret)) |
| 509 | pr_err("post_recv cmd failed\n" ); |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue) |
| 515 | { |
| 516 | spin_lock(lock: &queue->rsp_wr_wait_lock); |
| 517 | while (!list_empty(head: &queue->rsp_wr_wait_list)) { |
| 518 | struct nvmet_rdma_rsp *rsp; |
| 519 | bool ret; |
| 520 | |
| 521 | rsp = list_entry(queue->rsp_wr_wait_list.next, |
| 522 | struct nvmet_rdma_rsp, wait_list); |
| 523 | list_del(entry: &rsp->wait_list); |
| 524 | |
| 525 | spin_unlock(lock: &queue->rsp_wr_wait_lock); |
| 526 | ret = nvmet_rdma_execute_command(rsp); |
| 527 | spin_lock(lock: &queue->rsp_wr_wait_lock); |
| 528 | |
| 529 | if (!ret) { |
| 530 | list_add(new: &rsp->wait_list, head: &queue->rsp_wr_wait_list); |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | spin_unlock(lock: &queue->rsp_wr_wait_lock); |
| 535 | } |
| 536 | |
| 537 | static u16 nvmet_rdma_check_pi_status(struct ib_mr *sig_mr) |
| 538 | { |
| 539 | struct ib_mr_status mr_status; |
| 540 | int ret; |
| 541 | u16 status = 0; |
| 542 | |
| 543 | ret = ib_check_mr_status(mr: sig_mr, check_mask: IB_MR_CHECK_SIG_STATUS, mr_status: &mr_status); |
| 544 | if (ret) { |
| 545 | pr_err("ib_check_mr_status failed, ret %d\n" , ret); |
| 546 | return NVME_SC_INVALID_PI; |
| 547 | } |
| 548 | |
| 549 | if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { |
| 550 | switch (mr_status.sig_err.err_type) { |
| 551 | case IB_SIG_BAD_GUARD: |
| 552 | status = NVME_SC_GUARD_CHECK; |
| 553 | break; |
| 554 | case IB_SIG_BAD_REFTAG: |
| 555 | status = NVME_SC_REFTAG_CHECK; |
| 556 | break; |
| 557 | case IB_SIG_BAD_APPTAG: |
| 558 | status = NVME_SC_APPTAG_CHECK; |
| 559 | break; |
| 560 | } |
| 561 | pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n" , |
| 562 | mr_status.sig_err.err_type, |
| 563 | mr_status.sig_err.expected, |
| 564 | mr_status.sig_err.actual); |
| 565 | } |
| 566 | |
| 567 | return status; |
| 568 | } |
| 569 | |
| 570 | static void nvmet_rdma_set_sig_domain(struct blk_integrity *bi, |
| 571 | struct nvme_command *cmd, struct ib_sig_domain *domain, |
| 572 | u16 control, u8 pi_type) |
| 573 | { |
| 574 | domain->sig_type = IB_SIG_TYPE_T10_DIF; |
| 575 | domain->sig.dif.bg_type = IB_T10DIF_CRC; |
| 576 | domain->sig.dif.pi_interval = 1 << bi->interval_exp; |
| 577 | domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag); |
| 578 | if (control & NVME_RW_PRINFO_PRCHK_REF) |
| 579 | domain->sig.dif.ref_remap = true; |
| 580 | |
| 581 | domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.lbat); |
| 582 | domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.lbatm); |
| 583 | domain->sig.dif.app_escape = true; |
| 584 | if (pi_type == NVME_NS_DPS_PI_TYPE3) |
| 585 | domain->sig.dif.ref_escape = true; |
| 586 | } |
| 587 | |
| 588 | static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req, |
| 589 | struct ib_sig_attrs *sig_attrs) |
| 590 | { |
| 591 | struct nvme_command *cmd = req->cmd; |
| 592 | u16 control = le16_to_cpu(cmd->rw.control); |
| 593 | u8 pi_type = req->ns->pi_type; |
| 594 | struct blk_integrity *bi; |
| 595 | |
| 596 | bi = bdev_get_integrity(bdev: req->ns->bdev); |
| 597 | |
| 598 | memset(sig_attrs, 0, sizeof(*sig_attrs)); |
| 599 | |
| 600 | if (control & NVME_RW_PRINFO_PRACT) { |
| 601 | /* for WRITE_INSERT/READ_STRIP no wire domain */ |
| 602 | sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE; |
| 603 | nvmet_rdma_set_sig_domain(bi, cmd, domain: &sig_attrs->mem, control, |
| 604 | pi_type); |
| 605 | /* Clear the PRACT bit since HCA will generate/verify the PI */ |
| 606 | control &= ~NVME_RW_PRINFO_PRACT; |
| 607 | cmd->rw.control = cpu_to_le16(control); |
| 608 | /* PI is added by the HW */ |
| 609 | req->transfer_len += req->metadata_len; |
| 610 | } else { |
| 611 | /* for WRITE_PASS/READ_PASS both wire/memory domains exist */ |
| 612 | nvmet_rdma_set_sig_domain(bi, cmd, domain: &sig_attrs->wire, control, |
| 613 | pi_type); |
| 614 | nvmet_rdma_set_sig_domain(bi, cmd, domain: &sig_attrs->mem, control, |
| 615 | pi_type); |
| 616 | } |
| 617 | |
| 618 | if (control & NVME_RW_PRINFO_PRCHK_REF) |
| 619 | sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG; |
| 620 | if (control & NVME_RW_PRINFO_PRCHK_GUARD) |
| 621 | sig_attrs->check_mask |= IB_SIG_CHECK_GUARD; |
| 622 | if (control & NVME_RW_PRINFO_PRCHK_APP) |
| 623 | sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG; |
| 624 | } |
| 625 | |
| 626 | static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp *rsp, u64 addr, u32 key, |
| 627 | struct ib_sig_attrs *sig_attrs) |
| 628 | { |
| 629 | struct rdma_cm_id *cm_id = rsp->queue->cm_id; |
| 630 | struct nvmet_req *req = &rsp->req; |
| 631 | int ret; |
| 632 | |
| 633 | if (req->metadata_len) |
| 634 | ret = rdma_rw_ctx_signature_init(ctx: &rsp->rw, qp: cm_id->qp, |
| 635 | port_num: cm_id->port_num, sg: req->sg, sg_cnt: req->sg_cnt, |
| 636 | prot_sg: req->metadata_sg, prot_sg_cnt: req->metadata_sg_cnt, sig_attrs, |
| 637 | remote_addr: addr, rkey: key, dir: nvmet_data_dir(req)); |
| 638 | else |
| 639 | ret = rdma_rw_ctx_init(ctx: &rsp->rw, qp: cm_id->qp, port_num: cm_id->port_num, |
| 640 | sg: req->sg, sg_cnt: req->sg_cnt, sg_offset: 0, remote_addr: addr, rkey: key, |
| 641 | dir: nvmet_data_dir(req)); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp *rsp) |
| 647 | { |
| 648 | struct rdma_cm_id *cm_id = rsp->queue->cm_id; |
| 649 | struct nvmet_req *req = &rsp->req; |
| 650 | |
| 651 | if (req->metadata_len) |
| 652 | rdma_rw_ctx_destroy_signature(ctx: &rsp->rw, qp: cm_id->qp, |
| 653 | port_num: cm_id->port_num, sg: req->sg, sg_cnt: req->sg_cnt, |
| 654 | prot_sg: req->metadata_sg, prot_sg_cnt: req->metadata_sg_cnt, |
| 655 | dir: nvmet_data_dir(req)); |
| 656 | else |
| 657 | rdma_rw_ctx_destroy(ctx: &rsp->rw, qp: cm_id->qp, port_num: cm_id->port_num, |
| 658 | sg: req->sg, sg_cnt: req->sg_cnt, dir: nvmet_data_dir(req)); |
| 659 | } |
| 660 | |
| 661 | static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp) |
| 662 | { |
| 663 | struct nvmet_rdma_queue *queue = rsp->queue; |
| 664 | |
| 665 | atomic_add(i: 1 + rsp->n_rdma, v: &queue->sq_wr_avail); |
| 666 | |
| 667 | if (rsp->n_rdma) |
| 668 | nvmet_rdma_rw_ctx_destroy(rsp); |
| 669 | |
| 670 | if (rsp->req.sg != rsp->cmd->inline_sg) |
| 671 | nvmet_req_free_sgls(req: &rsp->req); |
| 672 | |
| 673 | if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list))) |
| 674 | nvmet_rdma_process_wr_wait_list(queue); |
| 675 | |
| 676 | nvmet_rdma_put_rsp(rsp); |
| 677 | } |
| 678 | |
| 679 | static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue) |
| 680 | { |
| 681 | if (queue->nvme_sq.ctrl) { |
| 682 | nvmet_ctrl_fatal_error(ctrl: queue->nvme_sq.ctrl); |
| 683 | } else { |
| 684 | /* |
| 685 | * we didn't setup the controller yet in case |
| 686 | * of admin connect error, just disconnect and |
| 687 | * cleanup the queue |
| 688 | */ |
| 689 | nvmet_rdma_queue_disconnect(queue); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) |
| 694 | { |
| 695 | struct nvmet_rdma_rsp *rsp = |
| 696 | container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe); |
| 697 | struct nvmet_rdma_queue *queue = wc->qp->qp_context; |
| 698 | |
| 699 | nvmet_rdma_release_rsp(rsp); |
| 700 | |
| 701 | if (unlikely(wc->status != IB_WC_SUCCESS && |
| 702 | wc->status != IB_WC_WR_FLUSH_ERR)) { |
| 703 | pr_err("SEND for CQE 0x%p failed with status %s (%d).\n" , |
| 704 | wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); |
| 705 | nvmet_rdma_error_comp(queue); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static void nvmet_rdma_queue_response(struct nvmet_req *req) |
| 710 | { |
| 711 | struct nvmet_rdma_rsp *rsp = |
| 712 | container_of(req, struct nvmet_rdma_rsp, req); |
| 713 | struct rdma_cm_id *cm_id = rsp->queue->cm_id; |
| 714 | struct ib_send_wr *first_wr; |
| 715 | |
| 716 | if (rsp->invalidate_rkey) { |
| 717 | rsp->send_wr.opcode = IB_WR_SEND_WITH_INV; |
| 718 | rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey; |
| 719 | } else { |
| 720 | rsp->send_wr.opcode = IB_WR_SEND; |
| 721 | } |
| 722 | |
| 723 | if (nvmet_rdma_need_data_out(rsp)) { |
| 724 | if (rsp->req.metadata_len) |
| 725 | first_wr = rdma_rw_ctx_wrs(ctx: &rsp->rw, qp: cm_id->qp, |
| 726 | port_num: cm_id->port_num, cqe: &rsp->write_cqe, NULL); |
| 727 | else |
| 728 | first_wr = rdma_rw_ctx_wrs(ctx: &rsp->rw, qp: cm_id->qp, |
| 729 | port_num: cm_id->port_num, NULL, chain_wr: &rsp->send_wr); |
| 730 | } else { |
| 731 | first_wr = &rsp->send_wr; |
| 732 | } |
| 733 | |
| 734 | nvmet_rdma_post_recv(ndev: rsp->queue->dev, cmd: rsp->cmd); |
| 735 | |
| 736 | ib_dma_sync_single_for_device(dev: rsp->queue->dev->device, |
| 737 | addr: rsp->send_sge.addr, size: rsp->send_sge.length, |
| 738 | dir: DMA_TO_DEVICE); |
| 739 | |
| 740 | if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) { |
| 741 | pr_err("sending cmd response failed\n" ); |
| 742 | nvmet_rdma_release_rsp(rsp); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc) |
| 747 | { |
| 748 | struct nvmet_rdma_rsp *rsp = |
| 749 | container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe); |
| 750 | struct nvmet_rdma_queue *queue = wc->qp->qp_context; |
| 751 | u16 status = 0; |
| 752 | |
| 753 | WARN_ON(rsp->n_rdma <= 0); |
| 754 | atomic_add(i: rsp->n_rdma, v: &queue->sq_wr_avail); |
| 755 | rsp->n_rdma = 0; |
| 756 | |
| 757 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 758 | nvmet_rdma_rw_ctx_destroy(rsp); |
| 759 | nvmet_req_uninit(req: &rsp->req); |
| 760 | nvmet_rdma_release_rsp(rsp); |
| 761 | if (wc->status != IB_WC_WR_FLUSH_ERR) { |
| 762 | pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n" , |
| 763 | wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); |
| 764 | nvmet_rdma_error_comp(queue); |
| 765 | } |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | if (rsp->req.metadata_len) |
| 770 | status = nvmet_rdma_check_pi_status(sig_mr: rsp->rw.reg->mr); |
| 771 | nvmet_rdma_rw_ctx_destroy(rsp); |
| 772 | |
| 773 | if (unlikely(status)) |
| 774 | nvmet_req_complete(req: &rsp->req, status); |
| 775 | else |
| 776 | rsp->req.execute(&rsp->req); |
| 777 | } |
| 778 | |
| 779 | static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc) |
| 780 | { |
| 781 | struct nvmet_rdma_rsp *rsp = |
| 782 | container_of(wc->wr_cqe, struct nvmet_rdma_rsp, write_cqe); |
| 783 | struct nvmet_rdma_queue *queue = wc->qp->qp_context; |
| 784 | struct rdma_cm_id *cm_id = rsp->queue->cm_id; |
| 785 | u16 status; |
| 786 | |
| 787 | if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) |
| 788 | return; |
| 789 | |
| 790 | WARN_ON(rsp->n_rdma <= 0); |
| 791 | atomic_add(i: rsp->n_rdma, v: &queue->sq_wr_avail); |
| 792 | rsp->n_rdma = 0; |
| 793 | |
| 794 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 795 | nvmet_rdma_rw_ctx_destroy(rsp); |
| 796 | nvmet_req_uninit(req: &rsp->req); |
| 797 | nvmet_rdma_release_rsp(rsp); |
| 798 | if (wc->status != IB_WC_WR_FLUSH_ERR) { |
| 799 | pr_info("RDMA WRITE for CQE failed with status %s (%d).\n" , |
| 800 | ib_wc_status_msg(wc->status), wc->status); |
| 801 | nvmet_rdma_error_comp(queue); |
| 802 | } |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Upon RDMA completion check the signature status |
| 808 | * - if succeeded send good NVMe response |
| 809 | * - if failed send bad NVMe response with appropriate error |
| 810 | */ |
| 811 | status = nvmet_rdma_check_pi_status(sig_mr: rsp->rw.reg->mr); |
| 812 | if (unlikely(status)) |
| 813 | rsp->req.cqe->status = cpu_to_le16(status << 1); |
| 814 | nvmet_rdma_rw_ctx_destroy(rsp); |
| 815 | |
| 816 | if (unlikely(ib_post_send(cm_id->qp, &rsp->send_wr, NULL))) { |
| 817 | pr_err("sending cmd response failed\n" ); |
| 818 | nvmet_rdma_release_rsp(rsp); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len, |
| 823 | u64 off) |
| 824 | { |
| 825 | int sg_count = num_pages(len); |
| 826 | struct scatterlist *sg; |
| 827 | int i; |
| 828 | |
| 829 | sg = rsp->cmd->inline_sg; |
| 830 | for (i = 0; i < sg_count; i++, sg++) { |
| 831 | if (i < sg_count - 1) |
| 832 | sg_unmark_end(sg); |
| 833 | else |
| 834 | sg_mark_end(sg); |
| 835 | sg->offset = off; |
| 836 | sg->length = min_t(int, len, PAGE_SIZE - off); |
| 837 | len -= sg->length; |
| 838 | if (!i) |
| 839 | off = 0; |
| 840 | } |
| 841 | |
| 842 | rsp->req.sg = rsp->cmd->inline_sg; |
| 843 | rsp->req.sg_cnt = sg_count; |
| 844 | } |
| 845 | |
| 846 | static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp) |
| 847 | { |
| 848 | struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl; |
| 849 | u64 off = le64_to_cpu(sgl->addr); |
| 850 | u32 len = le32_to_cpu(sgl->length); |
| 851 | |
| 852 | if (!nvme_is_write(cmd: rsp->req.cmd)) { |
| 853 | rsp->req.error_loc = |
| 854 | offsetof(struct nvme_common_command, opcode); |
| 855 | return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 856 | } |
| 857 | |
| 858 | if (off + len > rsp->queue->dev->inline_data_size) { |
| 859 | pr_err("invalid inline data offset!\n" ); |
| 860 | return NVME_SC_SGL_INVALID_OFFSET | NVME_STATUS_DNR; |
| 861 | } |
| 862 | |
| 863 | /* no data command? */ |
| 864 | if (!len) |
| 865 | return 0; |
| 866 | |
| 867 | nvmet_rdma_use_inline_sg(rsp, len, off); |
| 868 | rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA; |
| 869 | rsp->req.transfer_len += len; |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp, |
| 874 | struct nvme_keyed_sgl_desc *sgl, bool invalidate) |
| 875 | { |
| 876 | u64 addr = le64_to_cpu(sgl->addr); |
| 877 | u32 key = get_unaligned_le32(p: sgl->key); |
| 878 | struct ib_sig_attrs sig_attrs; |
| 879 | int ret; |
| 880 | |
| 881 | rsp->req.transfer_len = get_unaligned_le24(p: sgl->length); |
| 882 | |
| 883 | /* no data command? */ |
| 884 | if (!rsp->req.transfer_len) |
| 885 | return 0; |
| 886 | |
| 887 | if (rsp->req.metadata_len) |
| 888 | nvmet_rdma_set_sig_attrs(req: &rsp->req, sig_attrs: &sig_attrs); |
| 889 | |
| 890 | ret = nvmet_req_alloc_sgls(req: &rsp->req); |
| 891 | if (unlikely(ret < 0)) |
| 892 | goto error_out; |
| 893 | |
| 894 | ret = nvmet_rdma_rw_ctx_init(rsp, addr, key, sig_attrs: &sig_attrs); |
| 895 | if (unlikely(ret < 0)) |
| 896 | goto error_out; |
| 897 | rsp->n_rdma += ret; |
| 898 | |
| 899 | if (invalidate) |
| 900 | rsp->invalidate_rkey = key; |
| 901 | |
| 902 | return 0; |
| 903 | |
| 904 | error_out: |
| 905 | rsp->req.transfer_len = 0; |
| 906 | return NVME_SC_INTERNAL; |
| 907 | } |
| 908 | |
| 909 | static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp) |
| 910 | { |
| 911 | struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl; |
| 912 | |
| 913 | switch (sgl->type >> 4) { |
| 914 | case NVME_SGL_FMT_DATA_DESC: |
| 915 | switch (sgl->type & 0xf) { |
| 916 | case NVME_SGL_FMT_OFFSET: |
| 917 | return nvmet_rdma_map_sgl_inline(rsp); |
| 918 | default: |
| 919 | pr_err("invalid SGL subtype: %#x\n" , sgl->type); |
| 920 | rsp->req.error_loc = |
| 921 | offsetof(struct nvme_common_command, dptr); |
| 922 | return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 923 | } |
| 924 | case NVME_KEY_SGL_FMT_DATA_DESC: |
| 925 | switch (sgl->type & 0xf) { |
| 926 | case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE: |
| 927 | return nvmet_rdma_map_sgl_keyed(rsp, sgl, invalidate: true); |
| 928 | case NVME_SGL_FMT_ADDRESS: |
| 929 | return nvmet_rdma_map_sgl_keyed(rsp, sgl, invalidate: false); |
| 930 | default: |
| 931 | pr_err("invalid SGL subtype: %#x\n" , sgl->type); |
| 932 | rsp->req.error_loc = |
| 933 | offsetof(struct nvme_common_command, dptr); |
| 934 | return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 935 | } |
| 936 | default: |
| 937 | pr_err("invalid SGL type: %#x\n" , sgl->type); |
| 938 | rsp->req.error_loc = offsetof(struct nvme_common_command, dptr); |
| 939 | return NVME_SC_SGL_INVALID_TYPE | NVME_STATUS_DNR; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp) |
| 944 | { |
| 945 | struct nvmet_rdma_queue *queue = rsp->queue; |
| 946 | |
| 947 | if (unlikely(atomic_sub_return(1 + rsp->n_rdma, |
| 948 | &queue->sq_wr_avail) < 0)) { |
| 949 | pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n" , |
| 950 | 1 + rsp->n_rdma, queue->idx, |
| 951 | queue->nvme_sq.ctrl->cntlid); |
| 952 | atomic_add(i: 1 + rsp->n_rdma, v: &queue->sq_wr_avail); |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | if (nvmet_rdma_need_data_in(rsp)) { |
| 957 | if (rdma_rw_ctx_post(ctx: &rsp->rw, qp: queue->qp, |
| 958 | port_num: queue->cm_id->port_num, cqe: &rsp->read_cqe, NULL)) |
| 959 | nvmet_req_complete(req: &rsp->req, status: NVME_SC_DATA_XFER_ERROR); |
| 960 | } else { |
| 961 | rsp->req.execute(&rsp->req); |
| 962 | } |
| 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue, |
| 968 | struct nvmet_rdma_rsp *cmd) |
| 969 | { |
| 970 | u16 status; |
| 971 | |
| 972 | ib_dma_sync_single_for_cpu(dev: queue->dev->device, |
| 973 | addr: cmd->cmd->sge[0].addr, size: cmd->cmd->sge[0].length, |
| 974 | dir: DMA_FROM_DEVICE); |
| 975 | ib_dma_sync_single_for_cpu(dev: queue->dev->device, |
| 976 | addr: cmd->send_sge.addr, size: cmd->send_sge.length, |
| 977 | dir: DMA_TO_DEVICE); |
| 978 | |
| 979 | if (!nvmet_req_init(req: &cmd->req, sq: &queue->nvme_sq, ops: &nvmet_rdma_ops)) |
| 980 | return; |
| 981 | |
| 982 | status = nvmet_rdma_map_sgl(rsp: cmd); |
| 983 | if (status) |
| 984 | goto out_err; |
| 985 | |
| 986 | if (unlikely(!nvmet_rdma_execute_command(cmd))) { |
| 987 | spin_lock(lock: &queue->rsp_wr_wait_lock); |
| 988 | list_add_tail(new: &cmd->wait_list, head: &queue->rsp_wr_wait_list); |
| 989 | spin_unlock(lock: &queue->rsp_wr_wait_lock); |
| 990 | } |
| 991 | |
| 992 | return; |
| 993 | |
| 994 | out_err: |
| 995 | nvmet_req_complete(req: &cmd->req, status); |
| 996 | } |
| 997 | |
| 998 | static bool nvmet_rdma_recv_not_live(struct nvmet_rdma_queue *queue, |
| 999 | struct nvmet_rdma_rsp *rsp) |
| 1000 | { |
| 1001 | unsigned long flags; |
| 1002 | bool ret = true; |
| 1003 | |
| 1004 | spin_lock_irqsave(&queue->state_lock, flags); |
| 1005 | /* |
| 1006 | * recheck queue state is not live to prevent a race condition |
| 1007 | * with RDMA_CM_EVENT_ESTABLISHED handler. |
| 1008 | */ |
| 1009 | if (queue->state == NVMET_RDMA_Q_LIVE) |
| 1010 | ret = false; |
| 1011 | else if (queue->state == NVMET_RDMA_Q_CONNECTING) |
| 1012 | list_add_tail(new: &rsp->wait_list, head: &queue->rsp_wait_list); |
| 1013 | else |
| 1014 | nvmet_rdma_put_rsp(rsp); |
| 1015 | spin_unlock_irqrestore(lock: &queue->state_lock, flags); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1020 | { |
| 1021 | struct nvmet_rdma_cmd *cmd = |
| 1022 | container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe); |
| 1023 | struct nvmet_rdma_queue *queue = wc->qp->qp_context; |
| 1024 | struct nvmet_rdma_rsp *rsp; |
| 1025 | |
| 1026 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1027 | if (wc->status != IB_WC_WR_FLUSH_ERR) { |
| 1028 | pr_err("RECV for CQE 0x%p failed with status %s (%d)\n" , |
| 1029 | wc->wr_cqe, ib_wc_status_msg(wc->status), |
| 1030 | wc->status); |
| 1031 | nvmet_rdma_error_comp(queue); |
| 1032 | } |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | if (unlikely(wc->byte_len < sizeof(struct nvme_command))) { |
| 1037 | pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n" ); |
| 1038 | nvmet_rdma_error_comp(queue); |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | cmd->queue = queue; |
| 1043 | rsp = nvmet_rdma_get_rsp(queue); |
| 1044 | if (unlikely(!rsp)) { |
| 1045 | /* |
| 1046 | * we get here only under memory pressure, |
| 1047 | * silently drop and have the host retry |
| 1048 | * as we can't even fail it. |
| 1049 | */ |
| 1050 | nvmet_rdma_post_recv(ndev: queue->dev, cmd); |
| 1051 | return; |
| 1052 | } |
| 1053 | rsp->queue = queue; |
| 1054 | rsp->cmd = cmd; |
| 1055 | rsp->flags = 0; |
| 1056 | rsp->req.cmd = cmd->nvme_cmd; |
| 1057 | rsp->req.port = queue->port; |
| 1058 | rsp->n_rdma = 0; |
| 1059 | rsp->invalidate_rkey = 0; |
| 1060 | |
| 1061 | if (unlikely(queue->state != NVMET_RDMA_Q_LIVE) && |
| 1062 | nvmet_rdma_recv_not_live(queue, rsp)) |
| 1063 | return; |
| 1064 | |
| 1065 | nvmet_rdma_handle_command(queue, cmd: rsp); |
| 1066 | } |
| 1067 | |
| 1068 | static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq *nsrq) |
| 1069 | { |
| 1070 | nvmet_rdma_free_cmds(ndev: nsrq->ndev, cmds: nsrq->cmds, nr_cmds: nsrq->ndev->srq_size, |
| 1071 | admin: false); |
| 1072 | ib_destroy_srq(srq: nsrq->srq); |
| 1073 | |
| 1074 | kfree(objp: nsrq); |
| 1075 | } |
| 1076 | |
| 1077 | static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device *ndev) |
| 1078 | { |
| 1079 | int i; |
| 1080 | |
| 1081 | if (!ndev->srqs) |
| 1082 | return; |
| 1083 | |
| 1084 | for (i = 0; i < ndev->srq_count; i++) |
| 1085 | nvmet_rdma_destroy_srq(nsrq: ndev->srqs[i]); |
| 1086 | |
| 1087 | kfree(objp: ndev->srqs); |
| 1088 | } |
| 1089 | |
| 1090 | static struct nvmet_rdma_srq * |
| 1091 | nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev) |
| 1092 | { |
| 1093 | struct ib_srq_init_attr srq_attr = { NULL, }; |
| 1094 | size_t srq_size = ndev->srq_size; |
| 1095 | struct nvmet_rdma_srq *nsrq; |
| 1096 | struct ib_srq *srq; |
| 1097 | int ret, i; |
| 1098 | |
| 1099 | nsrq = kzalloc(sizeof(*nsrq), GFP_KERNEL); |
| 1100 | if (!nsrq) |
| 1101 | return ERR_PTR(error: -ENOMEM); |
| 1102 | |
| 1103 | srq_attr.attr.max_wr = srq_size; |
| 1104 | srq_attr.attr.max_sge = 1 + ndev->inline_page_count; |
| 1105 | srq_attr.attr.srq_limit = 0; |
| 1106 | srq_attr.srq_type = IB_SRQT_BASIC; |
| 1107 | srq = ib_create_srq(pd: ndev->pd, srq_init_attr: &srq_attr); |
| 1108 | if (IS_ERR(ptr: srq)) { |
| 1109 | ret = PTR_ERR(ptr: srq); |
| 1110 | goto out_free; |
| 1111 | } |
| 1112 | |
| 1113 | nsrq->cmds = nvmet_rdma_alloc_cmds(ndev, nr_cmds: srq_size, admin: false); |
| 1114 | if (IS_ERR(ptr: nsrq->cmds)) { |
| 1115 | ret = PTR_ERR(ptr: nsrq->cmds); |
| 1116 | goto out_destroy_srq; |
| 1117 | } |
| 1118 | |
| 1119 | nsrq->srq = srq; |
| 1120 | nsrq->ndev = ndev; |
| 1121 | |
| 1122 | for (i = 0; i < srq_size; i++) { |
| 1123 | nsrq->cmds[i].nsrq = nsrq; |
| 1124 | ret = nvmet_rdma_post_recv(ndev, cmd: &nsrq->cmds[i]); |
| 1125 | if (ret) |
| 1126 | goto out_free_cmds; |
| 1127 | } |
| 1128 | |
| 1129 | return nsrq; |
| 1130 | |
| 1131 | out_free_cmds: |
| 1132 | nvmet_rdma_free_cmds(ndev, cmds: nsrq->cmds, nr_cmds: srq_size, admin: false); |
| 1133 | out_destroy_srq: |
| 1134 | ib_destroy_srq(srq); |
| 1135 | out_free: |
| 1136 | kfree(objp: nsrq); |
| 1137 | return ERR_PTR(error: ret); |
| 1138 | } |
| 1139 | |
| 1140 | static int nvmet_rdma_init_srqs(struct nvmet_rdma_device *ndev) |
| 1141 | { |
| 1142 | int i, ret; |
| 1143 | |
| 1144 | if (!ndev->device->attrs.max_srq_wr || !ndev->device->attrs.max_srq) { |
| 1145 | /* |
| 1146 | * If SRQs aren't supported we just go ahead and use normal |
| 1147 | * non-shared receive queues. |
| 1148 | */ |
| 1149 | pr_info("SRQ requested but not supported.\n" ); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | ndev->srq_size = min(ndev->device->attrs.max_srq_wr, |
| 1154 | nvmet_rdma_srq_size); |
| 1155 | ndev->srq_count = min(ndev->device->num_comp_vectors, |
| 1156 | ndev->device->attrs.max_srq); |
| 1157 | |
| 1158 | ndev->srqs = kcalloc(ndev->srq_count, sizeof(*ndev->srqs), GFP_KERNEL); |
| 1159 | if (!ndev->srqs) |
| 1160 | return -ENOMEM; |
| 1161 | |
| 1162 | for (i = 0; i < ndev->srq_count; i++) { |
| 1163 | ndev->srqs[i] = nvmet_rdma_init_srq(ndev); |
| 1164 | if (IS_ERR(ptr: ndev->srqs[i])) { |
| 1165 | ret = PTR_ERR(ptr: ndev->srqs[i]); |
| 1166 | goto err_srq; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | return 0; |
| 1171 | |
| 1172 | err_srq: |
| 1173 | while (--i >= 0) |
| 1174 | nvmet_rdma_destroy_srq(nsrq: ndev->srqs[i]); |
| 1175 | kfree(objp: ndev->srqs); |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | static void nvmet_rdma_free_dev(struct kref *ref) |
| 1180 | { |
| 1181 | struct nvmet_rdma_device *ndev = |
| 1182 | container_of(ref, struct nvmet_rdma_device, ref); |
| 1183 | |
| 1184 | mutex_lock(&device_list_mutex); |
| 1185 | list_del(entry: &ndev->entry); |
| 1186 | mutex_unlock(lock: &device_list_mutex); |
| 1187 | |
| 1188 | nvmet_rdma_destroy_srqs(ndev); |
| 1189 | ib_dealloc_pd(pd: ndev->pd); |
| 1190 | |
| 1191 | kfree(objp: ndev); |
| 1192 | } |
| 1193 | |
| 1194 | static struct nvmet_rdma_device * |
| 1195 | nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id) |
| 1196 | { |
| 1197 | struct nvmet_rdma_port *port = cm_id->context; |
| 1198 | struct nvmet_port *nport = port->nport; |
| 1199 | struct nvmet_rdma_device *ndev; |
| 1200 | int inline_page_count; |
| 1201 | int inline_sge_count; |
| 1202 | int ret; |
| 1203 | |
| 1204 | mutex_lock(&device_list_mutex); |
| 1205 | list_for_each_entry(ndev, &device_list, entry) { |
| 1206 | if (ndev->device->node_guid == cm_id->device->node_guid && |
| 1207 | kref_get_unless_zero(kref: &ndev->ref)) |
| 1208 | goto out_unlock; |
| 1209 | } |
| 1210 | |
| 1211 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); |
| 1212 | if (!ndev) |
| 1213 | goto out_err; |
| 1214 | |
| 1215 | inline_page_count = num_pages(len: nport->inline_data_size); |
| 1216 | inline_sge_count = max(cm_id->device->attrs.max_sge_rd, |
| 1217 | cm_id->device->attrs.max_recv_sge) - 1; |
| 1218 | if (inline_page_count > inline_sge_count) { |
| 1219 | pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n" , |
| 1220 | nport->inline_data_size, cm_id->device->name, |
| 1221 | inline_sge_count * PAGE_SIZE); |
| 1222 | nport->inline_data_size = inline_sge_count * PAGE_SIZE; |
| 1223 | inline_page_count = inline_sge_count; |
| 1224 | } |
| 1225 | ndev->inline_data_size = nport->inline_data_size; |
| 1226 | ndev->inline_page_count = inline_page_count; |
| 1227 | |
| 1228 | if (nport->pi_enable && !(cm_id->device->attrs.kernel_cap_flags & |
| 1229 | IBK_INTEGRITY_HANDOVER)) { |
| 1230 | pr_warn("T10-PI is not supported by device %s. Disabling it\n" , |
| 1231 | cm_id->device->name); |
| 1232 | nport->pi_enable = false; |
| 1233 | } |
| 1234 | |
| 1235 | ndev->device = cm_id->device; |
| 1236 | kref_init(kref: &ndev->ref); |
| 1237 | |
| 1238 | ndev->pd = ib_alloc_pd(ndev->device, 0); |
| 1239 | if (IS_ERR(ptr: ndev->pd)) |
| 1240 | goto out_free_dev; |
| 1241 | |
| 1242 | if (nvmet_rdma_use_srq) { |
| 1243 | ret = nvmet_rdma_init_srqs(ndev); |
| 1244 | if (ret) |
| 1245 | goto out_free_pd; |
| 1246 | } |
| 1247 | |
| 1248 | list_add(new: &ndev->entry, head: &device_list); |
| 1249 | out_unlock: |
| 1250 | mutex_unlock(lock: &device_list_mutex); |
| 1251 | pr_debug("added %s.\n" , ndev->device->name); |
| 1252 | return ndev; |
| 1253 | |
| 1254 | out_free_pd: |
| 1255 | ib_dealloc_pd(pd: ndev->pd); |
| 1256 | out_free_dev: |
| 1257 | kfree(objp: ndev); |
| 1258 | out_err: |
| 1259 | mutex_unlock(lock: &device_list_mutex); |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | |
| 1263 | static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue) |
| 1264 | { |
| 1265 | struct ib_qp_init_attr qp_attr = { }; |
| 1266 | struct nvmet_rdma_device *ndev = queue->dev; |
| 1267 | int nr_cqe, ret, i, factor; |
| 1268 | |
| 1269 | /* |
| 1270 | * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND. |
| 1271 | */ |
| 1272 | nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size; |
| 1273 | |
| 1274 | queue->cq = ib_cq_pool_get(dev: ndev->device, nr_cqe: nr_cqe + 1, |
| 1275 | comp_vector_hint: queue->comp_vector, poll_ctx: IB_POLL_WORKQUEUE); |
| 1276 | if (IS_ERR(ptr: queue->cq)) { |
| 1277 | ret = PTR_ERR(ptr: queue->cq); |
| 1278 | pr_err("failed to create CQ cqe= %d ret= %d\n" , |
| 1279 | nr_cqe + 1, ret); |
| 1280 | goto out; |
| 1281 | } |
| 1282 | |
| 1283 | qp_attr.qp_context = queue; |
| 1284 | qp_attr.event_handler = nvmet_rdma_qp_event; |
| 1285 | qp_attr.send_cq = queue->cq; |
| 1286 | qp_attr.recv_cq = queue->cq; |
| 1287 | qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 1288 | qp_attr.qp_type = IB_QPT_RC; |
| 1289 | /* +1 for drain */ |
| 1290 | qp_attr.cap.max_send_wr = queue->send_queue_size + 1; |
| 1291 | factor = rdma_rw_mr_factor(device: ndev->device, port_num: queue->cm_id->port_num, |
| 1292 | maxpages: 1 << NVMET_RDMA_MAX_MDTS); |
| 1293 | qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor; |
| 1294 | qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd, |
| 1295 | ndev->device->attrs.max_send_sge); |
| 1296 | |
| 1297 | if (queue->nsrq) { |
| 1298 | qp_attr.srq = queue->nsrq->srq; |
| 1299 | } else { |
| 1300 | /* +1 for drain */ |
| 1301 | qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size; |
| 1302 | qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count; |
| 1303 | } |
| 1304 | |
| 1305 | if (queue->port->pi_enable && queue->host_qid) |
| 1306 | qp_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; |
| 1307 | |
| 1308 | ret = rdma_create_qp(id: queue->cm_id, pd: ndev->pd, qp_init_attr: &qp_attr); |
| 1309 | if (ret) { |
| 1310 | pr_err("failed to create_qp ret= %d\n" , ret); |
| 1311 | goto err_destroy_cq; |
| 1312 | } |
| 1313 | queue->qp = queue->cm_id->qp; |
| 1314 | |
| 1315 | atomic_set(v: &queue->sq_wr_avail, i: qp_attr.cap.max_send_wr); |
| 1316 | |
| 1317 | pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n" , |
| 1318 | __func__, queue->cq->cqe, qp_attr.cap.max_send_sge, |
| 1319 | qp_attr.cap.max_send_wr, queue->cm_id); |
| 1320 | |
| 1321 | if (!queue->nsrq) { |
| 1322 | for (i = 0; i < queue->recv_queue_size; i++) { |
| 1323 | queue->cmds[i].queue = queue; |
| 1324 | ret = nvmet_rdma_post_recv(ndev, cmd: &queue->cmds[i]); |
| 1325 | if (ret) |
| 1326 | goto err_destroy_qp; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | out: |
| 1331 | return ret; |
| 1332 | |
| 1333 | err_destroy_qp: |
| 1334 | rdma_destroy_qp(id: queue->cm_id); |
| 1335 | err_destroy_cq: |
| 1336 | ib_cq_pool_put(cq: queue->cq, nr_cqe: nr_cqe + 1); |
| 1337 | goto out; |
| 1338 | } |
| 1339 | |
| 1340 | static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue) |
| 1341 | { |
| 1342 | ib_drain_qp(qp: queue->qp); |
| 1343 | if (queue->cm_id) |
| 1344 | rdma_destroy_id(id: queue->cm_id); |
| 1345 | ib_destroy_qp(qp: queue->qp); |
| 1346 | ib_cq_pool_put(cq: queue->cq, nr_cqe: queue->recv_queue_size + 2 * |
| 1347 | queue->send_queue_size + 1); |
| 1348 | } |
| 1349 | |
| 1350 | static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue) |
| 1351 | { |
| 1352 | pr_debug("freeing queue %d\n" , queue->idx); |
| 1353 | |
| 1354 | nvmet_sq_destroy(sq: &queue->nvme_sq); |
| 1355 | nvmet_cq_put(cq: &queue->nvme_cq); |
| 1356 | |
| 1357 | nvmet_rdma_destroy_queue_ib(queue); |
| 1358 | if (!queue->nsrq) { |
| 1359 | nvmet_rdma_free_cmds(ndev: queue->dev, cmds: queue->cmds, |
| 1360 | nr_cmds: queue->recv_queue_size, |
| 1361 | admin: !queue->host_qid); |
| 1362 | } |
| 1363 | nvmet_rdma_free_rsps(queue); |
| 1364 | ida_free(&nvmet_rdma_queue_ida, id: queue->idx); |
| 1365 | kfree(objp: queue); |
| 1366 | } |
| 1367 | |
| 1368 | static void nvmet_rdma_release_queue_work(struct work_struct *w) |
| 1369 | { |
| 1370 | struct nvmet_rdma_queue *queue = |
| 1371 | container_of(w, struct nvmet_rdma_queue, release_work); |
| 1372 | struct nvmet_rdma_device *dev = queue->dev; |
| 1373 | |
| 1374 | nvmet_rdma_free_queue(queue); |
| 1375 | |
| 1376 | kref_put(kref: &dev->ref, release: nvmet_rdma_free_dev); |
| 1377 | } |
| 1378 | |
| 1379 | static int |
| 1380 | nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn, |
| 1381 | struct nvmet_rdma_queue *queue) |
| 1382 | { |
| 1383 | struct nvme_rdma_cm_req *req; |
| 1384 | |
| 1385 | req = (struct nvme_rdma_cm_req *)conn->private_data; |
| 1386 | if (!req || conn->private_data_len == 0) |
| 1387 | return NVME_RDMA_CM_INVALID_LEN; |
| 1388 | |
| 1389 | if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0) |
| 1390 | return NVME_RDMA_CM_INVALID_RECFMT; |
| 1391 | |
| 1392 | queue->host_qid = le16_to_cpu(req->qid); |
| 1393 | |
| 1394 | /* |
| 1395 | * req->hsqsize corresponds to our recv queue size plus 1 |
| 1396 | * req->hrqsize corresponds to our send queue size |
| 1397 | */ |
| 1398 | queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1; |
| 1399 | queue->send_queue_size = le16_to_cpu(req->hrqsize); |
| 1400 | |
| 1401 | if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH) |
| 1402 | return NVME_RDMA_CM_INVALID_HSQSIZE; |
| 1403 | |
| 1404 | /* XXX: Should we enforce some kind of max for IO queues? */ |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id, |
| 1410 | enum nvme_rdma_cm_status status) |
| 1411 | { |
| 1412 | struct nvme_rdma_cm_rej rej; |
| 1413 | |
| 1414 | pr_debug("rejecting connect request: status %d (%s)\n" , |
| 1415 | status, nvme_rdma_cm_msg(status)); |
| 1416 | |
| 1417 | rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1418 | rej.sts = cpu_to_le16(status); |
| 1419 | |
| 1420 | return rdma_reject(id: cm_id, private_data: (void *)&rej, private_data_len: sizeof(rej), |
| 1421 | reason: IB_CM_REJ_CONSUMER_DEFINED); |
| 1422 | } |
| 1423 | |
| 1424 | static struct nvmet_rdma_queue * |
| 1425 | nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev, |
| 1426 | struct rdma_cm_id *cm_id, |
| 1427 | struct rdma_cm_event *event) |
| 1428 | { |
| 1429 | struct nvmet_rdma_port *port = cm_id->context; |
| 1430 | struct nvmet_rdma_queue *queue; |
| 1431 | int ret; |
| 1432 | |
| 1433 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); |
| 1434 | if (!queue) { |
| 1435 | ret = NVME_RDMA_CM_NO_RSC; |
| 1436 | goto out_reject; |
| 1437 | } |
| 1438 | |
| 1439 | nvmet_cq_init(cq: &queue->nvme_cq); |
| 1440 | ret = nvmet_sq_init(sq: &queue->nvme_sq, cq: &queue->nvme_cq); |
| 1441 | if (ret) { |
| 1442 | ret = NVME_RDMA_CM_NO_RSC; |
| 1443 | goto out_free_queue; |
| 1444 | } |
| 1445 | |
| 1446 | ret = nvmet_rdma_parse_cm_connect_req(conn: &event->param.conn, queue); |
| 1447 | if (ret) |
| 1448 | goto out_destroy_sq; |
| 1449 | |
| 1450 | /* |
| 1451 | * Schedules the actual release because calling rdma_destroy_id from |
| 1452 | * inside a CM callback would trigger a deadlock. (great API design..) |
| 1453 | */ |
| 1454 | INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work); |
| 1455 | queue->dev = ndev; |
| 1456 | queue->cm_id = cm_id; |
| 1457 | queue->port = port->nport; |
| 1458 | |
| 1459 | spin_lock_init(&queue->state_lock); |
| 1460 | queue->state = NVMET_RDMA_Q_CONNECTING; |
| 1461 | INIT_LIST_HEAD(list: &queue->rsp_wait_list); |
| 1462 | INIT_LIST_HEAD(list: &queue->rsp_wr_wait_list); |
| 1463 | spin_lock_init(&queue->rsp_wr_wait_lock); |
| 1464 | INIT_LIST_HEAD(list: &queue->queue_list); |
| 1465 | |
| 1466 | queue->idx = ida_alloc(ida: &nvmet_rdma_queue_ida, GFP_KERNEL); |
| 1467 | if (queue->idx < 0) { |
| 1468 | ret = NVME_RDMA_CM_NO_RSC; |
| 1469 | goto out_destroy_sq; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Spread the io queues across completion vectors, |
| 1474 | * but still keep all admin queues on vector 0. |
| 1475 | */ |
| 1476 | queue->comp_vector = !queue->host_qid ? 0 : |
| 1477 | queue->idx % ndev->device->num_comp_vectors; |
| 1478 | |
| 1479 | |
| 1480 | ret = nvmet_rdma_alloc_rsps(queue); |
| 1481 | if (ret) { |
| 1482 | ret = NVME_RDMA_CM_NO_RSC; |
| 1483 | goto out_ida_remove; |
| 1484 | } |
| 1485 | |
| 1486 | if (ndev->srqs) { |
| 1487 | queue->nsrq = ndev->srqs[queue->comp_vector % ndev->srq_count]; |
| 1488 | } else { |
| 1489 | queue->cmds = nvmet_rdma_alloc_cmds(ndev, |
| 1490 | nr_cmds: queue->recv_queue_size, |
| 1491 | admin: !queue->host_qid); |
| 1492 | if (IS_ERR(ptr: queue->cmds)) { |
| 1493 | ret = NVME_RDMA_CM_NO_RSC; |
| 1494 | goto out_free_responses; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | ret = nvmet_rdma_create_queue_ib(queue); |
| 1499 | if (ret) { |
| 1500 | pr_err("%s: creating RDMA queue failed (%d).\n" , |
| 1501 | __func__, ret); |
| 1502 | ret = NVME_RDMA_CM_NO_RSC; |
| 1503 | goto out_free_cmds; |
| 1504 | } |
| 1505 | |
| 1506 | return queue; |
| 1507 | |
| 1508 | out_free_cmds: |
| 1509 | if (!queue->nsrq) { |
| 1510 | nvmet_rdma_free_cmds(ndev: queue->dev, cmds: queue->cmds, |
| 1511 | nr_cmds: queue->recv_queue_size, |
| 1512 | admin: !queue->host_qid); |
| 1513 | } |
| 1514 | out_free_responses: |
| 1515 | nvmet_rdma_free_rsps(queue); |
| 1516 | out_ida_remove: |
| 1517 | ida_free(&nvmet_rdma_queue_ida, id: queue->idx); |
| 1518 | out_destroy_sq: |
| 1519 | nvmet_sq_destroy(sq: &queue->nvme_sq); |
| 1520 | out_free_queue: |
| 1521 | nvmet_cq_put(cq: &queue->nvme_cq); |
| 1522 | kfree(objp: queue); |
| 1523 | out_reject: |
| 1524 | nvmet_rdma_cm_reject(cm_id, status: ret); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | |
| 1528 | static void nvmet_rdma_qp_event(struct ib_event *event, void *priv) |
| 1529 | { |
| 1530 | struct nvmet_rdma_queue *queue = priv; |
| 1531 | |
| 1532 | switch (event->event) { |
| 1533 | case IB_EVENT_COMM_EST: |
| 1534 | rdma_notify(id: queue->cm_id, event: event->event); |
| 1535 | break; |
| 1536 | case IB_EVENT_QP_LAST_WQE_REACHED: |
| 1537 | pr_debug("received last WQE reached event for queue=0x%p\n" , |
| 1538 | queue); |
| 1539 | break; |
| 1540 | default: |
| 1541 | pr_err("received IB QP event: %s (%d)\n" , |
| 1542 | ib_event_msg(event->event), event->event); |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id, |
| 1548 | struct nvmet_rdma_queue *queue, |
| 1549 | struct rdma_conn_param *p) |
| 1550 | { |
| 1551 | struct rdma_conn_param param = { }; |
| 1552 | struct nvme_rdma_cm_rep priv = { }; |
| 1553 | int ret = -ENOMEM; |
| 1554 | |
| 1555 | param.rnr_retry_count = 7; |
| 1556 | param.flow_control = 1; |
| 1557 | param.initiator_depth = min_t(u8, p->initiator_depth, |
| 1558 | queue->dev->device->attrs.max_qp_init_rd_atom); |
| 1559 | param.private_data = &priv; |
| 1560 | param.private_data_len = sizeof(priv); |
| 1561 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1562 | priv.crqsize = cpu_to_le16(queue->recv_queue_size); |
| 1563 | |
| 1564 | ret = rdma_accept(id: cm_id, conn_param: ¶m); |
| 1565 | if (ret) |
| 1566 | pr_err("rdma_accept failed (error code = %d)\n" , ret); |
| 1567 | |
| 1568 | return ret; |
| 1569 | } |
| 1570 | |
| 1571 | static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, |
| 1572 | struct rdma_cm_event *event) |
| 1573 | { |
| 1574 | struct nvmet_rdma_device *ndev; |
| 1575 | struct nvmet_rdma_queue *queue; |
| 1576 | int ret = -EINVAL; |
| 1577 | |
| 1578 | ndev = nvmet_rdma_find_get_device(cm_id); |
| 1579 | if (!ndev) { |
| 1580 | nvmet_rdma_cm_reject(cm_id, status: NVME_RDMA_CM_NO_RSC); |
| 1581 | return -ECONNREFUSED; |
| 1582 | } |
| 1583 | |
| 1584 | queue = nvmet_rdma_alloc_queue(ndev, cm_id, event); |
| 1585 | if (!queue) { |
| 1586 | ret = -ENOMEM; |
| 1587 | goto put_device; |
| 1588 | } |
| 1589 | |
| 1590 | if (queue->host_qid == 0) { |
| 1591 | struct nvmet_rdma_queue *q; |
| 1592 | int pending = 0; |
| 1593 | |
| 1594 | /* Check for pending controller teardown */ |
| 1595 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1596 | list_for_each_entry(q, &nvmet_rdma_queue_list, queue_list) { |
| 1597 | if (q->nvme_sq.ctrl == queue->nvme_sq.ctrl && |
| 1598 | q->state == NVMET_RDMA_Q_DISCONNECTING) |
| 1599 | pending++; |
| 1600 | } |
| 1601 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1602 | if (pending > NVMET_RDMA_BACKLOG) |
| 1603 | return NVME_SC_CONNECT_CTRL_BUSY; |
| 1604 | } |
| 1605 | |
| 1606 | ret = nvmet_rdma_cm_accept(cm_id, queue, p: &event->param.conn); |
| 1607 | if (ret) { |
| 1608 | /* |
| 1609 | * Don't destroy the cm_id in free path, as we implicitly |
| 1610 | * destroy the cm_id here with non-zero ret code. |
| 1611 | */ |
| 1612 | queue->cm_id = NULL; |
| 1613 | goto free_queue; |
| 1614 | } |
| 1615 | |
| 1616 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1617 | list_add_tail(new: &queue->queue_list, head: &nvmet_rdma_queue_list); |
| 1618 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1619 | |
| 1620 | return 0; |
| 1621 | |
| 1622 | free_queue: |
| 1623 | nvmet_rdma_free_queue(queue); |
| 1624 | put_device: |
| 1625 | kref_put(kref: &ndev->ref, release: nvmet_rdma_free_dev); |
| 1626 | |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue) |
| 1631 | { |
| 1632 | unsigned long flags; |
| 1633 | |
| 1634 | spin_lock_irqsave(&queue->state_lock, flags); |
| 1635 | if (queue->state != NVMET_RDMA_Q_CONNECTING) { |
| 1636 | pr_warn("trying to establish a connected queue\n" ); |
| 1637 | goto out_unlock; |
| 1638 | } |
| 1639 | queue->state = NVMET_RDMA_Q_LIVE; |
| 1640 | |
| 1641 | while (!list_empty(head: &queue->rsp_wait_list)) { |
| 1642 | struct nvmet_rdma_rsp *cmd; |
| 1643 | |
| 1644 | cmd = list_first_entry(&queue->rsp_wait_list, |
| 1645 | struct nvmet_rdma_rsp, wait_list); |
| 1646 | list_del(entry: &cmd->wait_list); |
| 1647 | |
| 1648 | spin_unlock_irqrestore(lock: &queue->state_lock, flags); |
| 1649 | nvmet_rdma_handle_command(queue, cmd); |
| 1650 | spin_lock_irqsave(&queue->state_lock, flags); |
| 1651 | } |
| 1652 | |
| 1653 | out_unlock: |
| 1654 | spin_unlock_irqrestore(lock: &queue->state_lock, flags); |
| 1655 | } |
| 1656 | |
| 1657 | static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) |
| 1658 | { |
| 1659 | bool disconnect = false; |
| 1660 | unsigned long flags; |
| 1661 | |
| 1662 | pr_debug("cm_id= %p queue->state= %d\n" , queue->cm_id, queue->state); |
| 1663 | |
| 1664 | spin_lock_irqsave(&queue->state_lock, flags); |
| 1665 | switch (queue->state) { |
| 1666 | case NVMET_RDMA_Q_CONNECTING: |
| 1667 | while (!list_empty(head: &queue->rsp_wait_list)) { |
| 1668 | struct nvmet_rdma_rsp *rsp; |
| 1669 | |
| 1670 | rsp = list_first_entry(&queue->rsp_wait_list, |
| 1671 | struct nvmet_rdma_rsp, |
| 1672 | wait_list); |
| 1673 | list_del(entry: &rsp->wait_list); |
| 1674 | nvmet_rdma_put_rsp(rsp); |
| 1675 | } |
| 1676 | fallthrough; |
| 1677 | case NVMET_RDMA_Q_LIVE: |
| 1678 | queue->state = NVMET_RDMA_Q_DISCONNECTING; |
| 1679 | disconnect = true; |
| 1680 | break; |
| 1681 | case NVMET_RDMA_Q_DISCONNECTING: |
| 1682 | break; |
| 1683 | } |
| 1684 | spin_unlock_irqrestore(lock: &queue->state_lock, flags); |
| 1685 | |
| 1686 | if (disconnect) { |
| 1687 | rdma_disconnect(id: queue->cm_id); |
| 1688 | queue_work(wq: nvmet_wq, work: &queue->release_work); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) |
| 1693 | { |
| 1694 | bool disconnect = false; |
| 1695 | |
| 1696 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1697 | if (!list_empty(head: &queue->queue_list)) { |
| 1698 | list_del_init(entry: &queue->queue_list); |
| 1699 | disconnect = true; |
| 1700 | } |
| 1701 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1702 | |
| 1703 | if (disconnect) |
| 1704 | __nvmet_rdma_queue_disconnect(queue); |
| 1705 | } |
| 1706 | |
| 1707 | static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id, |
| 1708 | struct nvmet_rdma_queue *queue) |
| 1709 | { |
| 1710 | WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING); |
| 1711 | |
| 1712 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1713 | if (!list_empty(head: &queue->queue_list)) |
| 1714 | list_del_init(entry: &queue->queue_list); |
| 1715 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1716 | |
| 1717 | pr_err("failed to connect queue %d\n" , queue->idx); |
| 1718 | queue_work(wq: nvmet_wq, work: &queue->release_work); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * nvmet_rdma_device_removal() - Handle RDMA device removal |
| 1723 | * @cm_id: rdma_cm id, used for nvmet port |
| 1724 | * @queue: nvmet rdma queue (cm id qp_context) |
| 1725 | * |
| 1726 | * DEVICE_REMOVAL event notifies us that the RDMA device is about |
| 1727 | * to unplug. Note that this event can be generated on a normal |
| 1728 | * queue cm_id and/or a device bound listener cm_id (where in this |
| 1729 | * case queue will be null). |
| 1730 | * |
| 1731 | * We registered an ib_client to handle device removal for queues, |
| 1732 | * so we only need to handle the listening port cm_ids. In this case |
| 1733 | * we nullify the priv to prevent double cm_id destruction and destroying |
| 1734 | * the cm_id implicitly by returning a non-zero rc to the callout. |
| 1735 | */ |
| 1736 | static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id, |
| 1737 | struct nvmet_rdma_queue *queue) |
| 1738 | { |
| 1739 | struct nvmet_rdma_port *port; |
| 1740 | |
| 1741 | if (queue) { |
| 1742 | /* |
| 1743 | * This is a queue cm_id. we have registered |
| 1744 | * an ib_client to handle queues removal |
| 1745 | * so don't interfere and just return. |
| 1746 | */ |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | port = cm_id->context; |
| 1751 | |
| 1752 | /* |
| 1753 | * This is a listener cm_id. Make sure that |
| 1754 | * future remove_port won't invoke a double |
| 1755 | * cm_id destroy. use atomic xchg to make sure |
| 1756 | * we don't compete with remove_port. |
| 1757 | */ |
| 1758 | if (xchg(&port->cm_id, NULL) != cm_id) |
| 1759 | return 0; |
| 1760 | |
| 1761 | /* |
| 1762 | * We need to return 1 so that the core will destroy |
| 1763 | * its own ID. What a great API design.. |
| 1764 | */ |
| 1765 | return 1; |
| 1766 | } |
| 1767 | |
| 1768 | static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 1769 | struct rdma_cm_event *event) |
| 1770 | { |
| 1771 | struct nvmet_rdma_queue *queue = NULL; |
| 1772 | int ret = 0; |
| 1773 | |
| 1774 | if (cm_id->qp) |
| 1775 | queue = cm_id->qp->qp_context; |
| 1776 | |
| 1777 | pr_debug("%s (%d): status %d id %p\n" , |
| 1778 | rdma_event_msg(event->event), event->event, |
| 1779 | event->status, cm_id); |
| 1780 | |
| 1781 | switch (event->event) { |
| 1782 | case RDMA_CM_EVENT_CONNECT_REQUEST: |
| 1783 | ret = nvmet_rdma_queue_connect(cm_id, event); |
| 1784 | break; |
| 1785 | case RDMA_CM_EVENT_ESTABLISHED: |
| 1786 | nvmet_rdma_queue_established(queue); |
| 1787 | break; |
| 1788 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 1789 | if (!queue) { |
| 1790 | struct nvmet_rdma_port *port = cm_id->context; |
| 1791 | |
| 1792 | queue_delayed_work(wq: nvmet_wq, dwork: &port->repair_work, delay: 0); |
| 1793 | break; |
| 1794 | } |
| 1795 | fallthrough; |
| 1796 | case RDMA_CM_EVENT_DISCONNECTED: |
| 1797 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 1798 | nvmet_rdma_queue_disconnect(queue); |
| 1799 | break; |
| 1800 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
| 1801 | ret = nvmet_rdma_device_removal(cm_id, queue); |
| 1802 | break; |
| 1803 | case RDMA_CM_EVENT_REJECTED: |
| 1804 | pr_debug("Connection rejected: %s\n" , |
| 1805 | rdma_reject_msg(cm_id, event->status)); |
| 1806 | fallthrough; |
| 1807 | case RDMA_CM_EVENT_UNREACHABLE: |
| 1808 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 1809 | nvmet_rdma_queue_connect_fail(cm_id, queue); |
| 1810 | break; |
| 1811 | default: |
| 1812 | pr_err("received unrecognized RDMA CM event %d\n" , |
| 1813 | event->event); |
| 1814 | break; |
| 1815 | } |
| 1816 | |
| 1817 | return ret; |
| 1818 | } |
| 1819 | |
| 1820 | static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl) |
| 1821 | { |
| 1822 | struct nvmet_rdma_queue *queue, *n; |
| 1823 | |
| 1824 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1825 | list_for_each_entry_safe(queue, n, &nvmet_rdma_queue_list, queue_list) { |
| 1826 | if (queue->nvme_sq.ctrl != ctrl) |
| 1827 | continue; |
| 1828 | list_del_init(entry: &queue->queue_list); |
| 1829 | __nvmet_rdma_queue_disconnect(queue); |
| 1830 | } |
| 1831 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1832 | } |
| 1833 | |
| 1834 | static void nvmet_rdma_destroy_port_queues(struct nvmet_rdma_port *port) |
| 1835 | { |
| 1836 | struct nvmet_rdma_queue *queue, *tmp; |
| 1837 | struct nvmet_port *nport = port->nport; |
| 1838 | |
| 1839 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 1840 | list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list, |
| 1841 | queue_list) { |
| 1842 | if (queue->port != nport) |
| 1843 | continue; |
| 1844 | |
| 1845 | list_del_init(entry: &queue->queue_list); |
| 1846 | __nvmet_rdma_queue_disconnect(queue); |
| 1847 | } |
| 1848 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 1849 | } |
| 1850 | |
| 1851 | static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port) |
| 1852 | { |
| 1853 | struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL); |
| 1854 | |
| 1855 | if (cm_id) |
| 1856 | rdma_destroy_id(id: cm_id); |
| 1857 | |
| 1858 | /* |
| 1859 | * Destroy the remaining queues, which are not belong to any |
| 1860 | * controller yet. Do it here after the RDMA-CM was destroyed |
| 1861 | * guarantees that no new queue will be created. |
| 1862 | */ |
| 1863 | nvmet_rdma_destroy_port_queues(port); |
| 1864 | } |
| 1865 | |
| 1866 | static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port) |
| 1867 | { |
| 1868 | struct sockaddr *addr = (struct sockaddr *)&port->addr; |
| 1869 | struct rdma_cm_id *cm_id; |
| 1870 | int ret; |
| 1871 | |
| 1872 | cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port, |
| 1873 | RDMA_PS_TCP, IB_QPT_RC); |
| 1874 | if (IS_ERR(ptr: cm_id)) { |
| 1875 | pr_err("CM ID creation failed\n" ); |
| 1876 | return PTR_ERR(ptr: cm_id); |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * Allow both IPv4 and IPv6 sockets to bind a single port |
| 1881 | * at the same time. |
| 1882 | */ |
| 1883 | ret = rdma_set_afonly(id: cm_id, afonly: 1); |
| 1884 | if (ret) { |
| 1885 | pr_err("rdma_set_afonly failed (%d)\n" , ret); |
| 1886 | goto out_destroy_id; |
| 1887 | } |
| 1888 | |
| 1889 | ret = rdma_bind_addr(id: cm_id, addr); |
| 1890 | if (ret) { |
| 1891 | pr_err("binding CM ID to %pISpcs failed (%d)\n" , addr, ret); |
| 1892 | goto out_destroy_id; |
| 1893 | } |
| 1894 | |
| 1895 | ret = rdma_listen(id: cm_id, NVMET_RDMA_BACKLOG); |
| 1896 | if (ret) { |
| 1897 | pr_err("listening to %pISpcs failed (%d)\n" , addr, ret); |
| 1898 | goto out_destroy_id; |
| 1899 | } |
| 1900 | |
| 1901 | port->cm_id = cm_id; |
| 1902 | return 0; |
| 1903 | |
| 1904 | out_destroy_id: |
| 1905 | rdma_destroy_id(id: cm_id); |
| 1906 | return ret; |
| 1907 | } |
| 1908 | |
| 1909 | static void nvmet_rdma_repair_port_work(struct work_struct *w) |
| 1910 | { |
| 1911 | struct nvmet_rdma_port *port = container_of(to_delayed_work(w), |
| 1912 | struct nvmet_rdma_port, repair_work); |
| 1913 | int ret; |
| 1914 | |
| 1915 | nvmet_rdma_disable_port(port); |
| 1916 | ret = nvmet_rdma_enable_port(port); |
| 1917 | if (ret) |
| 1918 | queue_delayed_work(wq: nvmet_wq, dwork: &port->repair_work, delay: 5 * HZ); |
| 1919 | } |
| 1920 | |
| 1921 | static int nvmet_rdma_add_port(struct nvmet_port *nport) |
| 1922 | { |
| 1923 | struct nvmet_rdma_port *port; |
| 1924 | __kernel_sa_family_t af; |
| 1925 | int ret; |
| 1926 | |
| 1927 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 1928 | if (!port) |
| 1929 | return -ENOMEM; |
| 1930 | |
| 1931 | nport->priv = port; |
| 1932 | port->nport = nport; |
| 1933 | INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work); |
| 1934 | |
| 1935 | switch (nport->disc_addr.adrfam) { |
| 1936 | case NVMF_ADDR_FAMILY_IP4: |
| 1937 | af = AF_INET; |
| 1938 | break; |
| 1939 | case NVMF_ADDR_FAMILY_IP6: |
| 1940 | af = AF_INET6; |
| 1941 | break; |
| 1942 | default: |
| 1943 | pr_err("address family %d not supported\n" , |
| 1944 | nport->disc_addr.adrfam); |
| 1945 | ret = -EINVAL; |
| 1946 | goto out_free_port; |
| 1947 | } |
| 1948 | |
| 1949 | if (nport->inline_data_size < 0) { |
| 1950 | nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE; |
| 1951 | } else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) { |
| 1952 | pr_warn("inline_data_size %u is too large, reducing to %u\n" , |
| 1953 | nport->inline_data_size, |
| 1954 | NVMET_RDMA_MAX_INLINE_DATA_SIZE); |
| 1955 | nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE; |
| 1956 | } |
| 1957 | |
| 1958 | if (nport->max_queue_size < 0) { |
| 1959 | nport->max_queue_size = NVME_RDMA_DEFAULT_QUEUE_SIZE; |
| 1960 | } else if (nport->max_queue_size > NVME_RDMA_MAX_QUEUE_SIZE) { |
| 1961 | pr_warn("max_queue_size %u is too large, reducing to %u\n" , |
| 1962 | nport->max_queue_size, NVME_RDMA_MAX_QUEUE_SIZE); |
| 1963 | nport->max_queue_size = NVME_RDMA_MAX_QUEUE_SIZE; |
| 1964 | } |
| 1965 | |
| 1966 | ret = inet_pton_with_scope(net: &init_net, af, src: nport->disc_addr.traddr, |
| 1967 | port: nport->disc_addr.trsvcid, addr: &port->addr); |
| 1968 | if (ret) { |
| 1969 | pr_err("malformed ip/port passed: %s:%s\n" , |
| 1970 | nport->disc_addr.traddr, nport->disc_addr.trsvcid); |
| 1971 | goto out_free_port; |
| 1972 | } |
| 1973 | |
| 1974 | ret = nvmet_rdma_enable_port(port); |
| 1975 | if (ret) |
| 1976 | goto out_free_port; |
| 1977 | |
| 1978 | pr_info("enabling port %d (%pISpcs)\n" , |
| 1979 | le16_to_cpu(nport->disc_addr.portid), |
| 1980 | (struct sockaddr *)&port->addr); |
| 1981 | |
| 1982 | return 0; |
| 1983 | |
| 1984 | out_free_port: |
| 1985 | kfree(objp: port); |
| 1986 | return ret; |
| 1987 | } |
| 1988 | |
| 1989 | static void nvmet_rdma_remove_port(struct nvmet_port *nport) |
| 1990 | { |
| 1991 | struct nvmet_rdma_port *port = nport->priv; |
| 1992 | |
| 1993 | cancel_delayed_work_sync(dwork: &port->repair_work); |
| 1994 | nvmet_rdma_disable_port(port); |
| 1995 | kfree(objp: port); |
| 1996 | } |
| 1997 | |
| 1998 | static void nvmet_rdma_disc_port_addr(struct nvmet_req *req, |
| 1999 | struct nvmet_port *nport, char *traddr) |
| 2000 | { |
| 2001 | struct nvmet_rdma_port *port = nport->priv; |
| 2002 | struct rdma_cm_id *cm_id = port->cm_id; |
| 2003 | |
| 2004 | if (inet_addr_is_any(addr: &cm_id->route.addr.src_addr)) { |
| 2005 | struct nvmet_rdma_rsp *rsp = |
| 2006 | container_of(req, struct nvmet_rdma_rsp, req); |
| 2007 | struct rdma_cm_id *req_cm_id = rsp->queue->cm_id; |
| 2008 | struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr; |
| 2009 | |
| 2010 | sprintf(buf: traddr, fmt: "%pISc" , addr); |
| 2011 | } else { |
| 2012 | memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | static ssize_t nvmet_rdma_host_port_addr(struct nvmet_ctrl *ctrl, |
| 2017 | char *traddr, size_t traddr_len) |
| 2018 | { |
| 2019 | struct nvmet_sq *nvme_sq = ctrl->sqs[0]; |
| 2020 | struct nvmet_rdma_queue *queue = |
| 2021 | container_of(nvme_sq, struct nvmet_rdma_queue, nvme_sq); |
| 2022 | |
| 2023 | return snprintf(buf: traddr, size: traddr_len, fmt: "%pISc" , |
| 2024 | (struct sockaddr *)&queue->cm_id->route.addr.dst_addr); |
| 2025 | } |
| 2026 | |
| 2027 | static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl) |
| 2028 | { |
| 2029 | if (ctrl->pi_support) |
| 2030 | return NVMET_RDMA_MAX_METADATA_MDTS; |
| 2031 | return NVMET_RDMA_MAX_MDTS; |
| 2032 | } |
| 2033 | |
| 2034 | static u16 nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl *ctrl) |
| 2035 | { |
| 2036 | if (ctrl->pi_support) |
| 2037 | return NVME_RDMA_MAX_METADATA_QUEUE_SIZE; |
| 2038 | return NVME_RDMA_MAX_QUEUE_SIZE; |
| 2039 | } |
| 2040 | |
| 2041 | static const struct nvmet_fabrics_ops nvmet_rdma_ops = { |
| 2042 | .owner = THIS_MODULE, |
| 2043 | .type = NVMF_TRTYPE_RDMA, |
| 2044 | .msdbd = 1, |
| 2045 | .flags = NVMF_KEYED_SGLS | NVMF_METADATA_SUPPORTED, |
| 2046 | .add_port = nvmet_rdma_add_port, |
| 2047 | .remove_port = nvmet_rdma_remove_port, |
| 2048 | .queue_response = nvmet_rdma_queue_response, |
| 2049 | .delete_ctrl = nvmet_rdma_delete_ctrl, |
| 2050 | .disc_traddr = nvmet_rdma_disc_port_addr, |
| 2051 | .host_traddr = nvmet_rdma_host_port_addr, |
| 2052 | .get_mdts = nvmet_rdma_get_mdts, |
| 2053 | .get_max_queue_size = nvmet_rdma_get_max_queue_size, |
| 2054 | }; |
| 2055 | |
| 2056 | static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data) |
| 2057 | { |
| 2058 | struct nvmet_rdma_queue *queue, *tmp; |
| 2059 | struct nvmet_rdma_device *ndev; |
| 2060 | bool found = false; |
| 2061 | |
| 2062 | mutex_lock(&device_list_mutex); |
| 2063 | list_for_each_entry(ndev, &device_list, entry) { |
| 2064 | if (ndev->device == ib_device) { |
| 2065 | found = true; |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
| 2069 | mutex_unlock(lock: &device_list_mutex); |
| 2070 | |
| 2071 | if (!found) |
| 2072 | return; |
| 2073 | |
| 2074 | /* |
| 2075 | * IB Device that is used by nvmet controllers is being removed, |
| 2076 | * delete all queues using this device. |
| 2077 | */ |
| 2078 | mutex_lock(&nvmet_rdma_queue_mutex); |
| 2079 | list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list, |
| 2080 | queue_list) { |
| 2081 | if (queue->dev->device != ib_device) |
| 2082 | continue; |
| 2083 | |
| 2084 | pr_info("Removing queue %d\n" , queue->idx); |
| 2085 | list_del_init(entry: &queue->queue_list); |
| 2086 | __nvmet_rdma_queue_disconnect(queue); |
| 2087 | } |
| 2088 | mutex_unlock(lock: &nvmet_rdma_queue_mutex); |
| 2089 | |
| 2090 | flush_workqueue(nvmet_wq); |
| 2091 | } |
| 2092 | |
| 2093 | static struct ib_client nvmet_rdma_ib_client = { |
| 2094 | .name = "nvmet_rdma" , |
| 2095 | .remove = nvmet_rdma_remove_one |
| 2096 | }; |
| 2097 | |
| 2098 | static int __init nvmet_rdma_init(void) |
| 2099 | { |
| 2100 | int ret; |
| 2101 | |
| 2102 | ret = ib_register_client(client: &nvmet_rdma_ib_client); |
| 2103 | if (ret) |
| 2104 | return ret; |
| 2105 | |
| 2106 | ret = nvmet_register_transport(ops: &nvmet_rdma_ops); |
| 2107 | if (ret) |
| 2108 | goto err_ib_client; |
| 2109 | |
| 2110 | return 0; |
| 2111 | |
| 2112 | err_ib_client: |
| 2113 | ib_unregister_client(client: &nvmet_rdma_ib_client); |
| 2114 | return ret; |
| 2115 | } |
| 2116 | |
| 2117 | static void __exit nvmet_rdma_exit(void) |
| 2118 | { |
| 2119 | nvmet_unregister_transport(ops: &nvmet_rdma_ops); |
| 2120 | ib_unregister_client(client: &nvmet_rdma_ib_client); |
| 2121 | WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list)); |
| 2122 | ida_destroy(ida: &nvmet_rdma_queue_ida); |
| 2123 | } |
| 2124 | |
| 2125 | module_init(nvmet_rdma_init); |
| 2126 | module_exit(nvmet_rdma_exit); |
| 2127 | |
| 2128 | MODULE_DESCRIPTION("NVMe target RDMA transport driver" ); |
| 2129 | MODULE_LICENSE("GPL v2" ); |
| 2130 | MODULE_ALIAS("nvmet-transport-1" ); /* 1 == NVMF_TRTYPE_RDMA */ |
| 2131 | |