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