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