blob: ae41b6001c7e25fe11d88b5aa5af41ee8cd545ba [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/ctype.h>
9#include <linux/delay.h>
10#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/nvme.h>
14#include <linux/slab.h>
15#include <linux/string.h>
16#include <linux/wait.h>
17#include <linux/inet.h>
18#include <asm/unaligned.h>
19
20#include <rdma/ib_verbs.h>
21#include <rdma/rdma_cm.h>
22#include <rdma/rw.h>
23
24#include <linux/nvme-rdma.h>
25#include "nvmet.h"
26
27/*
28 * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data
29 */
30#define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE PAGE_SIZE
31#define NVMET_RDMA_MAX_INLINE_SGE 4
32#define NVMET_RDMA_MAX_INLINE_DATA_SIZE max_t(int, SZ_16K, PAGE_SIZE)
33
34struct nvmet_rdma_cmd {
35 struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1];
36 struct ib_cqe cqe;
37 struct ib_recv_wr wr;
38 struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE];
39 struct nvme_command *nvme_cmd;
40 struct nvmet_rdma_queue *queue;
41};
42
43enum {
44 NVMET_RDMA_REQ_INLINE_DATA = (1 << 0),
45 NVMET_RDMA_REQ_INVALIDATE_RKEY = (1 << 1),
46};
47
48struct nvmet_rdma_rsp {
49 struct ib_sge send_sge;
50 struct ib_cqe send_cqe;
51 struct ib_send_wr send_wr;
52
53 struct nvmet_rdma_cmd *cmd;
54 struct nvmet_rdma_queue *queue;
55
56 struct ib_cqe read_cqe;
57 struct rdma_rw_ctx rw;
58
59 struct nvmet_req req;
60
61 bool allocated;
62 u8 n_rdma;
63 u32 flags;
64 u32 invalidate_rkey;
65
66 struct list_head wait_list;
67 struct list_head free_list;
68};
69
70enum nvmet_rdma_queue_state {
71 NVMET_RDMA_Q_CONNECTING,
72 NVMET_RDMA_Q_LIVE,
73 NVMET_RDMA_Q_DISCONNECTING,
74};
75
76struct nvmet_rdma_queue {
77 struct rdma_cm_id *cm_id;
78 struct ib_qp *qp;
79 struct nvmet_port *port;
80 struct ib_cq *cq;
81 atomic_t sq_wr_avail;
82 struct nvmet_rdma_device *dev;
83 spinlock_t state_lock;
84 enum nvmet_rdma_queue_state state;
85 struct nvmet_cq nvme_cq;
86 struct nvmet_sq nvme_sq;
87
88 struct nvmet_rdma_rsp *rsps;
89 struct list_head free_rsps;
90 spinlock_t rsps_lock;
91 struct nvmet_rdma_cmd *cmds;
92
93 struct work_struct release_work;
94 struct list_head rsp_wait_list;
95 struct list_head rsp_wr_wait_list;
96 spinlock_t rsp_wr_wait_lock;
97
98 int idx;
99 int host_qid;
100 int recv_queue_size;
101 int send_queue_size;
102
103 struct list_head queue_list;
104};
105
106struct nvmet_rdma_device {
107 struct ib_device *device;
108 struct ib_pd *pd;
109 struct ib_srq *srq;
110 struct nvmet_rdma_cmd *srq_cmds;
111 size_t srq_size;
112 struct kref ref;
113 struct list_head entry;
114 int inline_data_size;
115 int inline_page_count;
116};
117
118static bool nvmet_rdma_use_srq;
119module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444);
120MODULE_PARM_DESC(use_srq, "Use shared receive queue.");
121
122static DEFINE_IDA(nvmet_rdma_queue_ida);
123static LIST_HEAD(nvmet_rdma_queue_list);
124static DEFINE_MUTEX(nvmet_rdma_queue_mutex);
125
126static LIST_HEAD(device_list);
127static DEFINE_MUTEX(device_list_mutex);
128
129static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp);
130static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc);
131static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
132static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc);
133static void nvmet_rdma_qp_event(struct ib_event *event, void *priv);
134static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue);
135static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
136 struct nvmet_rdma_rsp *r);
137static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
138 struct nvmet_rdma_rsp *r);
139
140static const struct nvmet_fabrics_ops nvmet_rdma_ops;
141
142static int num_pages(int len)
143{
144 return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT);
145}
146
147/* XXX: really should move to a generic header sooner or later.. */
148static inline u32 get_unaligned_le24(const u8 *p)
149{
150 return (u32)p[0] | (u32)p[1] << 8 | (u32)p[2] << 16;
151}
152
153static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp)
154{
155 return nvme_is_write(rsp->req.cmd) &&
156 rsp->req.transfer_len &&
157 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
158}
159
160static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp)
161{
162 return !nvme_is_write(rsp->req.cmd) &&
163 rsp->req.transfer_len &&
164 !rsp->req.cqe->status &&
165 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
166}
167
168static inline struct nvmet_rdma_rsp *
169nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue)
170{
171 struct nvmet_rdma_rsp *rsp;
172 unsigned long flags;
173
174 spin_lock_irqsave(&queue->rsps_lock, flags);
175 rsp = list_first_entry_or_null(&queue->free_rsps,
176 struct nvmet_rdma_rsp, free_list);
177 if (likely(rsp))
178 list_del(&rsp->free_list);
179 spin_unlock_irqrestore(&queue->rsps_lock, flags);
180
181 if (unlikely(!rsp)) {
182 int ret;
183
184 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
185 if (unlikely(!rsp))
186 return NULL;
187 ret = nvmet_rdma_alloc_rsp(queue->dev, rsp);
188 if (unlikely(ret)) {
189 kfree(rsp);
190 return NULL;
191 }
192
193 rsp->allocated = true;
194 }
195
196 return rsp;
197}
198
199static inline void
200nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp)
201{
202 unsigned long flags;
203
204 if (unlikely(rsp->allocated)) {
205 nvmet_rdma_free_rsp(rsp->queue->dev, rsp);
206 kfree(rsp);
207 return;
208 }
209
210 spin_lock_irqsave(&rsp->queue->rsps_lock, flags);
211 list_add_tail(&rsp->free_list, &rsp->queue->free_rsps);
212 spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags);
213}
214
215static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev,
216 struct nvmet_rdma_cmd *c)
217{
218 struct scatterlist *sg;
219 struct ib_sge *sge;
220 int i;
221
222 if (!ndev->inline_data_size)
223 return;
224
225 sg = c->inline_sg;
226 sge = &c->sge[1];
227
228 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
229 if (sge->length)
230 ib_dma_unmap_page(ndev->device, sge->addr,
231 sge->length, DMA_FROM_DEVICE);
232 if (sg_page(sg))
233 __free_page(sg_page(sg));
234 }
235}
236
237static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev,
238 struct nvmet_rdma_cmd *c)
239{
240 struct scatterlist *sg;
241 struct ib_sge *sge;
242 struct page *pg;
243 int len;
244 int i;
245
246 if (!ndev->inline_data_size)
247 return 0;
248
249 sg = c->inline_sg;
250 sg_init_table(sg, ndev->inline_page_count);
251 sge = &c->sge[1];
252 len = ndev->inline_data_size;
253
254 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
255 pg = alloc_page(GFP_KERNEL);
256 if (!pg)
257 goto out_err;
258 sg_assign_page(sg, pg);
259 sge->addr = ib_dma_map_page(ndev->device,
260 pg, 0, PAGE_SIZE, DMA_FROM_DEVICE);
261 if (ib_dma_mapping_error(ndev->device, sge->addr))
262 goto out_err;
263 sge->length = min_t(int, len, PAGE_SIZE);
264 sge->lkey = ndev->pd->local_dma_lkey;
265 len -= sge->length;
266 }
267
268 return 0;
269out_err:
270 for (; i >= 0; i--, sg--, sge--) {
271 if (sge->length)
272 ib_dma_unmap_page(ndev->device, sge->addr,
273 sge->length, DMA_FROM_DEVICE);
274 if (sg_page(sg))
275 __free_page(sg_page(sg));
276 }
277 return -ENOMEM;
278}
279
280static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev,
281 struct nvmet_rdma_cmd *c, bool admin)
282{
283 /* NVMe command / RDMA RECV */
284 c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL);
285 if (!c->nvme_cmd)
286 goto out;
287
288 c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd,
289 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
290 if (ib_dma_mapping_error(ndev->device, c->sge[0].addr))
291 goto out_free_cmd;
292
293 c->sge[0].length = sizeof(*c->nvme_cmd);
294 c->sge[0].lkey = ndev->pd->local_dma_lkey;
295
296 if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c))
297 goto out_unmap_cmd;
298
299 c->cqe.done = nvmet_rdma_recv_done;
300
301 c->wr.wr_cqe = &c->cqe;
302 c->wr.sg_list = c->sge;
303 c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1;
304
305 return 0;
306
307out_unmap_cmd:
308 ib_dma_unmap_single(ndev->device, c->sge[0].addr,
309 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
310out_free_cmd:
311 kfree(c->nvme_cmd);
312
313out:
314 return -ENOMEM;
315}
316
317static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev,
318 struct nvmet_rdma_cmd *c, bool admin)
319{
320 if (!admin)
321 nvmet_rdma_free_inline_pages(ndev, c);
322 ib_dma_unmap_single(ndev->device, c->sge[0].addr,
323 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
324 kfree(c->nvme_cmd);
325}
326
327static struct nvmet_rdma_cmd *
328nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev,
329 int nr_cmds, bool admin)
330{
331 struct nvmet_rdma_cmd *cmds;
332 int ret = -EINVAL, i;
333
334 cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL);
335 if (!cmds)
336 goto out;
337
338 for (i = 0; i < nr_cmds; i++) {
339 ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin);
340 if (ret)
341 goto out_free;
342 }
343
344 return cmds;
345
346out_free:
347 while (--i >= 0)
348 nvmet_rdma_free_cmd(ndev, cmds + i, admin);
349 kfree(cmds);
350out:
351 return ERR_PTR(ret);
352}
353
354static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev,
355 struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin)
356{
357 int i;
358
359 for (i = 0; i < nr_cmds; i++)
360 nvmet_rdma_free_cmd(ndev, cmds + i, admin);
361 kfree(cmds);
362}
363
364static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
365 struct nvmet_rdma_rsp *r)
366{
367 /* NVMe CQE / RDMA SEND */
368 r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL);
369 if (!r->req.cqe)
370 goto out;
371
372 r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe,
373 sizeof(*r->req.cqe), DMA_TO_DEVICE);
374 if (ib_dma_mapping_error(ndev->device, r->send_sge.addr))
375 goto out_free_rsp;
376
377 r->req.p2p_client = &ndev->device->dev;
378 r->send_sge.length = sizeof(*r->req.cqe);
379 r->send_sge.lkey = ndev->pd->local_dma_lkey;
380
381 r->send_cqe.done = nvmet_rdma_send_done;
382
383 r->send_wr.wr_cqe = &r->send_cqe;
384 r->send_wr.sg_list = &r->send_sge;
385 r->send_wr.num_sge = 1;
386 r->send_wr.send_flags = IB_SEND_SIGNALED;
387
388 /* Data In / RDMA READ */
389 r->read_cqe.done = nvmet_rdma_read_data_done;
390 return 0;
391
392out_free_rsp:
393 kfree(r->req.cqe);
394out:
395 return -ENOMEM;
396}
397
398static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
399 struct nvmet_rdma_rsp *r)
400{
401 ib_dma_unmap_single(ndev->device, r->send_sge.addr,
402 sizeof(*r->req.cqe), DMA_TO_DEVICE);
403 kfree(r->req.cqe);
404}
405
406static int
407nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue)
408{
409 struct nvmet_rdma_device *ndev = queue->dev;
410 int nr_rsps = queue->recv_queue_size * 2;
411 int ret = -EINVAL, i;
412
413 queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp),
414 GFP_KERNEL);
415 if (!queue->rsps)
416 goto out;
417
418 for (i = 0; i < nr_rsps; i++) {
419 struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
420
421 ret = nvmet_rdma_alloc_rsp(ndev, rsp);
422 if (ret)
423 goto out_free;
424
425 list_add_tail(&rsp->free_list, &queue->free_rsps);
426 }
427
428 return 0;
429
430out_free:
431 while (--i >= 0)
432 nvmet_rdma_free_rsp(ndev, &queue->rsps[i]);
433 kfree(queue->rsps);
434out:
435 return ret;
436}
437
438static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue)
439{
440 struct nvmet_rdma_device *ndev = queue->dev;
441 int i, nr_rsps = queue->recv_queue_size * 2;
442
443 for (i = 0; i < nr_rsps; i++)
444 nvmet_rdma_free_rsp(ndev, &queue->rsps[i]);
445 kfree(queue->rsps);
446}
447
448static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev,
449 struct nvmet_rdma_cmd *cmd)
450{
451 int ret;
452
453 ib_dma_sync_single_for_device(ndev->device,
454 cmd->sge[0].addr, cmd->sge[0].length,
455 DMA_FROM_DEVICE);
456
457 if (ndev->srq)
458 ret = ib_post_srq_recv(ndev->srq, &cmd->wr, NULL);
459 else
460 ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL);
461
462 if (unlikely(ret))
463 pr_err("post_recv cmd failed\n");
464
465 return ret;
466}
467
468static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue)
469{
470 spin_lock(&queue->rsp_wr_wait_lock);
471 while (!list_empty(&queue->rsp_wr_wait_list)) {
472 struct nvmet_rdma_rsp *rsp;
473 bool ret;
474
475 rsp = list_entry(queue->rsp_wr_wait_list.next,
476 struct nvmet_rdma_rsp, wait_list);
477 list_del(&rsp->wait_list);
478
479 spin_unlock(&queue->rsp_wr_wait_lock);
480 ret = nvmet_rdma_execute_command(rsp);
481 spin_lock(&queue->rsp_wr_wait_lock);
482
483 if (!ret) {
484 list_add(&rsp->wait_list, &queue->rsp_wr_wait_list);
485 break;
486 }
487 }
488 spin_unlock(&queue->rsp_wr_wait_lock);
489}
490
491
492static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
493{
494 struct nvmet_rdma_queue *queue = rsp->queue;
495
496 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
497
498 if (rsp->n_rdma) {
499 rdma_rw_ctx_destroy(&rsp->rw, queue->qp,
500 queue->cm_id->port_num, rsp->req.sg,
501 rsp->req.sg_cnt, nvmet_data_dir(&rsp->req));
502 }
503
504 if (rsp->req.sg != rsp->cmd->inline_sg)
505 nvmet_req_free_sgl(&rsp->req);
506
507 if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
508 nvmet_rdma_process_wr_wait_list(queue);
509
510 nvmet_rdma_put_rsp(rsp);
511}
512
513static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue)
514{
515 if (queue->nvme_sq.ctrl) {
516 nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
517 } else {
518 /*
519 * we didn't setup the controller yet in case
520 * of admin connect error, just disconnect and
521 * cleanup the queue
522 */
523 nvmet_rdma_queue_disconnect(queue);
524 }
525}
526
527static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
528{
529 struct nvmet_rdma_rsp *rsp =
530 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe);
531 struct nvmet_rdma_queue *queue = cq->cq_context;
532
533 nvmet_rdma_release_rsp(rsp);
534
535 if (unlikely(wc->status != IB_WC_SUCCESS &&
536 wc->status != IB_WC_WR_FLUSH_ERR)) {
537 pr_err("SEND for CQE 0x%p failed with status %s (%d).\n",
538 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
539 nvmet_rdma_error_comp(queue);
540 }
541}
542
543static void nvmet_rdma_queue_response(struct nvmet_req *req)
544{
545 struct nvmet_rdma_rsp *rsp =
546 container_of(req, struct nvmet_rdma_rsp, req);
547 struct rdma_cm_id *cm_id = rsp->queue->cm_id;
548 struct ib_send_wr *first_wr;
549
550 if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) {
551 rsp->send_wr.opcode = IB_WR_SEND_WITH_INV;
552 rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey;
553 } else {
554 rsp->send_wr.opcode = IB_WR_SEND;
555 }
556
557 if (nvmet_rdma_need_data_out(rsp))
558 first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
559 cm_id->port_num, NULL, &rsp->send_wr);
560 else
561 first_wr = &rsp->send_wr;
562
563 nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd);
564
565 ib_dma_sync_single_for_device(rsp->queue->dev->device,
566 rsp->send_sge.addr, rsp->send_sge.length,
567 DMA_TO_DEVICE);
568
569 if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) {
570 pr_err("sending cmd response failed\n");
571 nvmet_rdma_release_rsp(rsp);
572 }
573}
574
575static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc)
576{
577 struct nvmet_rdma_rsp *rsp =
578 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe);
579 struct nvmet_rdma_queue *queue = cq->cq_context;
580
581 WARN_ON(rsp->n_rdma <= 0);
582 atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
583 rdma_rw_ctx_destroy(&rsp->rw, queue->qp,
584 queue->cm_id->port_num, rsp->req.sg,
585 rsp->req.sg_cnt, nvmet_data_dir(&rsp->req));
586 rsp->n_rdma = 0;
587
588 if (unlikely(wc->status != IB_WC_SUCCESS)) {
589 nvmet_req_uninit(&rsp->req);
590 nvmet_rdma_release_rsp(rsp);
591 if (wc->status != IB_WC_WR_FLUSH_ERR) {
592 pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n",
593 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
594 nvmet_rdma_error_comp(queue);
595 }
596 return;
597 }
598
599 nvmet_req_execute(&rsp->req);
600}
601
602static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len,
603 u64 off)
604{
605 int sg_count = num_pages(len);
606 struct scatterlist *sg;
607 int i;
608
609 sg = rsp->cmd->inline_sg;
610 for (i = 0; i < sg_count; i++, sg++) {
611 if (i < sg_count - 1)
612 sg_unmark_end(sg);
613 else
614 sg_mark_end(sg);
615 sg->offset = off;
616 sg->length = min_t(int, len, PAGE_SIZE - off);
617 len -= sg->length;
618 if (!i)
619 off = 0;
620 }
621
622 rsp->req.sg = rsp->cmd->inline_sg;
623 rsp->req.sg_cnt = sg_count;
624}
625
626static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)
627{
628 struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl;
629 u64 off = le64_to_cpu(sgl->addr);
630 u32 len = le32_to_cpu(sgl->length);
631
632 if (!nvme_is_write(rsp->req.cmd)) {
633 rsp->req.error_loc =
634 offsetof(struct nvme_common_command, opcode);
635 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
636 }
637
638 if (off + len > rsp->queue->dev->inline_data_size) {
639 pr_err("invalid inline data offset!\n");
640 return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
641 }
642
643 /* no data command? */
644 if (!len)
645 return 0;
646
647 nvmet_rdma_use_inline_sg(rsp, len, off);
648 rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA;
649 rsp->req.transfer_len += len;
650 return 0;
651}
652
653static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
654 struct nvme_keyed_sgl_desc *sgl, bool invalidate)
655{
656 struct rdma_cm_id *cm_id = rsp->queue->cm_id;
657 u64 addr = le64_to_cpu(sgl->addr);
658 u32 key = get_unaligned_le32(sgl->key);
659 int ret;
660
661 rsp->req.transfer_len = get_unaligned_le24(sgl->length);
662
663 /* no data command? */
664 if (!rsp->req.transfer_len)
665 return 0;
666
667 ret = nvmet_req_alloc_sgl(&rsp->req);
668 if (ret < 0)
669 goto error_out;
670
671 ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
672 rsp->req.sg, rsp->req.sg_cnt, 0, addr, key,
673 nvmet_data_dir(&rsp->req));
674 if (ret < 0)
675 goto error_out;
676 rsp->n_rdma += ret;
677
678 if (invalidate) {
679 rsp->invalidate_rkey = key;
680 rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY;
681 }
682
683 return 0;
684
685error_out:
686 rsp->req.transfer_len = 0;
687 return NVME_SC_INTERNAL;
688}
689
690static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)
691{
692 struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl;
693
694 switch (sgl->type >> 4) {
695 case NVME_SGL_FMT_DATA_DESC:
696 switch (sgl->type & 0xf) {
697 case NVME_SGL_FMT_OFFSET:
698 return nvmet_rdma_map_sgl_inline(rsp);
699 default:
700 pr_err("invalid SGL subtype: %#x\n", sgl->type);
701 rsp->req.error_loc =
702 offsetof(struct nvme_common_command, dptr);
703 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
704 }
705 case NVME_KEY_SGL_FMT_DATA_DESC:
706 switch (sgl->type & 0xf) {
707 case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE:
708 return nvmet_rdma_map_sgl_keyed(rsp, sgl, true);
709 case NVME_SGL_FMT_ADDRESS:
710 return nvmet_rdma_map_sgl_keyed(rsp, sgl, false);
711 default:
712 pr_err("invalid SGL subtype: %#x\n", sgl->type);
713 rsp->req.error_loc =
714 offsetof(struct nvme_common_command, dptr);
715 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
716 }
717 default:
718 pr_err("invalid SGL type: %#x\n", sgl->type);
719 rsp->req.error_loc = offsetof(struct nvme_common_command, dptr);
720 return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR;
721 }
722}
723
724static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp)
725{
726 struct nvmet_rdma_queue *queue = rsp->queue;
727
728 if (unlikely(atomic_sub_return(1 + rsp->n_rdma,
729 &queue->sq_wr_avail) < 0)) {
730 pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
731 1 + rsp->n_rdma, queue->idx,
732 queue->nvme_sq.ctrl->cntlid);
733 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
734 return false;
735 }
736
737 if (nvmet_rdma_need_data_in(rsp)) {
738 if (rdma_rw_ctx_post(&rsp->rw, queue->qp,
739 queue->cm_id->port_num, &rsp->read_cqe, NULL))
740 nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR);
741 } else {
742 nvmet_req_execute(&rsp->req);
743 }
744
745 return true;
746}
747
748static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
749 struct nvmet_rdma_rsp *cmd)
750{
751 u16 status;
752
753 ib_dma_sync_single_for_cpu(queue->dev->device,
754 cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length,
755 DMA_FROM_DEVICE);
756 ib_dma_sync_single_for_cpu(queue->dev->device,
757 cmd->send_sge.addr, cmd->send_sge.length,
758 DMA_TO_DEVICE);
759
760 if (!nvmet_req_init(&cmd->req, &queue->nvme_cq,
761 &queue->nvme_sq, &nvmet_rdma_ops))
762 return;
763
764 status = nvmet_rdma_map_sgl(cmd);
765 if (status)
766 goto out_err;
767
768 if (unlikely(!nvmet_rdma_execute_command(cmd))) {
769 spin_lock(&queue->rsp_wr_wait_lock);
770 list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list);
771 spin_unlock(&queue->rsp_wr_wait_lock);
772 }
773
774 return;
775
776out_err:
777 nvmet_req_complete(&cmd->req, status);
778}
779
780static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
781{
782 struct nvmet_rdma_cmd *cmd =
783 container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe);
784 struct nvmet_rdma_queue *queue = cq->cq_context;
785 struct nvmet_rdma_rsp *rsp;
786
787 if (unlikely(wc->status != IB_WC_SUCCESS)) {
788 if (wc->status != IB_WC_WR_FLUSH_ERR) {
789 pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
790 wc->wr_cqe, ib_wc_status_msg(wc->status),
791 wc->status);
792 nvmet_rdma_error_comp(queue);
793 }
794 return;
795 }
796
797 if (unlikely(wc->byte_len < sizeof(struct nvme_command))) {
798 pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
799 nvmet_rdma_error_comp(queue);
800 return;
801 }
802
803 cmd->queue = queue;
804 rsp = nvmet_rdma_get_rsp(queue);
805 if (unlikely(!rsp)) {
806 /*
807 * we get here only under memory pressure,
808 * silently drop and have the host retry
809 * as we can't even fail it.
810 */
811 nvmet_rdma_post_recv(queue->dev, cmd);
812 return;
813 }
814 rsp->queue = queue;
815 rsp->cmd = cmd;
816 rsp->flags = 0;
817 rsp->req.cmd = cmd->nvme_cmd;
818 rsp->req.port = queue->port;
819 rsp->n_rdma = 0;
820
821 if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
822 unsigned long flags;
823
824 spin_lock_irqsave(&queue->state_lock, flags);
825 if (queue->state == NVMET_RDMA_Q_CONNECTING)
826 list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
827 else
828 nvmet_rdma_put_rsp(rsp);
829 spin_unlock_irqrestore(&queue->state_lock, flags);
830 return;
831 }
832
833 nvmet_rdma_handle_command(queue, rsp);
834}
835
836static void nvmet_rdma_destroy_srq(struct nvmet_rdma_device *ndev)
837{
838 if (!ndev->srq)
839 return;
840
841 nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false);
842 ib_destroy_srq(ndev->srq);
843}
844
845static int nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev)
846{
847 struct ib_srq_init_attr srq_attr = { NULL, };
848 struct ib_srq *srq;
849 size_t srq_size;
850 int ret, i;
851
852 srq_size = 4095; /* XXX: tune */
853
854 srq_attr.attr.max_wr = srq_size;
855 srq_attr.attr.max_sge = 1 + ndev->inline_page_count;
856 srq_attr.attr.srq_limit = 0;
857 srq_attr.srq_type = IB_SRQT_BASIC;
858 srq = ib_create_srq(ndev->pd, &srq_attr);
859 if (IS_ERR(srq)) {
860 /*
861 * If SRQs aren't supported we just go ahead and use normal
862 * non-shared receive queues.
863 */
864 pr_info("SRQ requested but not supported.\n");
865 return 0;
866 }
867
868 ndev->srq_cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false);
869 if (IS_ERR(ndev->srq_cmds)) {
870 ret = PTR_ERR(ndev->srq_cmds);
871 goto out_destroy_srq;
872 }
873
874 ndev->srq = srq;
875 ndev->srq_size = srq_size;
876
877 for (i = 0; i < srq_size; i++) {
878 ret = nvmet_rdma_post_recv(ndev, &ndev->srq_cmds[i]);
879 if (ret)
880 goto out_free_cmds;
881 }
882
883 return 0;
884
885out_free_cmds:
886 nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false);
887out_destroy_srq:
888 ib_destroy_srq(srq);
889 return ret;
890}
891
892static void nvmet_rdma_free_dev(struct kref *ref)
893{
894 struct nvmet_rdma_device *ndev =
895 container_of(ref, struct nvmet_rdma_device, ref);
896
897 mutex_lock(&device_list_mutex);
898 list_del(&ndev->entry);
899 mutex_unlock(&device_list_mutex);
900
901 nvmet_rdma_destroy_srq(ndev);
902 ib_dealloc_pd(ndev->pd);
903
904 kfree(ndev);
905}
906
907static struct nvmet_rdma_device *
908nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id)
909{
910 struct nvmet_port *port = cm_id->context;
911 struct nvmet_rdma_device *ndev;
912 int inline_page_count;
913 int inline_sge_count;
914 int ret;
915
916 mutex_lock(&device_list_mutex);
917 list_for_each_entry(ndev, &device_list, entry) {
918 if (ndev->device->node_guid == cm_id->device->node_guid &&
919 kref_get_unless_zero(&ndev->ref))
920 goto out_unlock;
921 }
922
923 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
924 if (!ndev)
925 goto out_err;
926
927 inline_page_count = num_pages(port->inline_data_size);
928 inline_sge_count = max(cm_id->device->attrs.max_sge_rd,
929 cm_id->device->attrs.max_recv_sge) - 1;
930 if (inline_page_count > inline_sge_count) {
931 pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n",
932 port->inline_data_size, cm_id->device->name,
933 inline_sge_count * PAGE_SIZE);
934 port->inline_data_size = inline_sge_count * PAGE_SIZE;
935 inline_page_count = inline_sge_count;
936 }
937 ndev->inline_data_size = port->inline_data_size;
938 ndev->inline_page_count = inline_page_count;
939 ndev->device = cm_id->device;
940 kref_init(&ndev->ref);
941
942 ndev->pd = ib_alloc_pd(ndev->device, 0);
943 if (IS_ERR(ndev->pd))
944 goto out_free_dev;
945
946 if (nvmet_rdma_use_srq) {
947 ret = nvmet_rdma_init_srq(ndev);
948 if (ret)
949 goto out_free_pd;
950 }
951
952 list_add(&ndev->entry, &device_list);
953out_unlock:
954 mutex_unlock(&device_list_mutex);
955 pr_debug("added %s.\n", ndev->device->name);
956 return ndev;
957
958out_free_pd:
959 ib_dealloc_pd(ndev->pd);
960out_free_dev:
961 kfree(ndev);
962out_err:
963 mutex_unlock(&device_list_mutex);
964 return NULL;
965}
966
967static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue)
968{
969 struct ib_qp_init_attr qp_attr;
970 struct nvmet_rdma_device *ndev = queue->dev;
971 int comp_vector, nr_cqe, ret, i;
972
973 /*
974 * Spread the io queues across completion vectors,
975 * but still keep all admin queues on vector 0.
976 */
977 comp_vector = !queue->host_qid ? 0 :
978 queue->idx % ndev->device->num_comp_vectors;
979
980 /*
981 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
982 */
983 nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size;
984
985 queue->cq = ib_alloc_cq(ndev->device, queue,
986 nr_cqe + 1, comp_vector,
987 IB_POLL_WORKQUEUE);
988 if (IS_ERR(queue->cq)) {
989 ret = PTR_ERR(queue->cq);
990 pr_err("failed to create CQ cqe= %d ret= %d\n",
991 nr_cqe + 1, ret);
992 goto out;
993 }
994
995 memset(&qp_attr, 0, sizeof(qp_attr));
996 qp_attr.qp_context = queue;
997 qp_attr.event_handler = nvmet_rdma_qp_event;
998 qp_attr.send_cq = queue->cq;
999 qp_attr.recv_cq = queue->cq;
1000 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1001 qp_attr.qp_type = IB_QPT_RC;
1002 /* +1 for drain */
1003 qp_attr.cap.max_send_wr = queue->send_queue_size + 1;
1004 qp_attr.cap.max_rdma_ctxs = queue->send_queue_size;
1005 qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd,
1006 ndev->device->attrs.max_send_sge);
1007
1008 if (ndev->srq) {
1009 qp_attr.srq = ndev->srq;
1010 } else {
1011 /* +1 for drain */
1012 qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size;
1013 qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count;
1014 }
1015
1016 ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr);
1017 if (ret) {
1018 pr_err("failed to create_qp ret= %d\n", ret);
1019 goto err_destroy_cq;
1020 }
1021 queue->qp = queue->cm_id->qp;
1022
1023 atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr);
1024
1025 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1026 __func__, queue->cq->cqe, qp_attr.cap.max_send_sge,
1027 qp_attr.cap.max_send_wr, queue->cm_id);
1028
1029 if (!ndev->srq) {
1030 for (i = 0; i < queue->recv_queue_size; i++) {
1031 queue->cmds[i].queue = queue;
1032 ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]);
1033 if (ret)
1034 goto err_destroy_qp;
1035 }
1036 }
1037
1038out:
1039 return ret;
1040
1041err_destroy_qp:
1042 rdma_destroy_qp(queue->cm_id);
1043err_destroy_cq:
1044 ib_free_cq(queue->cq);
1045 goto out;
1046}
1047
1048static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue)
1049{
1050 ib_drain_qp(queue->qp);
1051 if (queue->cm_id)
1052 rdma_destroy_id(queue->cm_id);
1053 ib_destroy_qp(queue->qp);
1054 ib_free_cq(queue->cq);
1055}
1056
1057static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue)
1058{
1059 pr_debug("freeing queue %d\n", queue->idx);
1060
1061 nvmet_sq_destroy(&queue->nvme_sq);
1062
1063 nvmet_rdma_destroy_queue_ib(queue);
1064 if (!queue->dev->srq) {
1065 nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1066 queue->recv_queue_size,
1067 !queue->host_qid);
1068 }
1069 nvmet_rdma_free_rsps(queue);
1070 ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
1071 kfree(queue);
1072}
1073
1074static void nvmet_rdma_release_queue_work(struct work_struct *w)
1075{
1076 struct nvmet_rdma_queue *queue =
1077 container_of(w, struct nvmet_rdma_queue, release_work);
1078 struct nvmet_rdma_device *dev = queue->dev;
1079
1080 nvmet_rdma_free_queue(queue);
1081
1082 kref_put(&dev->ref, nvmet_rdma_free_dev);
1083}
1084
1085static int
1086nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn,
1087 struct nvmet_rdma_queue *queue)
1088{
1089 struct nvme_rdma_cm_req *req;
1090
1091 req = (struct nvme_rdma_cm_req *)conn->private_data;
1092 if (!req || conn->private_data_len == 0)
1093 return NVME_RDMA_CM_INVALID_LEN;
1094
1095 if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0)
1096 return NVME_RDMA_CM_INVALID_RECFMT;
1097
1098 queue->host_qid = le16_to_cpu(req->qid);
1099
1100 /*
1101 * req->hsqsize corresponds to our recv queue size plus 1
1102 * req->hrqsize corresponds to our send queue size
1103 */
1104 queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1;
1105 queue->send_queue_size = le16_to_cpu(req->hrqsize);
1106
1107 if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH)
1108 return NVME_RDMA_CM_INVALID_HSQSIZE;
1109
1110 /* XXX: Should we enforce some kind of max for IO queues? */
1111
1112 return 0;
1113}
1114
1115static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id,
1116 enum nvme_rdma_cm_status status)
1117{
1118 struct nvme_rdma_cm_rej rej;
1119
1120 pr_debug("rejecting connect request: status %d (%s)\n",
1121 status, nvme_rdma_cm_msg(status));
1122
1123 rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1124 rej.sts = cpu_to_le16(status);
1125
1126 return rdma_reject(cm_id, (void *)&rej, sizeof(rej));
1127}
1128
1129static struct nvmet_rdma_queue *
1130nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev,
1131 struct rdma_cm_id *cm_id,
1132 struct rdma_cm_event *event)
1133{
1134 struct nvmet_rdma_queue *queue;
1135 int ret;
1136
1137 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1138 if (!queue) {
1139 ret = NVME_RDMA_CM_NO_RSC;
1140 goto out_reject;
1141 }
1142
1143 ret = nvmet_sq_init(&queue->nvme_sq);
1144 if (ret) {
1145 ret = NVME_RDMA_CM_NO_RSC;
1146 goto out_free_queue;
1147 }
1148
1149 ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue);
1150 if (ret)
1151 goto out_destroy_sq;
1152
1153 /*
1154 * Schedules the actual release because calling rdma_destroy_id from
1155 * inside a CM callback would trigger a deadlock. (great API design..)
1156 */
1157 INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work);
1158 queue->dev = ndev;
1159 queue->cm_id = cm_id;
1160
1161 spin_lock_init(&queue->state_lock);
1162 queue->state = NVMET_RDMA_Q_CONNECTING;
1163 INIT_LIST_HEAD(&queue->rsp_wait_list);
1164 INIT_LIST_HEAD(&queue->rsp_wr_wait_list);
1165 spin_lock_init(&queue->rsp_wr_wait_lock);
1166 INIT_LIST_HEAD(&queue->free_rsps);
1167 spin_lock_init(&queue->rsps_lock);
1168 INIT_LIST_HEAD(&queue->queue_list);
1169
1170 queue->idx = ida_simple_get(&nvmet_rdma_queue_ida, 0, 0, GFP_KERNEL);
1171 if (queue->idx < 0) {
1172 ret = NVME_RDMA_CM_NO_RSC;
1173 goto out_destroy_sq;
1174 }
1175
1176 ret = nvmet_rdma_alloc_rsps(queue);
1177 if (ret) {
1178 ret = NVME_RDMA_CM_NO_RSC;
1179 goto out_ida_remove;
1180 }
1181
1182 if (!ndev->srq) {
1183 queue->cmds = nvmet_rdma_alloc_cmds(ndev,
1184 queue->recv_queue_size,
1185 !queue->host_qid);
1186 if (IS_ERR(queue->cmds)) {
1187 ret = NVME_RDMA_CM_NO_RSC;
1188 goto out_free_responses;
1189 }
1190 }
1191
1192 ret = nvmet_rdma_create_queue_ib(queue);
1193 if (ret) {
1194 pr_err("%s: creating RDMA queue failed (%d).\n",
1195 __func__, ret);
1196 ret = NVME_RDMA_CM_NO_RSC;
1197 goto out_free_cmds;
1198 }
1199
1200 return queue;
1201
1202out_free_cmds:
1203 if (!ndev->srq) {
1204 nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1205 queue->recv_queue_size,
1206 !queue->host_qid);
1207 }
1208out_free_responses:
1209 nvmet_rdma_free_rsps(queue);
1210out_ida_remove:
1211 ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
1212out_destroy_sq:
1213 nvmet_sq_destroy(&queue->nvme_sq);
1214out_free_queue:
1215 kfree(queue);
1216out_reject:
1217 nvmet_rdma_cm_reject(cm_id, ret);
1218 return NULL;
1219}
1220
1221static void nvmet_rdma_qp_event(struct ib_event *event, void *priv)
1222{
1223 struct nvmet_rdma_queue *queue = priv;
1224
1225 switch (event->event) {
1226 case IB_EVENT_COMM_EST:
1227 rdma_notify(queue->cm_id, event->event);
1228 break;
1229 default:
1230 pr_err("received IB QP event: %s (%d)\n",
1231 ib_event_msg(event->event), event->event);
1232 break;
1233 }
1234}
1235
1236static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id,
1237 struct nvmet_rdma_queue *queue,
1238 struct rdma_conn_param *p)
1239{
1240 struct rdma_conn_param param = { };
1241 struct nvme_rdma_cm_rep priv = { };
1242 int ret = -ENOMEM;
1243
1244 param.rnr_retry_count = 7;
1245 param.flow_control = 1;
1246 param.initiator_depth = min_t(u8, p->initiator_depth,
1247 queue->dev->device->attrs.max_qp_init_rd_atom);
1248 param.private_data = &priv;
1249 param.private_data_len = sizeof(priv);
1250 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1251 priv.crqsize = cpu_to_le16(queue->recv_queue_size);
1252
1253 ret = rdma_accept(cm_id, &param);
1254 if (ret)
1255 pr_err("rdma_accept failed (error code = %d)\n", ret);
1256
1257 return ret;
1258}
1259
1260static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id,
1261 struct rdma_cm_event *event)
1262{
1263 struct nvmet_rdma_device *ndev;
1264 struct nvmet_rdma_queue *queue;
1265 int ret = -EINVAL;
1266
1267 ndev = nvmet_rdma_find_get_device(cm_id);
1268 if (!ndev) {
1269 nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC);
1270 return -ECONNREFUSED;
1271 }
1272
1273 queue = nvmet_rdma_alloc_queue(ndev, cm_id, event);
1274 if (!queue) {
1275 ret = -ENOMEM;
1276 goto put_device;
1277 }
1278 queue->port = cm_id->context;
1279
1280 if (queue->host_qid == 0) {
1281 /* Let inflight controller teardown complete */
1282 flush_scheduled_work();
1283 }
1284
1285 ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn);
1286 if (ret) {
1287 /*
1288 * Don't destroy the cm_id in free path, as we implicitly
1289 * destroy the cm_id here with non-zero ret code.
1290 */
1291 queue->cm_id = NULL;
1292 goto free_queue;
1293 }
1294
1295 mutex_lock(&nvmet_rdma_queue_mutex);
1296 list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list);
1297 mutex_unlock(&nvmet_rdma_queue_mutex);
1298
1299 return 0;
1300
1301free_queue:
1302 nvmet_rdma_free_queue(queue);
1303put_device:
1304 kref_put(&ndev->ref, nvmet_rdma_free_dev);
1305
1306 return ret;
1307}
1308
1309static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue)
1310{
1311 unsigned long flags;
1312
1313 spin_lock_irqsave(&queue->state_lock, flags);
1314 if (queue->state != NVMET_RDMA_Q_CONNECTING) {
1315 pr_warn("trying to establish a connected queue\n");
1316 goto out_unlock;
1317 }
1318 queue->state = NVMET_RDMA_Q_LIVE;
1319
1320 while (!list_empty(&queue->rsp_wait_list)) {
1321 struct nvmet_rdma_rsp *cmd;
1322
1323 cmd = list_first_entry(&queue->rsp_wait_list,
1324 struct nvmet_rdma_rsp, wait_list);
1325 list_del(&cmd->wait_list);
1326
1327 spin_unlock_irqrestore(&queue->state_lock, flags);
1328 nvmet_rdma_handle_command(queue, cmd);
1329 spin_lock_irqsave(&queue->state_lock, flags);
1330 }
1331
1332out_unlock:
1333 spin_unlock_irqrestore(&queue->state_lock, flags);
1334}
1335
1336static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1337{
1338 bool disconnect = false;
1339 unsigned long flags;
1340
1341 pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state);
1342
1343 spin_lock_irqsave(&queue->state_lock, flags);
1344 switch (queue->state) {
1345 case NVMET_RDMA_Q_CONNECTING:
1346 while (!list_empty(&queue->rsp_wait_list)) {
1347 struct nvmet_rdma_rsp *rsp;
1348
1349 rsp = list_first_entry(&queue->rsp_wait_list,
1350 struct nvmet_rdma_rsp,
1351 wait_list);
1352 list_del(&rsp->wait_list);
1353 nvmet_rdma_put_rsp(rsp);
1354 }
1355 fallthrough;
1356 case NVMET_RDMA_Q_LIVE:
1357 queue->state = NVMET_RDMA_Q_DISCONNECTING;
1358 disconnect = true;
1359 break;
1360 case NVMET_RDMA_Q_DISCONNECTING:
1361 break;
1362 }
1363 spin_unlock_irqrestore(&queue->state_lock, flags);
1364
1365 if (disconnect) {
1366 rdma_disconnect(queue->cm_id);
1367 schedule_work(&queue->release_work);
1368 }
1369}
1370
1371static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1372{
1373 bool disconnect = false;
1374
1375 mutex_lock(&nvmet_rdma_queue_mutex);
1376 if (!list_empty(&queue->queue_list)) {
1377 list_del_init(&queue->queue_list);
1378 disconnect = true;
1379 }
1380 mutex_unlock(&nvmet_rdma_queue_mutex);
1381
1382 if (disconnect)
1383 __nvmet_rdma_queue_disconnect(queue);
1384}
1385
1386static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id,
1387 struct nvmet_rdma_queue *queue)
1388{
1389 WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING);
1390
1391 mutex_lock(&nvmet_rdma_queue_mutex);
1392 if (!list_empty(&queue->queue_list))
1393 list_del_init(&queue->queue_list);
1394 mutex_unlock(&nvmet_rdma_queue_mutex);
1395
1396 pr_err("failed to connect queue %d\n", queue->idx);
1397 schedule_work(&queue->release_work);
1398}
1399
1400/**
1401 * nvme_rdma_device_removal() - Handle RDMA device removal
1402 * @cm_id: rdma_cm id, used for nvmet port
1403 * @queue: nvmet rdma queue (cm id qp_context)
1404 *
1405 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1406 * to unplug. Note that this event can be generated on a normal
1407 * queue cm_id and/or a device bound listener cm_id (where in this
1408 * case queue will be null).
1409 *
1410 * We registered an ib_client to handle device removal for queues,
1411 * so we only need to handle the listening port cm_ids. In this case
1412 * we nullify the priv to prevent double cm_id destruction and destroying
1413 * the cm_id implicitely by returning a non-zero rc to the callout.
1414 */
1415static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id,
1416 struct nvmet_rdma_queue *queue)
1417{
1418 struct nvmet_port *port;
1419
1420 if (queue) {
1421 /*
1422 * This is a queue cm_id. we have registered
1423 * an ib_client to handle queues removal
1424 * so don't interfear and just return.
1425 */
1426 return 0;
1427 }
1428
1429 port = cm_id->context;
1430
1431 /*
1432 * This is a listener cm_id. Make sure that
1433 * future remove_port won't invoke a double
1434 * cm_id destroy. use atomic xchg to make sure
1435 * we don't compete with remove_port.
1436 */
1437 if (xchg(&port->priv, NULL) != cm_id)
1438 return 0;
1439
1440 /*
1441 * We need to return 1 so that the core will destroy
1442 * it's own ID. What a great API design..
1443 */
1444 return 1;
1445}
1446
1447static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id,
1448 struct rdma_cm_event *event)
1449{
1450 struct nvmet_rdma_queue *queue = NULL;
1451 int ret = 0;
1452
1453 if (cm_id->qp)
1454 queue = cm_id->qp->qp_context;
1455
1456 pr_debug("%s (%d): status %d id %p\n",
1457 rdma_event_msg(event->event), event->event,
1458 event->status, cm_id);
1459
1460 switch (event->event) {
1461 case RDMA_CM_EVENT_CONNECT_REQUEST:
1462 ret = nvmet_rdma_queue_connect(cm_id, event);
1463 break;
1464 case RDMA_CM_EVENT_ESTABLISHED:
1465 nvmet_rdma_queue_established(queue);
1466 break;
1467 case RDMA_CM_EVENT_ADDR_CHANGE:
1468 case RDMA_CM_EVENT_DISCONNECTED:
1469 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1470 nvmet_rdma_queue_disconnect(queue);
1471 break;
1472 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1473 ret = nvmet_rdma_device_removal(cm_id, queue);
1474 break;
1475 case RDMA_CM_EVENT_REJECTED:
1476 pr_debug("Connection rejected: %s\n",
1477 rdma_reject_msg(cm_id, event->status));
1478 /* FALLTHROUGH */
1479 case RDMA_CM_EVENT_UNREACHABLE:
1480 case RDMA_CM_EVENT_CONNECT_ERROR:
1481 nvmet_rdma_queue_connect_fail(cm_id, queue);
1482 break;
1483 default:
1484 pr_err("received unrecognized RDMA CM event %d\n",
1485 event->event);
1486 break;
1487 }
1488
1489 return ret;
1490}
1491
1492static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl)
1493{
1494 struct nvmet_rdma_queue *queue;
1495
1496restart:
1497 mutex_lock(&nvmet_rdma_queue_mutex);
1498 list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
1499 if (queue->nvme_sq.ctrl == ctrl) {
1500 list_del_init(&queue->queue_list);
1501 mutex_unlock(&nvmet_rdma_queue_mutex);
1502
1503 __nvmet_rdma_queue_disconnect(queue);
1504 goto restart;
1505 }
1506 }
1507 mutex_unlock(&nvmet_rdma_queue_mutex);
1508}
1509
1510static int nvmet_rdma_add_port(struct nvmet_port *port)
1511{
1512 struct rdma_cm_id *cm_id;
1513 struct sockaddr_storage addr = { };
1514 __kernel_sa_family_t af;
1515 int ret;
1516
1517 switch (port->disc_addr.adrfam) {
1518 case NVMF_ADDR_FAMILY_IP4:
1519 af = AF_INET;
1520 break;
1521 case NVMF_ADDR_FAMILY_IP6:
1522 af = AF_INET6;
1523 break;
1524 default:
1525 pr_err("address family %d not supported\n",
1526 port->disc_addr.adrfam);
1527 return -EINVAL;
1528 }
1529
1530 if (port->inline_data_size < 0) {
1531 port->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE;
1532 } else if (port->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) {
1533 pr_warn("inline_data_size %u is too large, reducing to %u\n",
1534 port->inline_data_size,
1535 NVMET_RDMA_MAX_INLINE_DATA_SIZE);
1536 port->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE;
1537 }
1538
1539 ret = inet_pton_with_scope(&init_net, af, port->disc_addr.traddr,
1540 port->disc_addr.trsvcid, &addr);
1541 if (ret) {
1542 pr_err("malformed ip/port passed: %s:%s\n",
1543 port->disc_addr.traddr, port->disc_addr.trsvcid);
1544 return ret;
1545 }
1546
1547 cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port,
1548 RDMA_PS_TCP, IB_QPT_RC);
1549 if (IS_ERR(cm_id)) {
1550 pr_err("CM ID creation failed\n");
1551 return PTR_ERR(cm_id);
1552 }
1553
1554 /*
1555 * Allow both IPv4 and IPv6 sockets to bind a single port
1556 * at the same time.
1557 */
1558 ret = rdma_set_afonly(cm_id, 1);
1559 if (ret) {
1560 pr_err("rdma_set_afonly failed (%d)\n", ret);
1561 goto out_destroy_id;
1562 }
1563
1564 ret = rdma_bind_addr(cm_id, (struct sockaddr *)&addr);
1565 if (ret) {
1566 pr_err("binding CM ID to %pISpcs failed (%d)\n",
1567 (struct sockaddr *)&addr, ret);
1568 goto out_destroy_id;
1569 }
1570
1571 ret = rdma_listen(cm_id, 128);
1572 if (ret) {
1573 pr_err("listening to %pISpcs failed (%d)\n",
1574 (struct sockaddr *)&addr, ret);
1575 goto out_destroy_id;
1576 }
1577
1578 pr_info("enabling port %d (%pISpcs)\n",
1579 le16_to_cpu(port->disc_addr.portid), (struct sockaddr *)&addr);
1580 port->priv = cm_id;
1581 return 0;
1582
1583out_destroy_id:
1584 rdma_destroy_id(cm_id);
1585 return ret;
1586}
1587
1588static void nvmet_rdma_remove_port(struct nvmet_port *port)
1589{
1590 struct rdma_cm_id *cm_id = xchg(&port->priv, NULL);
1591
1592 if (cm_id)
1593 rdma_destroy_id(cm_id);
1594}
1595
1596static void nvmet_rdma_disc_port_addr(struct nvmet_req *req,
1597 struct nvmet_port *port, char *traddr)
1598{
1599 struct rdma_cm_id *cm_id = port->priv;
1600
1601 if (inet_addr_is_any((struct sockaddr *)&cm_id->route.addr.src_addr)) {
1602 struct nvmet_rdma_rsp *rsp =
1603 container_of(req, struct nvmet_rdma_rsp, req);
1604 struct rdma_cm_id *req_cm_id = rsp->queue->cm_id;
1605 struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr;
1606
1607 sprintf(traddr, "%pISc", addr);
1608 } else {
1609 memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
1610 }
1611}
1612
1613static const struct nvmet_fabrics_ops nvmet_rdma_ops = {
1614 .owner = THIS_MODULE,
1615 .type = NVMF_TRTYPE_RDMA,
1616 .msdbd = 1,
1617 .has_keyed_sgls = 1,
1618 .add_port = nvmet_rdma_add_port,
1619 .remove_port = nvmet_rdma_remove_port,
1620 .queue_response = nvmet_rdma_queue_response,
1621 .delete_ctrl = nvmet_rdma_delete_ctrl,
1622 .disc_traddr = nvmet_rdma_disc_port_addr,
1623};
1624
1625static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1626{
1627 struct nvmet_rdma_queue *queue, *tmp;
1628 struct nvmet_rdma_device *ndev;
1629 bool found = false;
1630
1631 mutex_lock(&device_list_mutex);
1632 list_for_each_entry(ndev, &device_list, entry) {
1633 if (ndev->device == ib_device) {
1634 found = true;
1635 break;
1636 }
1637 }
1638 mutex_unlock(&device_list_mutex);
1639
1640 if (!found)
1641 return;
1642
1643 /*
1644 * IB Device that is used by nvmet controllers is being removed,
1645 * delete all queues using this device.
1646 */
1647 mutex_lock(&nvmet_rdma_queue_mutex);
1648 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
1649 queue_list) {
1650 if (queue->dev->device != ib_device)
1651 continue;
1652
1653 pr_info("Removing queue %d\n", queue->idx);
1654 list_del_init(&queue->queue_list);
1655 __nvmet_rdma_queue_disconnect(queue);
1656 }
1657 mutex_unlock(&nvmet_rdma_queue_mutex);
1658
1659 flush_scheduled_work();
1660}
1661
1662static struct ib_client nvmet_rdma_ib_client = {
1663 .name = "nvmet_rdma",
1664 .remove = nvmet_rdma_remove_one
1665};
1666
1667static int __init nvmet_rdma_init(void)
1668{
1669 int ret;
1670
1671 ret = ib_register_client(&nvmet_rdma_ib_client);
1672 if (ret)
1673 return ret;
1674
1675 ret = nvmet_register_transport(&nvmet_rdma_ops);
1676 if (ret)
1677 goto err_ib_client;
1678
1679 return 0;
1680
1681err_ib_client:
1682 ib_unregister_client(&nvmet_rdma_ib_client);
1683 return ret;
1684}
1685
1686static void __exit nvmet_rdma_exit(void)
1687{
1688 nvmet_unregister_transport(&nvmet_rdma_ops);
1689 ib_unregister_client(&nvmet_rdma_ib_client);
1690 WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list));
1691 ida_destroy(&nvmet_rdma_queue_ida);
1692}
1693
1694module_init(nvmet_rdma_init);
1695module_exit(nvmet_rdma_exit);
1696
1697MODULE_LICENSE("GPL v2");
1698MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */