This source file includes following definitions.
- to_rdma_ctrl
- put_unaligned_le24
- nvme_rdma_queue_idx
- nvme_rdma_poll_queue
- nvme_rdma_inline_data_size
- nvme_rdma_free_qe
- nvme_rdma_alloc_qe
- nvme_rdma_free_ring
- nvme_rdma_alloc_ring
- nvme_rdma_qp_event
- nvme_rdma_wait_for_cm
- nvme_rdma_create_qp
- nvme_rdma_exit_request
- nvme_rdma_init_request
- nvme_rdma_init_hctx
- nvme_rdma_init_admin_hctx
- nvme_rdma_free_dev
- nvme_rdma_dev_put
- nvme_rdma_dev_get
- nvme_rdma_find_get_device
- nvme_rdma_destroy_queue_ib
- nvme_rdma_get_max_fr_pages
- nvme_rdma_create_queue_ib
- nvme_rdma_alloc_queue
- __nvme_rdma_stop_queue
- nvme_rdma_stop_queue
- nvme_rdma_free_queue
- nvme_rdma_free_io_queues
- nvme_rdma_stop_io_queues
- nvme_rdma_start_queue
- nvme_rdma_start_io_queues
- nvme_rdma_alloc_io_queues
- nvme_rdma_alloc_tagset
- nvme_rdma_destroy_admin_queue
- nvme_rdma_configure_admin_queue
- nvme_rdma_destroy_io_queues
- nvme_rdma_configure_io_queues
- nvme_rdma_teardown_admin_queue
- nvme_rdma_teardown_io_queues
- nvme_rdma_free_ctrl
- nvme_rdma_reconnect_or_remove
- nvme_rdma_setup_ctrl
- nvme_rdma_reconnect_ctrl_work
- nvme_rdma_error_recovery_work
- nvme_rdma_error_recovery
- nvme_rdma_wr_error
- nvme_rdma_memreg_done
- nvme_rdma_inv_rkey_done
- nvme_rdma_inv_rkey
- nvme_rdma_unmap_data
- nvme_rdma_set_sg_null
- nvme_rdma_map_sg_inline
- nvme_rdma_map_sg_single
- nvme_rdma_map_sg_fr
- nvme_rdma_map_data
- nvme_rdma_send_done
- nvme_rdma_post_send
- nvme_rdma_post_recv
- nvme_rdma_tagset
- nvme_rdma_async_done
- nvme_rdma_submit_async_event
- nvme_rdma_process_nvme_rsp
- nvme_rdma_recv_done
- nvme_rdma_conn_established
- nvme_rdma_conn_rejected
- nvme_rdma_addr_resolved
- nvme_rdma_route_resolved
- nvme_rdma_cm_handler
- nvme_rdma_timeout
- nvme_rdma_queue_rq
- nvme_rdma_poll
- nvme_rdma_complete_rq
- nvme_rdma_map_queues
- nvme_rdma_shutdown_ctrl
- nvme_rdma_delete_ctrl
- nvme_rdma_reset_ctrl_work
- nvme_rdma_existing_controller
- nvme_rdma_create_ctrl
- nvme_rdma_remove_one
- nvme_rdma_init_module
- nvme_rdma_cleanup_module
1
2
3
4
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <rdma/mr_pool.h>
11 #include <linux/err.h>
12 #include <linux/string.h>
13 #include <linux/atomic.h>
14 #include <linux/blk-mq.h>
15 #include <linux/blk-mq-rdma.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/scatterlist.h>
20 #include <linux/nvme.h>
21 #include <asm/unaligned.h>
22
23 #include <rdma/ib_verbs.h>
24 #include <rdma/rdma_cm.h>
25 #include <linux/nvme-rdma.h>
26
27 #include "nvme.h"
28 #include "fabrics.h"
29
30
31 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000
32
33 #define NVME_RDMA_MAX_SEGMENTS 256
34
35 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4
36
37 struct nvme_rdma_device {
38 struct ib_device *dev;
39 struct ib_pd *pd;
40 struct kref ref;
41 struct list_head entry;
42 unsigned int num_inline_segments;
43 };
44
45 struct nvme_rdma_qe {
46 struct ib_cqe cqe;
47 void *data;
48 u64 dma;
49 };
50
51 struct nvme_rdma_queue;
52 struct nvme_rdma_request {
53 struct nvme_request req;
54 struct ib_mr *mr;
55 struct nvme_rdma_qe sqe;
56 union nvme_result result;
57 __le16 status;
58 refcount_t ref;
59 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
60 u32 num_sge;
61 int nents;
62 struct ib_reg_wr reg_wr;
63 struct ib_cqe reg_cqe;
64 struct nvme_rdma_queue *queue;
65 struct sg_table sg_table;
66 struct scatterlist first_sgl[];
67 };
68
69 enum nvme_rdma_queue_flags {
70 NVME_RDMA_Q_ALLOCATED = 0,
71 NVME_RDMA_Q_LIVE = 1,
72 NVME_RDMA_Q_TR_READY = 2,
73 };
74
75 struct nvme_rdma_queue {
76 struct nvme_rdma_qe *rsp_ring;
77 int queue_size;
78 size_t cmnd_capsule_len;
79 struct nvme_rdma_ctrl *ctrl;
80 struct nvme_rdma_device *device;
81 struct ib_cq *ib_cq;
82 struct ib_qp *qp;
83
84 unsigned long flags;
85 struct rdma_cm_id *cm_id;
86 int cm_error;
87 struct completion cm_done;
88 };
89
90 struct nvme_rdma_ctrl {
91
92 struct nvme_rdma_queue *queues;
93
94
95 struct blk_mq_tag_set tag_set;
96 struct work_struct err_work;
97
98 struct nvme_rdma_qe async_event_sqe;
99
100 struct delayed_work reconnect_work;
101
102 struct list_head list;
103
104 struct blk_mq_tag_set admin_tag_set;
105 struct nvme_rdma_device *device;
106
107 u32 max_fr_pages;
108
109 struct sockaddr_storage addr;
110 struct sockaddr_storage src_addr;
111
112 struct nvme_ctrl ctrl;
113 bool use_inline_data;
114 u32 io_queues[HCTX_MAX_TYPES];
115 };
116
117 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
118 {
119 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
120 }
121
122 static LIST_HEAD(device_list);
123 static DEFINE_MUTEX(device_list_mutex);
124
125 static LIST_HEAD(nvme_rdma_ctrl_list);
126 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
127
128
129
130
131
132
133 static bool register_always = true;
134 module_param(register_always, bool, 0444);
135 MODULE_PARM_DESC(register_always,
136 "Use memory registration even for contiguous memory regions");
137
138 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
139 struct rdma_cm_event *event);
140 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
141
142 static const struct blk_mq_ops nvme_rdma_mq_ops;
143 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
144
145
146 static inline void put_unaligned_le24(u32 val, u8 *p)
147 {
148 *p++ = val;
149 *p++ = val >> 8;
150 *p++ = val >> 16;
151 }
152
153 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
154 {
155 return queue - queue->ctrl->queues;
156 }
157
158 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
159 {
160 return nvme_rdma_queue_idx(queue) >
161 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
162 queue->ctrl->io_queues[HCTX_TYPE_READ];
163 }
164
165 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
166 {
167 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
168 }
169
170 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
171 size_t capsule_size, enum dma_data_direction dir)
172 {
173 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
174 kfree(qe->data);
175 }
176
177 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
178 size_t capsule_size, enum dma_data_direction dir)
179 {
180 qe->data = kzalloc(capsule_size, GFP_KERNEL);
181 if (!qe->data)
182 return -ENOMEM;
183
184 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
185 if (ib_dma_mapping_error(ibdev, qe->dma)) {
186 kfree(qe->data);
187 qe->data = NULL;
188 return -ENOMEM;
189 }
190
191 return 0;
192 }
193
194 static void nvme_rdma_free_ring(struct ib_device *ibdev,
195 struct nvme_rdma_qe *ring, size_t ib_queue_size,
196 size_t capsule_size, enum dma_data_direction dir)
197 {
198 int i;
199
200 for (i = 0; i < ib_queue_size; i++)
201 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
202 kfree(ring);
203 }
204
205 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
206 size_t ib_queue_size, size_t capsule_size,
207 enum dma_data_direction dir)
208 {
209 struct nvme_rdma_qe *ring;
210 int i;
211
212 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
213 if (!ring)
214 return NULL;
215
216
217
218
219
220
221 for (i = 0; i < ib_queue_size; i++) {
222 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
223 goto out_free_ring;
224 }
225
226 return ring;
227
228 out_free_ring:
229 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
230 return NULL;
231 }
232
233 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
234 {
235 pr_debug("QP event %s (%d)\n",
236 ib_event_msg(event->event), event->event);
237
238 }
239
240 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
241 {
242 int ret;
243
244 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
245 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
246 if (ret < 0)
247 return ret;
248 if (ret == 0)
249 return -ETIMEDOUT;
250 WARN_ON_ONCE(queue->cm_error > 0);
251 return queue->cm_error;
252 }
253
254 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
255 {
256 struct nvme_rdma_device *dev = queue->device;
257 struct ib_qp_init_attr init_attr;
258 int ret;
259
260 memset(&init_attr, 0, sizeof(init_attr));
261 init_attr.event_handler = nvme_rdma_qp_event;
262
263 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
264
265 init_attr.cap.max_recv_wr = queue->queue_size + 1;
266 init_attr.cap.max_recv_sge = 1;
267 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
268 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
269 init_attr.qp_type = IB_QPT_RC;
270 init_attr.send_cq = queue->ib_cq;
271 init_attr.recv_cq = queue->ib_cq;
272
273 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
274
275 queue->qp = queue->cm_id->qp;
276 return ret;
277 }
278
279 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
280 struct request *rq, unsigned int hctx_idx)
281 {
282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
283
284 kfree(req->sqe.data);
285 }
286
287 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
288 struct request *rq, unsigned int hctx_idx,
289 unsigned int numa_node)
290 {
291 struct nvme_rdma_ctrl *ctrl = set->driver_data;
292 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
293 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
294 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
295
296 nvme_req(rq)->ctrl = &ctrl->ctrl;
297 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
298 if (!req->sqe.data)
299 return -ENOMEM;
300
301 req->queue = queue;
302
303 return 0;
304 }
305
306 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
307 unsigned int hctx_idx)
308 {
309 struct nvme_rdma_ctrl *ctrl = data;
310 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
311
312 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
313
314 hctx->driver_data = queue;
315 return 0;
316 }
317
318 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
319 unsigned int hctx_idx)
320 {
321 struct nvme_rdma_ctrl *ctrl = data;
322 struct nvme_rdma_queue *queue = &ctrl->queues[0];
323
324 BUG_ON(hctx_idx != 0);
325
326 hctx->driver_data = queue;
327 return 0;
328 }
329
330 static void nvme_rdma_free_dev(struct kref *ref)
331 {
332 struct nvme_rdma_device *ndev =
333 container_of(ref, struct nvme_rdma_device, ref);
334
335 mutex_lock(&device_list_mutex);
336 list_del(&ndev->entry);
337 mutex_unlock(&device_list_mutex);
338
339 ib_dealloc_pd(ndev->pd);
340 kfree(ndev);
341 }
342
343 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
344 {
345 kref_put(&dev->ref, nvme_rdma_free_dev);
346 }
347
348 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
349 {
350 return kref_get_unless_zero(&dev->ref);
351 }
352
353 static struct nvme_rdma_device *
354 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
355 {
356 struct nvme_rdma_device *ndev;
357
358 mutex_lock(&device_list_mutex);
359 list_for_each_entry(ndev, &device_list, entry) {
360 if (ndev->dev->node_guid == cm_id->device->node_guid &&
361 nvme_rdma_dev_get(ndev))
362 goto out_unlock;
363 }
364
365 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
366 if (!ndev)
367 goto out_err;
368
369 ndev->dev = cm_id->device;
370 kref_init(&ndev->ref);
371
372 ndev->pd = ib_alloc_pd(ndev->dev,
373 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
374 if (IS_ERR(ndev->pd))
375 goto out_free_dev;
376
377 if (!(ndev->dev->attrs.device_cap_flags &
378 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
379 dev_err(&ndev->dev->dev,
380 "Memory registrations not supported.\n");
381 goto out_free_pd;
382 }
383
384 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
385 ndev->dev->attrs.max_send_sge - 1);
386 list_add(&ndev->entry, &device_list);
387 out_unlock:
388 mutex_unlock(&device_list_mutex);
389 return ndev;
390
391 out_free_pd:
392 ib_dealloc_pd(ndev->pd);
393 out_free_dev:
394 kfree(ndev);
395 out_err:
396 mutex_unlock(&device_list_mutex);
397 return NULL;
398 }
399
400 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
401 {
402 struct nvme_rdma_device *dev;
403 struct ib_device *ibdev;
404
405 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
406 return;
407
408 dev = queue->device;
409 ibdev = dev->dev;
410
411 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
412
413
414
415
416
417
418 ib_destroy_qp(queue->qp);
419 ib_free_cq(queue->ib_cq);
420
421 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
422 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
423
424 nvme_rdma_dev_put(dev);
425 }
426
427 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
428 {
429 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
430 ibdev->attrs.max_fast_reg_page_list_len - 1);
431 }
432
433 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
434 {
435 struct ib_device *ibdev;
436 const int send_wr_factor = 3;
437 const int cq_factor = send_wr_factor + 1;
438 int comp_vector, idx = nvme_rdma_queue_idx(queue);
439 enum ib_poll_context poll_ctx;
440 int ret, pages_per_mr;
441
442 queue->device = nvme_rdma_find_get_device(queue->cm_id);
443 if (!queue->device) {
444 dev_err(queue->cm_id->device->dev.parent,
445 "no client data found!\n");
446 return -ECONNREFUSED;
447 }
448 ibdev = queue->device->dev;
449
450
451
452
453
454 comp_vector = idx == 0 ? idx : idx - 1;
455
456
457 if (nvme_rdma_poll_queue(queue))
458 poll_ctx = IB_POLL_DIRECT;
459 else
460 poll_ctx = IB_POLL_SOFTIRQ;
461
462
463 queue->ib_cq = ib_alloc_cq(ibdev, queue,
464 cq_factor * queue->queue_size + 1,
465 comp_vector, poll_ctx);
466 if (IS_ERR(queue->ib_cq)) {
467 ret = PTR_ERR(queue->ib_cq);
468 goto out_put_dev;
469 }
470
471 ret = nvme_rdma_create_qp(queue, send_wr_factor);
472 if (ret)
473 goto out_destroy_ib_cq;
474
475 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
476 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
477 if (!queue->rsp_ring) {
478 ret = -ENOMEM;
479 goto out_destroy_qp;
480 }
481
482
483
484
485
486
487 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev) + 1;
488 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
489 queue->queue_size,
490 IB_MR_TYPE_MEM_REG,
491 pages_per_mr, 0);
492 if (ret) {
493 dev_err(queue->ctrl->ctrl.device,
494 "failed to initialize MR pool sized %d for QID %d\n",
495 queue->queue_size, idx);
496 goto out_destroy_ring;
497 }
498
499 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
500
501 return 0;
502
503 out_destroy_ring:
504 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
505 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
506 out_destroy_qp:
507 rdma_destroy_qp(queue->cm_id);
508 out_destroy_ib_cq:
509 ib_free_cq(queue->ib_cq);
510 out_put_dev:
511 nvme_rdma_dev_put(queue->device);
512 return ret;
513 }
514
515 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
516 int idx, size_t queue_size)
517 {
518 struct nvme_rdma_queue *queue;
519 struct sockaddr *src_addr = NULL;
520 int ret;
521
522 queue = &ctrl->queues[idx];
523 queue->ctrl = ctrl;
524 init_completion(&queue->cm_done);
525
526 if (idx > 0)
527 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
528 else
529 queue->cmnd_capsule_len = sizeof(struct nvme_command);
530
531 queue->queue_size = queue_size;
532
533 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
534 RDMA_PS_TCP, IB_QPT_RC);
535 if (IS_ERR(queue->cm_id)) {
536 dev_info(ctrl->ctrl.device,
537 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
538 return PTR_ERR(queue->cm_id);
539 }
540
541 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
542 src_addr = (struct sockaddr *)&ctrl->src_addr;
543
544 queue->cm_error = -ETIMEDOUT;
545 ret = rdma_resolve_addr(queue->cm_id, src_addr,
546 (struct sockaddr *)&ctrl->addr,
547 NVME_RDMA_CONNECT_TIMEOUT_MS);
548 if (ret) {
549 dev_info(ctrl->ctrl.device,
550 "rdma_resolve_addr failed (%d).\n", ret);
551 goto out_destroy_cm_id;
552 }
553
554 ret = nvme_rdma_wait_for_cm(queue);
555 if (ret) {
556 dev_info(ctrl->ctrl.device,
557 "rdma connection establishment failed (%d)\n", ret);
558 goto out_destroy_cm_id;
559 }
560
561 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
562
563 return 0;
564
565 out_destroy_cm_id:
566 rdma_destroy_id(queue->cm_id);
567 nvme_rdma_destroy_queue_ib(queue);
568 return ret;
569 }
570
571 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
572 {
573 rdma_disconnect(queue->cm_id);
574 ib_drain_qp(queue->qp);
575 }
576
577 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
578 {
579 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
580 return;
581 __nvme_rdma_stop_queue(queue);
582 }
583
584 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
585 {
586 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
587 return;
588
589 nvme_rdma_destroy_queue_ib(queue);
590 rdma_destroy_id(queue->cm_id);
591 }
592
593 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
594 {
595 int i;
596
597 for (i = 1; i < ctrl->ctrl.queue_count; i++)
598 nvme_rdma_free_queue(&ctrl->queues[i]);
599 }
600
601 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
602 {
603 int i;
604
605 for (i = 1; i < ctrl->ctrl.queue_count; i++)
606 nvme_rdma_stop_queue(&ctrl->queues[i]);
607 }
608
609 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
610 {
611 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
612 bool poll = nvme_rdma_poll_queue(queue);
613 int ret;
614
615 if (idx)
616 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
617 else
618 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
619
620 if (!ret) {
621 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
622 } else {
623 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
624 __nvme_rdma_stop_queue(queue);
625 dev_info(ctrl->ctrl.device,
626 "failed to connect queue: %d ret=%d\n", idx, ret);
627 }
628 return ret;
629 }
630
631 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
632 {
633 int i, ret = 0;
634
635 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
636 ret = nvme_rdma_start_queue(ctrl, i);
637 if (ret)
638 goto out_stop_queues;
639 }
640
641 return 0;
642
643 out_stop_queues:
644 for (i--; i >= 1; i--)
645 nvme_rdma_stop_queue(&ctrl->queues[i]);
646 return ret;
647 }
648
649 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
650 {
651 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
652 struct ib_device *ibdev = ctrl->device->dev;
653 unsigned int nr_io_queues, nr_default_queues;
654 unsigned int nr_read_queues, nr_poll_queues;
655 int i, ret;
656
657 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
658 min(opts->nr_io_queues, num_online_cpus()));
659 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
660 min(opts->nr_write_queues, num_online_cpus()));
661 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
662 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
663
664 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
665 if (ret)
666 return ret;
667
668 ctrl->ctrl.queue_count = nr_io_queues + 1;
669 if (ctrl->ctrl.queue_count < 2)
670 return 0;
671
672 dev_info(ctrl->ctrl.device,
673 "creating %d I/O queues.\n", nr_io_queues);
674
675 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
676
677
678
679
680
681 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
682 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
683 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
684 min(nr_default_queues, nr_io_queues);
685 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
686 } else {
687
688
689
690
691
692 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
693 min(nr_read_queues, nr_io_queues);
694 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
695 }
696
697 if (opts->nr_poll_queues && nr_io_queues) {
698
699 ctrl->io_queues[HCTX_TYPE_POLL] =
700 min(nr_poll_queues, nr_io_queues);
701 }
702
703 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
704 ret = nvme_rdma_alloc_queue(ctrl, i,
705 ctrl->ctrl.sqsize + 1);
706 if (ret)
707 goto out_free_queues;
708 }
709
710 return 0;
711
712 out_free_queues:
713 for (i--; i >= 1; i--)
714 nvme_rdma_free_queue(&ctrl->queues[i]);
715
716 return ret;
717 }
718
719 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
720 bool admin)
721 {
722 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
723 struct blk_mq_tag_set *set;
724 int ret;
725
726 if (admin) {
727 set = &ctrl->admin_tag_set;
728 memset(set, 0, sizeof(*set));
729 set->ops = &nvme_rdma_admin_mq_ops;
730 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
731 set->reserved_tags = 2;
732 set->numa_node = nctrl->numa_node;
733 set->cmd_size = sizeof(struct nvme_rdma_request) +
734 SG_CHUNK_SIZE * sizeof(struct scatterlist);
735 set->driver_data = ctrl;
736 set->nr_hw_queues = 1;
737 set->timeout = ADMIN_TIMEOUT;
738 set->flags = BLK_MQ_F_NO_SCHED;
739 } else {
740 set = &ctrl->tag_set;
741 memset(set, 0, sizeof(*set));
742 set->ops = &nvme_rdma_mq_ops;
743 set->queue_depth = nctrl->sqsize + 1;
744 set->reserved_tags = 1;
745 set->numa_node = nctrl->numa_node;
746 set->flags = BLK_MQ_F_SHOULD_MERGE;
747 set->cmd_size = sizeof(struct nvme_rdma_request) +
748 SG_CHUNK_SIZE * sizeof(struct scatterlist);
749 set->driver_data = ctrl;
750 set->nr_hw_queues = nctrl->queue_count - 1;
751 set->timeout = NVME_IO_TIMEOUT;
752 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
753 }
754
755 ret = blk_mq_alloc_tag_set(set);
756 if (ret)
757 return ERR_PTR(ret);
758
759 return set;
760 }
761
762 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
763 bool remove)
764 {
765 if (remove) {
766 blk_cleanup_queue(ctrl->ctrl.admin_q);
767 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
768 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
769 }
770 if (ctrl->async_event_sqe.data) {
771 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
772 sizeof(struct nvme_command), DMA_TO_DEVICE);
773 ctrl->async_event_sqe.data = NULL;
774 }
775 nvme_rdma_free_queue(&ctrl->queues[0]);
776 }
777
778 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
779 bool new)
780 {
781 int error;
782
783 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
784 if (error)
785 return error;
786
787 ctrl->device = ctrl->queues[0].device;
788 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
789
790 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
791
792
793
794
795
796
797 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
798 sizeof(struct nvme_command), DMA_TO_DEVICE);
799 if (error)
800 goto out_free_queue;
801
802 if (new) {
803 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
804 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
805 error = PTR_ERR(ctrl->ctrl.admin_tagset);
806 goto out_free_async_qe;
807 }
808
809 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
810 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
811 error = PTR_ERR(ctrl->ctrl.fabrics_q);
812 goto out_free_tagset;
813 }
814
815 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
816 if (IS_ERR(ctrl->ctrl.admin_q)) {
817 error = PTR_ERR(ctrl->ctrl.admin_q);
818 goto out_cleanup_fabrics_q;
819 }
820 }
821
822 error = nvme_rdma_start_queue(ctrl, 0);
823 if (error)
824 goto out_cleanup_queue;
825
826 error = nvme_enable_ctrl(&ctrl->ctrl);
827 if (error)
828 goto out_stop_queue;
829
830 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
831 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
832
833 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
834
835 error = nvme_init_identify(&ctrl->ctrl);
836 if (error)
837 goto out_stop_queue;
838
839 return 0;
840
841 out_stop_queue:
842 nvme_rdma_stop_queue(&ctrl->queues[0]);
843 out_cleanup_queue:
844 if (new)
845 blk_cleanup_queue(ctrl->ctrl.admin_q);
846 out_cleanup_fabrics_q:
847 if (new)
848 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
849 out_free_tagset:
850 if (new)
851 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
852 out_free_async_qe:
853 if (ctrl->async_event_sqe.data) {
854 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
855 sizeof(struct nvme_command), DMA_TO_DEVICE);
856 ctrl->async_event_sqe.data = NULL;
857 }
858 out_free_queue:
859 nvme_rdma_free_queue(&ctrl->queues[0]);
860 return error;
861 }
862
863 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
864 bool remove)
865 {
866 if (remove) {
867 blk_cleanup_queue(ctrl->ctrl.connect_q);
868 blk_mq_free_tag_set(ctrl->ctrl.tagset);
869 }
870 nvme_rdma_free_io_queues(ctrl);
871 }
872
873 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
874 {
875 int ret;
876
877 ret = nvme_rdma_alloc_io_queues(ctrl);
878 if (ret)
879 return ret;
880
881 if (new) {
882 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
883 if (IS_ERR(ctrl->ctrl.tagset)) {
884 ret = PTR_ERR(ctrl->ctrl.tagset);
885 goto out_free_io_queues;
886 }
887
888 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
889 if (IS_ERR(ctrl->ctrl.connect_q)) {
890 ret = PTR_ERR(ctrl->ctrl.connect_q);
891 goto out_free_tag_set;
892 }
893 } else {
894 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
895 ctrl->ctrl.queue_count - 1);
896 }
897
898 ret = nvme_rdma_start_io_queues(ctrl);
899 if (ret)
900 goto out_cleanup_connect_q;
901
902 return 0;
903
904 out_cleanup_connect_q:
905 if (new)
906 blk_cleanup_queue(ctrl->ctrl.connect_q);
907 out_free_tag_set:
908 if (new)
909 blk_mq_free_tag_set(ctrl->ctrl.tagset);
910 out_free_io_queues:
911 nvme_rdma_free_io_queues(ctrl);
912 return ret;
913 }
914
915 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
916 bool remove)
917 {
918 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
919 nvme_rdma_stop_queue(&ctrl->queues[0]);
920 if (ctrl->ctrl.admin_tagset) {
921 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
922 nvme_cancel_request, &ctrl->ctrl);
923 blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
924 }
925 if (remove)
926 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
927 nvme_rdma_destroy_admin_queue(ctrl, remove);
928 }
929
930 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
931 bool remove)
932 {
933 if (ctrl->ctrl.queue_count > 1) {
934 nvme_stop_queues(&ctrl->ctrl);
935 nvme_rdma_stop_io_queues(ctrl);
936 if (ctrl->ctrl.tagset) {
937 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
938 nvme_cancel_request, &ctrl->ctrl);
939 blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
940 }
941 if (remove)
942 nvme_start_queues(&ctrl->ctrl);
943 nvme_rdma_destroy_io_queues(ctrl, remove);
944 }
945 }
946
947 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
948 {
949 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
950
951 if (list_empty(&ctrl->list))
952 goto free_ctrl;
953
954 mutex_lock(&nvme_rdma_ctrl_mutex);
955 list_del(&ctrl->list);
956 mutex_unlock(&nvme_rdma_ctrl_mutex);
957
958 nvmf_free_options(nctrl->opts);
959 free_ctrl:
960 kfree(ctrl->queues);
961 kfree(ctrl);
962 }
963
964 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
965 {
966
967 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
968 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
969 ctrl->ctrl.state == NVME_CTRL_LIVE);
970 return;
971 }
972
973 if (nvmf_should_reconnect(&ctrl->ctrl)) {
974 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
975 ctrl->ctrl.opts->reconnect_delay);
976 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
977 ctrl->ctrl.opts->reconnect_delay * HZ);
978 } else {
979 nvme_delete_ctrl(&ctrl->ctrl);
980 }
981 }
982
983 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
984 {
985 int ret = -EINVAL;
986 bool changed;
987
988 ret = nvme_rdma_configure_admin_queue(ctrl, new);
989 if (ret)
990 return ret;
991
992 if (ctrl->ctrl.icdoff) {
993 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
994 goto destroy_admin;
995 }
996
997 if (!(ctrl->ctrl.sgls & (1 << 2))) {
998 dev_err(ctrl->ctrl.device,
999 "Mandatory keyed sgls are not supported!\n");
1000 goto destroy_admin;
1001 }
1002
1003 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1004 dev_warn(ctrl->ctrl.device,
1005 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1006 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1007 }
1008
1009 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1010 dev_warn(ctrl->ctrl.device,
1011 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1012 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1013 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1014 }
1015
1016 if (ctrl->ctrl.sgls & (1 << 20))
1017 ctrl->use_inline_data = true;
1018
1019 if (ctrl->ctrl.queue_count > 1) {
1020 ret = nvme_rdma_configure_io_queues(ctrl, new);
1021 if (ret)
1022 goto destroy_admin;
1023 }
1024
1025 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1026 if (!changed) {
1027
1028 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1029 ret = -EINVAL;
1030 goto destroy_io;
1031 }
1032
1033 nvme_start_ctrl(&ctrl->ctrl);
1034 return 0;
1035
1036 destroy_io:
1037 if (ctrl->ctrl.queue_count > 1)
1038 nvme_rdma_destroy_io_queues(ctrl, new);
1039 destroy_admin:
1040 nvme_rdma_stop_queue(&ctrl->queues[0]);
1041 nvme_rdma_destroy_admin_queue(ctrl, new);
1042 return ret;
1043 }
1044
1045 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1046 {
1047 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1048 struct nvme_rdma_ctrl, reconnect_work);
1049
1050 ++ctrl->ctrl.nr_reconnects;
1051
1052 if (nvme_rdma_setup_ctrl(ctrl, false))
1053 goto requeue;
1054
1055 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1056 ctrl->ctrl.nr_reconnects);
1057
1058 ctrl->ctrl.nr_reconnects = 0;
1059
1060 return;
1061
1062 requeue:
1063 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1064 ctrl->ctrl.nr_reconnects);
1065 nvme_rdma_reconnect_or_remove(ctrl);
1066 }
1067
1068 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1069 {
1070 struct nvme_rdma_ctrl *ctrl = container_of(work,
1071 struct nvme_rdma_ctrl, err_work);
1072
1073 nvme_stop_keep_alive(&ctrl->ctrl);
1074 nvme_rdma_teardown_io_queues(ctrl, false);
1075 nvme_start_queues(&ctrl->ctrl);
1076 nvme_rdma_teardown_admin_queue(ctrl, false);
1077 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1078
1079 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1080
1081 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1082 return;
1083 }
1084
1085 nvme_rdma_reconnect_or_remove(ctrl);
1086 }
1087
1088 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1089 {
1090 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1091 return;
1092
1093 queue_work(nvme_reset_wq, &ctrl->err_work);
1094 }
1095
1096 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1097 const char *op)
1098 {
1099 struct nvme_rdma_queue *queue = cq->cq_context;
1100 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1101
1102 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1103 dev_info(ctrl->ctrl.device,
1104 "%s for CQE 0x%p failed with status %s (%d)\n",
1105 op, wc->wr_cqe,
1106 ib_wc_status_msg(wc->status), wc->status);
1107 nvme_rdma_error_recovery(ctrl);
1108 }
1109
1110 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1111 {
1112 if (unlikely(wc->status != IB_WC_SUCCESS))
1113 nvme_rdma_wr_error(cq, wc, "MEMREG");
1114 }
1115
1116 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1117 {
1118 struct nvme_rdma_request *req =
1119 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1120 struct request *rq = blk_mq_rq_from_pdu(req);
1121
1122 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1123 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1124 return;
1125 }
1126
1127 if (refcount_dec_and_test(&req->ref))
1128 nvme_end_request(rq, req->status, req->result);
1129
1130 }
1131
1132 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1133 struct nvme_rdma_request *req)
1134 {
1135 struct ib_send_wr wr = {
1136 .opcode = IB_WR_LOCAL_INV,
1137 .next = NULL,
1138 .num_sge = 0,
1139 .send_flags = IB_SEND_SIGNALED,
1140 .ex.invalidate_rkey = req->mr->rkey,
1141 };
1142
1143 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1144 wr.wr_cqe = &req->reg_cqe;
1145
1146 return ib_post_send(queue->qp, &wr, NULL);
1147 }
1148
1149 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1150 struct request *rq)
1151 {
1152 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1153 struct nvme_rdma_device *dev = queue->device;
1154 struct ib_device *ibdev = dev->dev;
1155
1156 if (!blk_rq_nr_phys_segments(rq))
1157 return;
1158
1159 if (req->mr) {
1160 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1161 req->mr = NULL;
1162 }
1163
1164 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, req->nents, rq_dma_dir(rq));
1165
1166 nvme_cleanup_cmd(rq);
1167 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
1168 }
1169
1170 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1171 {
1172 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1173
1174 sg->addr = 0;
1175 put_unaligned_le24(0, sg->length);
1176 put_unaligned_le32(0, sg->key);
1177 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1178 return 0;
1179 }
1180
1181 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1182 struct nvme_rdma_request *req, struct nvme_command *c,
1183 int count)
1184 {
1185 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1186 struct scatterlist *sgl = req->sg_table.sgl;
1187 struct ib_sge *sge = &req->sge[1];
1188 u32 len = 0;
1189 int i;
1190
1191 for (i = 0; i < count; i++, sgl++, sge++) {
1192 sge->addr = sg_dma_address(sgl);
1193 sge->length = sg_dma_len(sgl);
1194 sge->lkey = queue->device->pd->local_dma_lkey;
1195 len += sge->length;
1196 }
1197
1198 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1199 sg->length = cpu_to_le32(len);
1200 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1201
1202 req->num_sge += count;
1203 return 0;
1204 }
1205
1206 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1207 struct nvme_rdma_request *req, struct nvme_command *c)
1208 {
1209 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1210
1211 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1212 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1213 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1214 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1215 return 0;
1216 }
1217
1218 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1219 struct nvme_rdma_request *req, struct nvme_command *c,
1220 int count)
1221 {
1222 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1223 int nr;
1224
1225 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1226 if (WARN_ON_ONCE(!req->mr))
1227 return -EAGAIN;
1228
1229
1230
1231
1232
1233 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1234 if (unlikely(nr < count)) {
1235 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1236 req->mr = NULL;
1237 if (nr < 0)
1238 return nr;
1239 return -EINVAL;
1240 }
1241
1242 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1243
1244 req->reg_cqe.done = nvme_rdma_memreg_done;
1245 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1246 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1247 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1248 req->reg_wr.wr.num_sge = 0;
1249 req->reg_wr.mr = req->mr;
1250 req->reg_wr.key = req->mr->rkey;
1251 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1252 IB_ACCESS_REMOTE_READ |
1253 IB_ACCESS_REMOTE_WRITE;
1254
1255 sg->addr = cpu_to_le64(req->mr->iova);
1256 put_unaligned_le24(req->mr->length, sg->length);
1257 put_unaligned_le32(req->mr->rkey, sg->key);
1258 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1259 NVME_SGL_FMT_INVALIDATE;
1260
1261 return 0;
1262 }
1263
1264 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1265 struct request *rq, struct nvme_command *c)
1266 {
1267 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1268 struct nvme_rdma_device *dev = queue->device;
1269 struct ib_device *ibdev = dev->dev;
1270 int count, ret;
1271
1272 req->num_sge = 1;
1273 refcount_set(&req->ref, 2);
1274
1275 c->common.flags |= NVME_CMD_SGL_METABUF;
1276
1277 if (!blk_rq_nr_phys_segments(rq))
1278 return nvme_rdma_set_sg_null(c);
1279
1280 req->sg_table.sgl = req->first_sgl;
1281 ret = sg_alloc_table_chained(&req->sg_table,
1282 blk_rq_nr_phys_segments(rq), req->sg_table.sgl,
1283 SG_CHUNK_SIZE);
1284 if (ret)
1285 return -ENOMEM;
1286
1287 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1288
1289 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1290 rq_dma_dir(rq));
1291 if (unlikely(count <= 0)) {
1292 ret = -EIO;
1293 goto out_free_table;
1294 }
1295
1296 if (count <= dev->num_inline_segments) {
1297 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1298 queue->ctrl->use_inline_data &&
1299 blk_rq_payload_bytes(rq) <=
1300 nvme_rdma_inline_data_size(queue)) {
1301 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1302 goto out;
1303 }
1304
1305 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1306 ret = nvme_rdma_map_sg_single(queue, req, c);
1307 goto out;
1308 }
1309 }
1310
1311 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1312 out:
1313 if (unlikely(ret))
1314 goto out_unmap_sg;
1315
1316 return 0;
1317
1318 out_unmap_sg:
1319 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, req->nents, rq_dma_dir(rq));
1320 out_free_table:
1321 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
1322 return ret;
1323 }
1324
1325 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1326 {
1327 struct nvme_rdma_qe *qe =
1328 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1329 struct nvme_rdma_request *req =
1330 container_of(qe, struct nvme_rdma_request, sqe);
1331 struct request *rq = blk_mq_rq_from_pdu(req);
1332
1333 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1334 nvme_rdma_wr_error(cq, wc, "SEND");
1335 return;
1336 }
1337
1338 if (refcount_dec_and_test(&req->ref))
1339 nvme_end_request(rq, req->status, req->result);
1340 }
1341
1342 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1343 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1344 struct ib_send_wr *first)
1345 {
1346 struct ib_send_wr wr;
1347 int ret;
1348
1349 sge->addr = qe->dma;
1350 sge->length = sizeof(struct nvme_command),
1351 sge->lkey = queue->device->pd->local_dma_lkey;
1352
1353 wr.next = NULL;
1354 wr.wr_cqe = &qe->cqe;
1355 wr.sg_list = sge;
1356 wr.num_sge = num_sge;
1357 wr.opcode = IB_WR_SEND;
1358 wr.send_flags = IB_SEND_SIGNALED;
1359
1360 if (first)
1361 first->next = ≀
1362 else
1363 first = ≀
1364
1365 ret = ib_post_send(queue->qp, first, NULL);
1366 if (unlikely(ret)) {
1367 dev_err(queue->ctrl->ctrl.device,
1368 "%s failed with error code %d\n", __func__, ret);
1369 }
1370 return ret;
1371 }
1372
1373 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1374 struct nvme_rdma_qe *qe)
1375 {
1376 struct ib_recv_wr wr;
1377 struct ib_sge list;
1378 int ret;
1379
1380 list.addr = qe->dma;
1381 list.length = sizeof(struct nvme_completion);
1382 list.lkey = queue->device->pd->local_dma_lkey;
1383
1384 qe->cqe.done = nvme_rdma_recv_done;
1385
1386 wr.next = NULL;
1387 wr.wr_cqe = &qe->cqe;
1388 wr.sg_list = &list;
1389 wr.num_sge = 1;
1390
1391 ret = ib_post_recv(queue->qp, &wr, NULL);
1392 if (unlikely(ret)) {
1393 dev_err(queue->ctrl->ctrl.device,
1394 "%s failed with error code %d\n", __func__, ret);
1395 }
1396 return ret;
1397 }
1398
1399 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1400 {
1401 u32 queue_idx = nvme_rdma_queue_idx(queue);
1402
1403 if (queue_idx == 0)
1404 return queue->ctrl->admin_tag_set.tags[queue_idx];
1405 return queue->ctrl->tag_set.tags[queue_idx - 1];
1406 }
1407
1408 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1409 {
1410 if (unlikely(wc->status != IB_WC_SUCCESS))
1411 nvme_rdma_wr_error(cq, wc, "ASYNC");
1412 }
1413
1414 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1415 {
1416 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1417 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1418 struct ib_device *dev = queue->device->dev;
1419 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1420 struct nvme_command *cmd = sqe->data;
1421 struct ib_sge sge;
1422 int ret;
1423
1424 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1425
1426 memset(cmd, 0, sizeof(*cmd));
1427 cmd->common.opcode = nvme_admin_async_event;
1428 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1429 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1430 nvme_rdma_set_sg_null(cmd);
1431
1432 sqe->cqe.done = nvme_rdma_async_done;
1433
1434 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1435 DMA_TO_DEVICE);
1436
1437 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1438 WARN_ON_ONCE(ret);
1439 }
1440
1441 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1442 struct nvme_completion *cqe, struct ib_wc *wc)
1443 {
1444 struct request *rq;
1445 struct nvme_rdma_request *req;
1446
1447 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1448 if (!rq) {
1449 dev_err(queue->ctrl->ctrl.device,
1450 "tag 0x%x on QP %#x not found\n",
1451 cqe->command_id, queue->qp->qp_num);
1452 nvme_rdma_error_recovery(queue->ctrl);
1453 return;
1454 }
1455 req = blk_mq_rq_to_pdu(rq);
1456
1457 req->status = cqe->status;
1458 req->result = cqe->result;
1459
1460 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1461 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1462 dev_err(queue->ctrl->ctrl.device,
1463 "Bogus remote invalidation for rkey %#x\n",
1464 req->mr->rkey);
1465 nvme_rdma_error_recovery(queue->ctrl);
1466 }
1467 } else if (req->mr) {
1468 int ret;
1469
1470 ret = nvme_rdma_inv_rkey(queue, req);
1471 if (unlikely(ret < 0)) {
1472 dev_err(queue->ctrl->ctrl.device,
1473 "Queueing INV WR for rkey %#x failed (%d)\n",
1474 req->mr->rkey, ret);
1475 nvme_rdma_error_recovery(queue->ctrl);
1476 }
1477
1478 return;
1479 }
1480
1481 if (refcount_dec_and_test(&req->ref))
1482 nvme_end_request(rq, req->status, req->result);
1483 }
1484
1485 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1486 {
1487 struct nvme_rdma_qe *qe =
1488 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1489 struct nvme_rdma_queue *queue = cq->cq_context;
1490 struct ib_device *ibdev = queue->device->dev;
1491 struct nvme_completion *cqe = qe->data;
1492 const size_t len = sizeof(struct nvme_completion);
1493
1494 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1495 nvme_rdma_wr_error(cq, wc, "RECV");
1496 return;
1497 }
1498
1499 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1500
1501
1502
1503
1504
1505
1506 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1507 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
1508 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1509 &cqe->result);
1510 else
1511 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
1512 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1513
1514 nvme_rdma_post_recv(queue, qe);
1515 }
1516
1517 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1518 {
1519 int ret, i;
1520
1521 for (i = 0; i < queue->queue_size; i++) {
1522 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1523 if (ret)
1524 goto out_destroy_queue_ib;
1525 }
1526
1527 return 0;
1528
1529 out_destroy_queue_ib:
1530 nvme_rdma_destroy_queue_ib(queue);
1531 return ret;
1532 }
1533
1534 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1535 struct rdma_cm_event *ev)
1536 {
1537 struct rdma_cm_id *cm_id = queue->cm_id;
1538 int status = ev->status;
1539 const char *rej_msg;
1540 const struct nvme_rdma_cm_rej *rej_data;
1541 u8 rej_data_len;
1542
1543 rej_msg = rdma_reject_msg(cm_id, status);
1544 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1545
1546 if (rej_data && rej_data_len >= sizeof(u16)) {
1547 u16 sts = le16_to_cpu(rej_data->sts);
1548
1549 dev_err(queue->ctrl->ctrl.device,
1550 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1551 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1552 } else {
1553 dev_err(queue->ctrl->ctrl.device,
1554 "Connect rejected: status %d (%s).\n", status, rej_msg);
1555 }
1556
1557 return -ECONNRESET;
1558 }
1559
1560 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1561 {
1562 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
1563 int ret;
1564
1565 ret = nvme_rdma_create_queue_ib(queue);
1566 if (ret)
1567 return ret;
1568
1569 if (ctrl->opts->tos >= 0)
1570 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
1571 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1572 if (ret) {
1573 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
1574 queue->cm_error);
1575 goto out_destroy_queue;
1576 }
1577
1578 return 0;
1579
1580 out_destroy_queue:
1581 nvme_rdma_destroy_queue_ib(queue);
1582 return ret;
1583 }
1584
1585 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1586 {
1587 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1588 struct rdma_conn_param param = { };
1589 struct nvme_rdma_cm_req priv = { };
1590 int ret;
1591
1592 param.qp_num = queue->qp->qp_num;
1593 param.flow_control = 1;
1594
1595 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1596
1597 param.retry_count = 7;
1598 param.rnr_retry_count = 7;
1599 param.private_data = &priv;
1600 param.private_data_len = sizeof(priv);
1601
1602 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1603 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1604
1605
1606
1607
1608 if (priv.qid == 0) {
1609 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1610 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1611 } else {
1612
1613
1614
1615
1616
1617 priv.hrqsize = cpu_to_le16(queue->queue_size);
1618 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1619 }
1620
1621 ret = rdma_connect(queue->cm_id, ¶m);
1622 if (ret) {
1623 dev_err(ctrl->ctrl.device,
1624 "rdma_connect failed (%d).\n", ret);
1625 goto out_destroy_queue_ib;
1626 }
1627
1628 return 0;
1629
1630 out_destroy_queue_ib:
1631 nvme_rdma_destroy_queue_ib(queue);
1632 return ret;
1633 }
1634
1635 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1636 struct rdma_cm_event *ev)
1637 {
1638 struct nvme_rdma_queue *queue = cm_id->context;
1639 int cm_error = 0;
1640
1641 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1642 rdma_event_msg(ev->event), ev->event,
1643 ev->status, cm_id);
1644
1645 switch (ev->event) {
1646 case RDMA_CM_EVENT_ADDR_RESOLVED:
1647 cm_error = nvme_rdma_addr_resolved(queue);
1648 break;
1649 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1650 cm_error = nvme_rdma_route_resolved(queue);
1651 break;
1652 case RDMA_CM_EVENT_ESTABLISHED:
1653 queue->cm_error = nvme_rdma_conn_established(queue);
1654
1655 complete(&queue->cm_done);
1656 return 0;
1657 case RDMA_CM_EVENT_REJECTED:
1658 nvme_rdma_destroy_queue_ib(queue);
1659 cm_error = nvme_rdma_conn_rejected(queue, ev);
1660 break;
1661 case RDMA_CM_EVENT_ROUTE_ERROR:
1662 case RDMA_CM_EVENT_CONNECT_ERROR:
1663 case RDMA_CM_EVENT_UNREACHABLE:
1664 nvme_rdma_destroy_queue_ib(queue);
1665
1666 case RDMA_CM_EVENT_ADDR_ERROR:
1667 dev_dbg(queue->ctrl->ctrl.device,
1668 "CM error event %d\n", ev->event);
1669 cm_error = -ECONNRESET;
1670 break;
1671 case RDMA_CM_EVENT_DISCONNECTED:
1672 case RDMA_CM_EVENT_ADDR_CHANGE:
1673 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1674 dev_dbg(queue->ctrl->ctrl.device,
1675 "disconnect received - connection closed\n");
1676 nvme_rdma_error_recovery(queue->ctrl);
1677 break;
1678 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1679
1680 break;
1681 default:
1682 dev_err(queue->ctrl->ctrl.device,
1683 "Unexpected RDMA CM event (%d)\n", ev->event);
1684 nvme_rdma_error_recovery(queue->ctrl);
1685 break;
1686 }
1687
1688 if (cm_error) {
1689 queue->cm_error = cm_error;
1690 complete(&queue->cm_done);
1691 }
1692
1693 return 0;
1694 }
1695
1696 static enum blk_eh_timer_return
1697 nvme_rdma_timeout(struct request *rq, bool reserved)
1698 {
1699 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1700 struct nvme_rdma_queue *queue = req->queue;
1701 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1702
1703 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1704 rq->tag, nvme_rdma_queue_idx(queue));
1705
1706
1707
1708
1709
1710
1711 if (ctrl->ctrl.state == NVME_CTRL_RESETTING)
1712 return BLK_EH_RESET_TIMER;
1713
1714 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1715
1716
1717
1718
1719
1720 flush_work(&ctrl->err_work);
1721 nvme_rdma_teardown_io_queues(ctrl, false);
1722 nvme_rdma_teardown_admin_queue(ctrl, false);
1723 return BLK_EH_DONE;
1724 }
1725
1726 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1727 nvme_rdma_error_recovery(ctrl);
1728
1729 return BLK_EH_RESET_TIMER;
1730 }
1731
1732 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1733 const struct blk_mq_queue_data *bd)
1734 {
1735 struct nvme_ns *ns = hctx->queue->queuedata;
1736 struct nvme_rdma_queue *queue = hctx->driver_data;
1737 struct request *rq = bd->rq;
1738 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1739 struct nvme_rdma_qe *sqe = &req->sqe;
1740 struct nvme_command *c = sqe->data;
1741 struct ib_device *dev;
1742 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1743 blk_status_t ret;
1744 int err;
1745
1746 WARN_ON_ONCE(rq->tag < 0);
1747
1748 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
1749 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
1750
1751 dev = queue->device->dev;
1752
1753 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
1754 sizeof(struct nvme_command),
1755 DMA_TO_DEVICE);
1756 err = ib_dma_mapping_error(dev, req->sqe.dma);
1757 if (unlikely(err))
1758 return BLK_STS_RESOURCE;
1759
1760 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1761 sizeof(struct nvme_command), DMA_TO_DEVICE);
1762
1763 ret = nvme_setup_cmd(ns, rq, c);
1764 if (ret)
1765 goto unmap_qe;
1766
1767 blk_mq_start_request(rq);
1768
1769 err = nvme_rdma_map_data(queue, rq, c);
1770 if (unlikely(err < 0)) {
1771 dev_err(queue->ctrl->ctrl.device,
1772 "Failed to map data (%d)\n", err);
1773 nvme_cleanup_cmd(rq);
1774 goto err;
1775 }
1776
1777 sqe->cqe.done = nvme_rdma_send_done;
1778
1779 ib_dma_sync_single_for_device(dev, sqe->dma,
1780 sizeof(struct nvme_command), DMA_TO_DEVICE);
1781
1782 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1783 req->mr ? &req->reg_wr.wr : NULL);
1784 if (unlikely(err)) {
1785 nvme_rdma_unmap_data(queue, rq);
1786 goto err;
1787 }
1788
1789 return BLK_STS_OK;
1790
1791 err:
1792 if (err == -ENOMEM || err == -EAGAIN)
1793 ret = BLK_STS_RESOURCE;
1794 else
1795 ret = BLK_STS_IOERR;
1796 unmap_qe:
1797 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
1798 DMA_TO_DEVICE);
1799 return ret;
1800 }
1801
1802 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
1803 {
1804 struct nvme_rdma_queue *queue = hctx->driver_data;
1805
1806 return ib_process_cq_direct(queue->ib_cq, -1);
1807 }
1808
1809 static void nvme_rdma_complete_rq(struct request *rq)
1810 {
1811 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1812 struct nvme_rdma_queue *queue = req->queue;
1813 struct ib_device *ibdev = queue->device->dev;
1814
1815 nvme_rdma_unmap_data(queue, rq);
1816 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
1817 DMA_TO_DEVICE);
1818 nvme_complete_rq(rq);
1819 }
1820
1821 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1822 {
1823 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1824 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1825
1826 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
1827
1828 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1829 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1830 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1831 set->map[HCTX_TYPE_READ].nr_queues =
1832 ctrl->io_queues[HCTX_TYPE_READ];
1833 set->map[HCTX_TYPE_READ].queue_offset =
1834 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1835 } else {
1836
1837 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1838 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1839 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1840 set->map[HCTX_TYPE_READ].nr_queues =
1841 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1842 set->map[HCTX_TYPE_READ].queue_offset = 0;
1843 }
1844 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
1845 ctrl->device->dev, 0);
1846 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
1847 ctrl->device->dev, 0);
1848
1849 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
1850
1851 set->map[HCTX_TYPE_POLL].nr_queues =
1852 ctrl->io_queues[HCTX_TYPE_POLL];
1853 set->map[HCTX_TYPE_POLL].queue_offset =
1854 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1855 ctrl->io_queues[HCTX_TYPE_READ];
1856 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
1857 }
1858
1859 dev_info(ctrl->ctrl.device,
1860 "mapped %d/%d/%d default/read/poll queues.\n",
1861 ctrl->io_queues[HCTX_TYPE_DEFAULT],
1862 ctrl->io_queues[HCTX_TYPE_READ],
1863 ctrl->io_queues[HCTX_TYPE_POLL]);
1864
1865 return 0;
1866 }
1867
1868 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1869 .queue_rq = nvme_rdma_queue_rq,
1870 .complete = nvme_rdma_complete_rq,
1871 .init_request = nvme_rdma_init_request,
1872 .exit_request = nvme_rdma_exit_request,
1873 .init_hctx = nvme_rdma_init_hctx,
1874 .timeout = nvme_rdma_timeout,
1875 .map_queues = nvme_rdma_map_queues,
1876 .poll = nvme_rdma_poll,
1877 };
1878
1879 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1880 .queue_rq = nvme_rdma_queue_rq,
1881 .complete = nvme_rdma_complete_rq,
1882 .init_request = nvme_rdma_init_request,
1883 .exit_request = nvme_rdma_exit_request,
1884 .init_hctx = nvme_rdma_init_admin_hctx,
1885 .timeout = nvme_rdma_timeout,
1886 };
1887
1888 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1889 {
1890 cancel_work_sync(&ctrl->err_work);
1891 cancel_delayed_work_sync(&ctrl->reconnect_work);
1892
1893 nvme_rdma_teardown_io_queues(ctrl, shutdown);
1894 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1895 if (shutdown)
1896 nvme_shutdown_ctrl(&ctrl->ctrl);
1897 else
1898 nvme_disable_ctrl(&ctrl->ctrl);
1899 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
1900 }
1901
1902 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
1903 {
1904 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
1905 }
1906
1907 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1908 {
1909 struct nvme_rdma_ctrl *ctrl =
1910 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1911
1912 nvme_stop_ctrl(&ctrl->ctrl);
1913 nvme_rdma_shutdown_ctrl(ctrl, false);
1914
1915 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1916
1917 WARN_ON_ONCE(1);
1918 return;
1919 }
1920
1921 if (nvme_rdma_setup_ctrl(ctrl, false))
1922 goto out_fail;
1923
1924 return;
1925
1926 out_fail:
1927 ++ctrl->ctrl.nr_reconnects;
1928 nvme_rdma_reconnect_or_remove(ctrl);
1929 }
1930
1931 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1932 .name = "rdma",
1933 .module = THIS_MODULE,
1934 .flags = NVME_F_FABRICS,
1935 .reg_read32 = nvmf_reg_read32,
1936 .reg_read64 = nvmf_reg_read64,
1937 .reg_write32 = nvmf_reg_write32,
1938 .free_ctrl = nvme_rdma_free_ctrl,
1939 .submit_async_event = nvme_rdma_submit_async_event,
1940 .delete_ctrl = nvme_rdma_delete_ctrl,
1941 .get_address = nvmf_get_address,
1942 };
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956 static bool
1957 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1958 {
1959 struct nvme_rdma_ctrl *ctrl;
1960 bool found = false;
1961
1962 mutex_lock(&nvme_rdma_ctrl_mutex);
1963 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1964 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
1965 if (found)
1966 break;
1967 }
1968 mutex_unlock(&nvme_rdma_ctrl_mutex);
1969
1970 return found;
1971 }
1972
1973 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1974 struct nvmf_ctrl_options *opts)
1975 {
1976 struct nvme_rdma_ctrl *ctrl;
1977 int ret;
1978 bool changed;
1979
1980 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1981 if (!ctrl)
1982 return ERR_PTR(-ENOMEM);
1983 ctrl->ctrl.opts = opts;
1984 INIT_LIST_HEAD(&ctrl->list);
1985
1986 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1987 opts->trsvcid =
1988 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1989 if (!opts->trsvcid) {
1990 ret = -ENOMEM;
1991 goto out_free_ctrl;
1992 }
1993 opts->mask |= NVMF_OPT_TRSVCID;
1994 }
1995
1996 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1997 opts->traddr, opts->trsvcid, &ctrl->addr);
1998 if (ret) {
1999 pr_err("malformed address passed: %s:%s\n",
2000 opts->traddr, opts->trsvcid);
2001 goto out_free_ctrl;
2002 }
2003
2004 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2005 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2006 opts->host_traddr, NULL, &ctrl->src_addr);
2007 if (ret) {
2008 pr_err("malformed src address passed: %s\n",
2009 opts->host_traddr);
2010 goto out_free_ctrl;
2011 }
2012 }
2013
2014 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2015 ret = -EALREADY;
2016 goto out_free_ctrl;
2017 }
2018
2019 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2020 nvme_rdma_reconnect_ctrl_work);
2021 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
2022 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2023
2024 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2025 opts->nr_poll_queues + 1;
2026 ctrl->ctrl.sqsize = opts->queue_size - 1;
2027 ctrl->ctrl.kato = opts->kato;
2028
2029 ret = -ENOMEM;
2030 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2031 GFP_KERNEL);
2032 if (!ctrl->queues)
2033 goto out_free_ctrl;
2034
2035 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2036 0 );
2037 if (ret)
2038 goto out_kfree_queues;
2039
2040 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2041 WARN_ON_ONCE(!changed);
2042
2043 ret = nvme_rdma_setup_ctrl(ctrl, true);
2044 if (ret)
2045 goto out_uninit_ctrl;
2046
2047 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2048 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2049
2050 nvme_get_ctrl(&ctrl->ctrl);
2051
2052 mutex_lock(&nvme_rdma_ctrl_mutex);
2053 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2054 mutex_unlock(&nvme_rdma_ctrl_mutex);
2055
2056 return &ctrl->ctrl;
2057
2058 out_uninit_ctrl:
2059 nvme_uninit_ctrl(&ctrl->ctrl);
2060 nvme_put_ctrl(&ctrl->ctrl);
2061 if (ret > 0)
2062 ret = -EIO;
2063 return ERR_PTR(ret);
2064 out_kfree_queues:
2065 kfree(ctrl->queues);
2066 out_free_ctrl:
2067 kfree(ctrl);
2068 return ERR_PTR(ret);
2069 }
2070
2071 static struct nvmf_transport_ops nvme_rdma_transport = {
2072 .name = "rdma",
2073 .module = THIS_MODULE,
2074 .required_opts = NVMF_OPT_TRADDR,
2075 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2076 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2077 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2078 NVMF_OPT_TOS,
2079 .create_ctrl = nvme_rdma_create_ctrl,
2080 };
2081
2082 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2083 {
2084 struct nvme_rdma_ctrl *ctrl;
2085 struct nvme_rdma_device *ndev;
2086 bool found = false;
2087
2088 mutex_lock(&device_list_mutex);
2089 list_for_each_entry(ndev, &device_list, entry) {
2090 if (ndev->dev == ib_device) {
2091 found = true;
2092 break;
2093 }
2094 }
2095 mutex_unlock(&device_list_mutex);
2096
2097 if (!found)
2098 return;
2099
2100
2101 mutex_lock(&nvme_rdma_ctrl_mutex);
2102 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2103 if (ctrl->device->dev != ib_device)
2104 continue;
2105 nvme_delete_ctrl(&ctrl->ctrl);
2106 }
2107 mutex_unlock(&nvme_rdma_ctrl_mutex);
2108
2109 flush_workqueue(nvme_delete_wq);
2110 }
2111
2112 static struct ib_client nvme_rdma_ib_client = {
2113 .name = "nvme_rdma",
2114 .remove = nvme_rdma_remove_one
2115 };
2116
2117 static int __init nvme_rdma_init_module(void)
2118 {
2119 int ret;
2120
2121 ret = ib_register_client(&nvme_rdma_ib_client);
2122 if (ret)
2123 return ret;
2124
2125 ret = nvmf_register_transport(&nvme_rdma_transport);
2126 if (ret)
2127 goto err_unreg_client;
2128
2129 return 0;
2130
2131 err_unreg_client:
2132 ib_unregister_client(&nvme_rdma_ib_client);
2133 return ret;
2134 }
2135
2136 static void __exit nvme_rdma_cleanup_module(void)
2137 {
2138 struct nvme_rdma_ctrl *ctrl;
2139
2140 nvmf_unregister_transport(&nvme_rdma_transport);
2141 ib_unregister_client(&nvme_rdma_ib_client);
2142
2143 mutex_lock(&nvme_rdma_ctrl_mutex);
2144 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2145 nvme_delete_ctrl(&ctrl->ctrl);
2146 mutex_unlock(&nvme_rdma_ctrl_mutex);
2147 flush_workqueue(nvme_delete_wq);
2148 }
2149
2150 module_init(nvme_rdma_init_module);
2151 module_exit(nvme_rdma_cleanup_module);
2152
2153 MODULE_LICENSE("GPL v2");