Commit | Line | Data |
---|---|---|
71102307 CH |
1 | /* |
2 | * NVMe over Fabrics RDMA host code. | |
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 | |
71102307 CH |
15 | #include <linux/module.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/string.h> | |
71102307 CH |
20 | #include <linux/atomic.h> |
21 | #include <linux/blk-mq.h> | |
22 | #include <linux/types.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/mutex.h> | |
25 | #include <linux/scatterlist.h> | |
26 | #include <linux/nvme.h> | |
71102307 CH |
27 | #include <asm/unaligned.h> |
28 | ||
29 | #include <rdma/ib_verbs.h> | |
30 | #include <rdma/rdma_cm.h> | |
31 | #include <rdma/ib_cm.h> | |
32 | #include <linux/nvme-rdma.h> | |
33 | ||
34 | #include "nvme.h" | |
35 | #include "fabrics.h" | |
36 | ||
37 | ||
38 | #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */ | |
39 | ||
40 | #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */ | |
41 | ||
42 | #define NVME_RDMA_MAX_SEGMENTS 256 | |
43 | ||
44 | #define NVME_RDMA_MAX_INLINE_SEGMENTS 1 | |
45 | ||
71102307 CH |
46 | /* |
47 | * We handle AEN commands ourselves and don't even let the | |
48 | * block layer know about them. | |
49 | */ | |
50 | #define NVME_RDMA_NR_AEN_COMMANDS 1 | |
51 | #define NVME_RDMA_AQ_BLKMQ_DEPTH \ | |
52 | (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS) | |
53 | ||
54 | struct nvme_rdma_device { | |
55 | struct ib_device *dev; | |
56 | struct ib_pd *pd; | |
57 | struct ib_mr *mr; | |
58 | struct kref ref; | |
59 | struct list_head entry; | |
60 | }; | |
61 | ||
62 | struct nvme_rdma_qe { | |
63 | struct ib_cqe cqe; | |
64 | void *data; | |
65 | u64 dma; | |
66 | }; | |
67 | ||
68 | struct nvme_rdma_queue; | |
69 | struct nvme_rdma_request { | |
70 | struct ib_mr *mr; | |
71 | struct nvme_rdma_qe sqe; | |
72 | struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; | |
73 | u32 num_sge; | |
74 | int nents; | |
75 | bool inline_data; | |
71102307 CH |
76 | struct ib_reg_wr reg_wr; |
77 | struct ib_cqe reg_cqe; | |
78 | struct nvme_rdma_queue *queue; | |
79 | struct sg_table sg_table; | |
80 | struct scatterlist first_sgl[]; | |
81 | }; | |
82 | ||
83 | enum nvme_rdma_queue_flags { | |
84 | NVME_RDMA_Q_CONNECTED = (1 << 0), | |
f361e5a0 | 85 | NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1), |
e89ca58f | 86 | NVME_RDMA_Q_DELETING = (1 << 2), |
71102307 CH |
87 | }; |
88 | ||
89 | struct nvme_rdma_queue { | |
90 | struct nvme_rdma_qe *rsp_ring; | |
91 | u8 sig_count; | |
92 | int queue_size; | |
93 | size_t cmnd_capsule_len; | |
94 | struct nvme_rdma_ctrl *ctrl; | |
95 | struct nvme_rdma_device *device; | |
96 | struct ib_cq *ib_cq; | |
97 | struct ib_qp *qp; | |
98 | ||
99 | unsigned long flags; | |
100 | struct rdma_cm_id *cm_id; | |
101 | int cm_error; | |
102 | struct completion cm_done; | |
103 | }; | |
104 | ||
105 | struct nvme_rdma_ctrl { | |
106 | /* read and written in the hot path */ | |
107 | spinlock_t lock; | |
108 | ||
109 | /* read only in the hot path */ | |
110 | struct nvme_rdma_queue *queues; | |
111 | u32 queue_count; | |
112 | ||
113 | /* other member variables */ | |
71102307 CH |
114 | struct blk_mq_tag_set tag_set; |
115 | struct work_struct delete_work; | |
116 | struct work_struct reset_work; | |
117 | struct work_struct err_work; | |
118 | ||
119 | struct nvme_rdma_qe async_event_sqe; | |
120 | ||
121 | int reconnect_delay; | |
122 | struct delayed_work reconnect_work; | |
123 | ||
124 | struct list_head list; | |
125 | ||
126 | struct blk_mq_tag_set admin_tag_set; | |
127 | struct nvme_rdma_device *device; | |
128 | ||
129 | u64 cap; | |
130 | u32 max_fr_pages; | |
131 | ||
132 | union { | |
133 | struct sockaddr addr; | |
134 | struct sockaddr_in addr_in; | |
135 | }; | |
136 | ||
137 | struct nvme_ctrl ctrl; | |
138 | }; | |
139 | ||
140 | static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) | |
141 | { | |
142 | return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); | |
143 | } | |
144 | ||
145 | static LIST_HEAD(device_list); | |
146 | static DEFINE_MUTEX(device_list_mutex); | |
147 | ||
148 | static LIST_HEAD(nvme_rdma_ctrl_list); | |
149 | static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); | |
150 | ||
151 | static struct workqueue_struct *nvme_rdma_wq; | |
152 | ||
153 | /* | |
154 | * Disabling this option makes small I/O goes faster, but is fundamentally | |
155 | * unsafe. With it turned off we will have to register a global rkey that | |
156 | * allows read and write access to all physical memory. | |
157 | */ | |
158 | static bool register_always = true; | |
159 | module_param(register_always, bool, 0444); | |
160 | MODULE_PARM_DESC(register_always, | |
161 | "Use memory registration even for contiguous memory regions"); | |
162 | ||
163 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, | |
164 | struct rdma_cm_event *event); | |
165 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); | |
71102307 CH |
166 | |
167 | /* XXX: really should move to a generic header sooner or later.. */ | |
168 | static inline void put_unaligned_le24(u32 val, u8 *p) | |
169 | { | |
170 | *p++ = val; | |
171 | *p++ = val >> 8; | |
172 | *p++ = val >> 16; | |
173 | } | |
174 | ||
175 | static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) | |
176 | { | |
177 | return queue - queue->ctrl->queues; | |
178 | } | |
179 | ||
180 | static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) | |
181 | { | |
182 | return queue->cmnd_capsule_len - sizeof(struct nvme_command); | |
183 | } | |
184 | ||
185 | static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, | |
186 | size_t capsule_size, enum dma_data_direction dir) | |
187 | { | |
188 | ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); | |
189 | kfree(qe->data); | |
190 | } | |
191 | ||
192 | static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, | |
193 | size_t capsule_size, enum dma_data_direction dir) | |
194 | { | |
195 | qe->data = kzalloc(capsule_size, GFP_KERNEL); | |
196 | if (!qe->data) | |
197 | return -ENOMEM; | |
198 | ||
199 | qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); | |
200 | if (ib_dma_mapping_error(ibdev, qe->dma)) { | |
201 | kfree(qe->data); | |
202 | return -ENOMEM; | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | static void nvme_rdma_free_ring(struct ib_device *ibdev, | |
209 | struct nvme_rdma_qe *ring, size_t ib_queue_size, | |
210 | size_t capsule_size, enum dma_data_direction dir) | |
211 | { | |
212 | int i; | |
213 | ||
214 | for (i = 0; i < ib_queue_size; i++) | |
215 | nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); | |
216 | kfree(ring); | |
217 | } | |
218 | ||
219 | static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, | |
220 | size_t ib_queue_size, size_t capsule_size, | |
221 | enum dma_data_direction dir) | |
222 | { | |
223 | struct nvme_rdma_qe *ring; | |
224 | int i; | |
225 | ||
226 | ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); | |
227 | if (!ring) | |
228 | return NULL; | |
229 | ||
230 | for (i = 0; i < ib_queue_size; i++) { | |
231 | if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) | |
232 | goto out_free_ring; | |
233 | } | |
234 | ||
235 | return ring; | |
236 | ||
237 | out_free_ring: | |
238 | nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | static void nvme_rdma_qp_event(struct ib_event *event, void *context) | |
243 | { | |
244 | pr_debug("QP event %d\n", event->event); | |
245 | } | |
246 | ||
247 | static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) | |
248 | { | |
249 | wait_for_completion_interruptible_timeout(&queue->cm_done, | |
250 | msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); | |
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 | /* +1 for drain */ | |
263 | init_attr.cap.max_send_wr = factor * queue->queue_size + 1; | |
264 | /* +1 for drain */ | |
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 + NVME_RDMA_MAX_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 int nvme_rdma_reinit_request(void *data, struct request *rq) | |
280 | { | |
281 | struct nvme_rdma_ctrl *ctrl = data; | |
282 | struct nvme_rdma_device *dev = ctrl->device; | |
283 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
284 | int ret = 0; | |
285 | ||
f5b7b559 | 286 | if (!req->mr->need_inval) |
71102307 CH |
287 | goto out; |
288 | ||
289 | ib_dereg_mr(req->mr); | |
290 | ||
291 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, | |
292 | ctrl->max_fr_pages); | |
293 | if (IS_ERR(req->mr)) { | |
71102307 | 294 | ret = PTR_ERR(req->mr); |
458a9632 | 295 | req->mr = NULL; |
1bda18de | 296 | goto out; |
71102307 CH |
297 | } |
298 | ||
f5b7b559 | 299 | req->mr->need_inval = false; |
71102307 CH |
300 | |
301 | out: | |
302 | return ret; | |
303 | } | |
304 | ||
305 | static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl, | |
306 | struct request *rq, unsigned int queue_idx) | |
307 | { | |
308 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
309 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; | |
310 | struct nvme_rdma_device *dev = queue->device; | |
311 | ||
312 | if (req->mr) | |
313 | ib_dereg_mr(req->mr); | |
314 | ||
315 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), | |
316 | DMA_TO_DEVICE); | |
317 | } | |
318 | ||
319 | static void nvme_rdma_exit_request(void *data, struct request *rq, | |
320 | unsigned int hctx_idx, unsigned int rq_idx) | |
321 | { | |
322 | return __nvme_rdma_exit_request(data, rq, hctx_idx + 1); | |
323 | } | |
324 | ||
325 | static void nvme_rdma_exit_admin_request(void *data, struct request *rq, | |
326 | unsigned int hctx_idx, unsigned int rq_idx) | |
327 | { | |
328 | return __nvme_rdma_exit_request(data, rq, 0); | |
329 | } | |
330 | ||
331 | static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl, | |
332 | struct request *rq, unsigned int queue_idx) | |
333 | { | |
334 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
335 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; | |
336 | struct nvme_rdma_device *dev = queue->device; | |
337 | struct ib_device *ibdev = dev->dev; | |
338 | int ret; | |
339 | ||
340 | BUG_ON(queue_idx >= ctrl->queue_count); | |
341 | ||
342 | ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command), | |
343 | DMA_TO_DEVICE); | |
344 | if (ret) | |
345 | return ret; | |
346 | ||
347 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, | |
348 | ctrl->max_fr_pages); | |
349 | if (IS_ERR(req->mr)) { | |
350 | ret = PTR_ERR(req->mr); | |
351 | goto out_free_qe; | |
352 | } | |
353 | ||
354 | req->queue = queue; | |
355 | ||
356 | return 0; | |
357 | ||
358 | out_free_qe: | |
359 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), | |
360 | DMA_TO_DEVICE); | |
361 | return -ENOMEM; | |
362 | } | |
363 | ||
364 | static int nvme_rdma_init_request(void *data, struct request *rq, | |
365 | unsigned int hctx_idx, unsigned int rq_idx, | |
366 | unsigned int numa_node) | |
367 | { | |
368 | return __nvme_rdma_init_request(data, rq, hctx_idx + 1); | |
369 | } | |
370 | ||
371 | static int nvme_rdma_init_admin_request(void *data, struct request *rq, | |
372 | unsigned int hctx_idx, unsigned int rq_idx, | |
373 | unsigned int numa_node) | |
374 | { | |
375 | return __nvme_rdma_init_request(data, rq, 0); | |
376 | } | |
377 | ||
378 | static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, | |
379 | unsigned int hctx_idx) | |
380 | { | |
381 | struct nvme_rdma_ctrl *ctrl = data; | |
382 | struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; | |
383 | ||
384 | BUG_ON(hctx_idx >= ctrl->queue_count); | |
385 | ||
386 | hctx->driver_data = queue; | |
387 | return 0; | |
388 | } | |
389 | ||
390 | static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, | |
391 | unsigned int hctx_idx) | |
392 | { | |
393 | struct nvme_rdma_ctrl *ctrl = data; | |
394 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; | |
395 | ||
396 | BUG_ON(hctx_idx != 0); | |
397 | ||
398 | hctx->driver_data = queue; | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static void nvme_rdma_free_dev(struct kref *ref) | |
403 | { | |
404 | struct nvme_rdma_device *ndev = | |
405 | container_of(ref, struct nvme_rdma_device, ref); | |
406 | ||
407 | mutex_lock(&device_list_mutex); | |
408 | list_del(&ndev->entry); | |
409 | mutex_unlock(&device_list_mutex); | |
410 | ||
411 | if (!register_always) | |
412 | ib_dereg_mr(ndev->mr); | |
413 | ib_dealloc_pd(ndev->pd); | |
414 | ||
415 | kfree(ndev); | |
416 | } | |
417 | ||
418 | static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) | |
419 | { | |
420 | kref_put(&dev->ref, nvme_rdma_free_dev); | |
421 | } | |
422 | ||
423 | static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) | |
424 | { | |
425 | return kref_get_unless_zero(&dev->ref); | |
426 | } | |
427 | ||
428 | static struct nvme_rdma_device * | |
429 | nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) | |
430 | { | |
431 | struct nvme_rdma_device *ndev; | |
432 | ||
433 | mutex_lock(&device_list_mutex); | |
434 | list_for_each_entry(ndev, &device_list, entry) { | |
435 | if (ndev->dev->node_guid == cm_id->device->node_guid && | |
436 | nvme_rdma_dev_get(ndev)) | |
437 | goto out_unlock; | |
438 | } | |
439 | ||
440 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); | |
441 | if (!ndev) | |
442 | goto out_err; | |
443 | ||
444 | ndev->dev = cm_id->device; | |
445 | kref_init(&ndev->ref); | |
446 | ||
447 | ndev->pd = ib_alloc_pd(ndev->dev); | |
448 | if (IS_ERR(ndev->pd)) | |
449 | goto out_free_dev; | |
450 | ||
451 | if (!register_always) { | |
452 | ndev->mr = ib_get_dma_mr(ndev->pd, | |
453 | IB_ACCESS_LOCAL_WRITE | | |
454 | IB_ACCESS_REMOTE_READ | | |
455 | IB_ACCESS_REMOTE_WRITE); | |
456 | if (IS_ERR(ndev->mr)) | |
457 | goto out_free_pd; | |
458 | } | |
459 | ||
460 | if (!(ndev->dev->attrs.device_cap_flags & | |
461 | IB_DEVICE_MEM_MGT_EXTENSIONS)) { | |
462 | dev_err(&ndev->dev->dev, | |
463 | "Memory registrations not supported.\n"); | |
464 | goto out_free_mr; | |
465 | } | |
466 | ||
467 | list_add(&ndev->entry, &device_list); | |
468 | out_unlock: | |
469 | mutex_unlock(&device_list_mutex); | |
470 | return ndev; | |
471 | ||
472 | out_free_mr: | |
473 | if (!register_always) | |
474 | ib_dereg_mr(ndev->mr); | |
475 | out_free_pd: | |
476 | ib_dealloc_pd(ndev->pd); | |
477 | out_free_dev: | |
478 | kfree(ndev); | |
479 | out_err: | |
480 | mutex_unlock(&device_list_mutex); | |
481 | return NULL; | |
482 | } | |
483 | ||
484 | static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) | |
485 | { | |
f361e5a0 SW |
486 | struct nvme_rdma_device *dev; |
487 | struct ib_device *ibdev; | |
71102307 | 488 | |
f361e5a0 SW |
489 | if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags)) |
490 | return; | |
491 | ||
492 | dev = queue->device; | |
493 | ibdev = dev->dev; | |
71102307 CH |
494 | rdma_destroy_qp(queue->cm_id); |
495 | ib_free_cq(queue->ib_cq); | |
496 | ||
497 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, | |
498 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); | |
499 | ||
500 | nvme_rdma_dev_put(dev); | |
501 | } | |
502 | ||
503 | static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue, | |
504 | struct nvme_rdma_device *dev) | |
505 | { | |
506 | struct ib_device *ibdev = dev->dev; | |
507 | const int send_wr_factor = 3; /* MR, SEND, INV */ | |
508 | const int cq_factor = send_wr_factor + 1; /* + RECV */ | |
509 | int comp_vector, idx = nvme_rdma_queue_idx(queue); | |
510 | ||
511 | int ret; | |
512 | ||
513 | queue->device = dev; | |
514 | ||
515 | /* | |
516 | * The admin queue is barely used once the controller is live, so don't | |
517 | * bother to spread it out. | |
518 | */ | |
519 | if (idx == 0) | |
520 | comp_vector = 0; | |
521 | else | |
522 | comp_vector = idx % ibdev->num_comp_vectors; | |
523 | ||
524 | ||
525 | /* +1 for ib_stop_cq */ | |
526 | queue->ib_cq = ib_alloc_cq(dev->dev, queue, | |
527 | cq_factor * queue->queue_size + 1, comp_vector, | |
528 | IB_POLL_SOFTIRQ); | |
529 | if (IS_ERR(queue->ib_cq)) { | |
530 | ret = PTR_ERR(queue->ib_cq); | |
531 | goto out; | |
532 | } | |
533 | ||
534 | ret = nvme_rdma_create_qp(queue, send_wr_factor); | |
535 | if (ret) | |
536 | goto out_destroy_ib_cq; | |
537 | ||
538 | queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, | |
539 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); | |
540 | if (!queue->rsp_ring) { | |
541 | ret = -ENOMEM; | |
542 | goto out_destroy_qp; | |
543 | } | |
f361e5a0 | 544 | set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags); |
71102307 CH |
545 | |
546 | return 0; | |
547 | ||
548 | out_destroy_qp: | |
549 | ib_destroy_qp(queue->qp); | |
550 | out_destroy_ib_cq: | |
551 | ib_free_cq(queue->ib_cq); | |
552 | out: | |
553 | return ret; | |
554 | } | |
555 | ||
556 | static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl, | |
557 | int idx, size_t queue_size) | |
558 | { | |
559 | struct nvme_rdma_queue *queue; | |
560 | int ret; | |
561 | ||
562 | queue = &ctrl->queues[idx]; | |
563 | queue->ctrl = ctrl; | |
564 | init_completion(&queue->cm_done); | |
565 | ||
566 | if (idx > 0) | |
567 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; | |
568 | else | |
569 | queue->cmnd_capsule_len = sizeof(struct nvme_command); | |
570 | ||
571 | queue->queue_size = queue_size; | |
572 | ||
573 | queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, | |
574 | RDMA_PS_TCP, IB_QPT_RC); | |
575 | if (IS_ERR(queue->cm_id)) { | |
576 | dev_info(ctrl->ctrl.device, | |
577 | "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); | |
578 | return PTR_ERR(queue->cm_id); | |
579 | } | |
580 | ||
581 | queue->cm_error = -ETIMEDOUT; | |
582 | ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr, | |
583 | NVME_RDMA_CONNECT_TIMEOUT_MS); | |
584 | if (ret) { | |
585 | dev_info(ctrl->ctrl.device, | |
586 | "rdma_resolve_addr failed (%d).\n", ret); | |
587 | goto out_destroy_cm_id; | |
588 | } | |
589 | ||
590 | ret = nvme_rdma_wait_for_cm(queue); | |
591 | if (ret) { | |
592 | dev_info(ctrl->ctrl.device, | |
593 | "rdma_resolve_addr wait failed (%d).\n", ret); | |
594 | goto out_destroy_cm_id; | |
595 | } | |
596 | ||
3b4ac786 | 597 | clear_bit(NVME_RDMA_Q_DELETING, &queue->flags); |
71102307 CH |
598 | set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags); |
599 | ||
600 | return 0; | |
601 | ||
602 | out_destroy_cm_id: | |
f361e5a0 | 603 | nvme_rdma_destroy_queue_ib(queue); |
71102307 CH |
604 | rdma_destroy_id(queue->cm_id); |
605 | return ret; | |
606 | } | |
607 | ||
608 | static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) | |
609 | { | |
610 | rdma_disconnect(queue->cm_id); | |
611 | ib_drain_qp(queue->qp); | |
612 | } | |
613 | ||
614 | static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) | |
615 | { | |
616 | nvme_rdma_destroy_queue_ib(queue); | |
617 | rdma_destroy_id(queue->cm_id); | |
618 | } | |
619 | ||
620 | static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue) | |
621 | { | |
e89ca58f | 622 | if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags)) |
71102307 CH |
623 | return; |
624 | nvme_rdma_stop_queue(queue); | |
625 | nvme_rdma_free_queue(queue); | |
626 | } | |
627 | ||
628 | static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) | |
629 | { | |
630 | int i; | |
631 | ||
632 | for (i = 1; i < ctrl->queue_count; i++) | |
633 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); | |
634 | } | |
635 | ||
636 | static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl) | |
637 | { | |
638 | int i, ret = 0; | |
639 | ||
640 | for (i = 1; i < ctrl->queue_count; i++) { | |
641 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i); | |
642 | if (ret) | |
643 | break; | |
644 | } | |
645 | ||
646 | return ret; | |
647 | } | |
648 | ||
649 | static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl) | |
650 | { | |
651 | int i, ret; | |
652 | ||
653 | for (i = 1; i < ctrl->queue_count; i++) { | |
c5af8654 JF |
654 | ret = nvme_rdma_init_queue(ctrl, i, |
655 | ctrl->ctrl.opts->queue_size); | |
71102307 CH |
656 | if (ret) { |
657 | dev_info(ctrl->ctrl.device, | |
658 | "failed to initialize i/o queue: %d\n", ret); | |
659 | goto out_free_queues; | |
660 | } | |
661 | } | |
662 | ||
663 | return 0; | |
664 | ||
665 | out_free_queues: | |
f361e5a0 | 666 | for (i--; i >= 1; i--) |
71102307 CH |
667 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); |
668 | ||
669 | return ret; | |
670 | } | |
671 | ||
672 | static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) | |
673 | { | |
674 | nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe, | |
675 | sizeof(struct nvme_command), DMA_TO_DEVICE); | |
676 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); | |
677 | blk_cleanup_queue(ctrl->ctrl.admin_q); | |
678 | blk_mq_free_tag_set(&ctrl->admin_tag_set); | |
679 | nvme_rdma_dev_put(ctrl->device); | |
680 | } | |
681 | ||
682 | static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) | |
683 | { | |
684 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); | |
685 | ||
686 | if (list_empty(&ctrl->list)) | |
687 | goto free_ctrl; | |
688 | ||
689 | mutex_lock(&nvme_rdma_ctrl_mutex); | |
690 | list_del(&ctrl->list); | |
691 | mutex_unlock(&nvme_rdma_ctrl_mutex); | |
692 | ||
71102307 CH |
693 | kfree(ctrl->queues); |
694 | nvmf_free_options(nctrl->opts); | |
695 | free_ctrl: | |
696 | kfree(ctrl); | |
697 | } | |
698 | ||
699 | static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) | |
700 | { | |
701 | struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), | |
702 | struct nvme_rdma_ctrl, reconnect_work); | |
703 | bool changed; | |
704 | int ret; | |
705 | ||
706 | if (ctrl->queue_count > 1) { | |
707 | nvme_rdma_free_io_queues(ctrl); | |
708 | ||
709 | ret = blk_mq_reinit_tagset(&ctrl->tag_set); | |
710 | if (ret) | |
711 | goto requeue; | |
712 | } | |
713 | ||
714 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); | |
715 | ||
716 | ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set); | |
717 | if (ret) | |
718 | goto requeue; | |
719 | ||
720 | ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); | |
721 | if (ret) | |
722 | goto requeue; | |
723 | ||
724 | blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true); | |
725 | ||
726 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); | |
727 | if (ret) | |
728 | goto stop_admin_q; | |
729 | ||
730 | ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); | |
731 | if (ret) | |
732 | goto stop_admin_q; | |
733 | ||
734 | nvme_start_keep_alive(&ctrl->ctrl); | |
735 | ||
736 | if (ctrl->queue_count > 1) { | |
737 | ret = nvme_rdma_init_io_queues(ctrl); | |
738 | if (ret) | |
739 | goto stop_admin_q; | |
740 | ||
741 | ret = nvme_rdma_connect_io_queues(ctrl); | |
742 | if (ret) | |
743 | goto stop_admin_q; | |
744 | } | |
745 | ||
746 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); | |
747 | WARN_ON_ONCE(!changed); | |
748 | ||
5f372eb3 | 749 | if (ctrl->queue_count > 1) { |
71102307 | 750 | nvme_start_queues(&ctrl->ctrl); |
5f372eb3 | 751 | nvme_queue_scan(&ctrl->ctrl); |
3ef1b4b2 | 752 | nvme_queue_async_events(&ctrl->ctrl); |
5f372eb3 | 753 | } |
71102307 CH |
754 | |
755 | dev_info(ctrl->ctrl.device, "Successfully reconnected\n"); | |
756 | ||
757 | return; | |
758 | ||
759 | stop_admin_q: | |
760 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); | |
761 | requeue: | |
762 | /* Make sure we are not resetting/deleting */ | |
763 | if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) { | |
764 | dev_info(ctrl->ctrl.device, | |
765 | "Failed reconnect attempt, requeueing...\n"); | |
766 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, | |
767 | ctrl->reconnect_delay * HZ); | |
768 | } | |
769 | } | |
770 | ||
771 | static void nvme_rdma_error_recovery_work(struct work_struct *work) | |
772 | { | |
773 | struct nvme_rdma_ctrl *ctrl = container_of(work, | |
774 | struct nvme_rdma_ctrl, err_work); | |
e89ca58f | 775 | int i; |
71102307 CH |
776 | |
777 | nvme_stop_keep_alive(&ctrl->ctrl); | |
e89ca58f SG |
778 | |
779 | for (i = 0; i < ctrl->queue_count; i++) | |
780 | clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags); | |
781 | ||
71102307 CH |
782 | if (ctrl->queue_count > 1) |
783 | nvme_stop_queues(&ctrl->ctrl); | |
784 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); | |
785 | ||
786 | /* We must take care of fastfail/requeue all our inflight requests */ | |
787 | if (ctrl->queue_count > 1) | |
788 | blk_mq_tagset_busy_iter(&ctrl->tag_set, | |
789 | nvme_cancel_request, &ctrl->ctrl); | |
790 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, | |
791 | nvme_cancel_request, &ctrl->ctrl); | |
792 | ||
793 | dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n", | |
794 | ctrl->reconnect_delay); | |
795 | ||
796 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, | |
797 | ctrl->reconnect_delay * HZ); | |
798 | } | |
799 | ||
800 | static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) | |
801 | { | |
802 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) | |
803 | return; | |
804 | ||
805 | queue_work(nvme_rdma_wq, &ctrl->err_work); | |
806 | } | |
807 | ||
808 | static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, | |
809 | const char *op) | |
810 | { | |
811 | struct nvme_rdma_queue *queue = cq->cq_context; | |
812 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; | |
813 | ||
814 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) | |
815 | dev_info(ctrl->ctrl.device, | |
816 | "%s for CQE 0x%p failed with status %s (%d)\n", | |
817 | op, wc->wr_cqe, | |
818 | ib_wc_status_msg(wc->status), wc->status); | |
819 | nvme_rdma_error_recovery(ctrl); | |
820 | } | |
821 | ||
822 | static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) | |
823 | { | |
824 | if (unlikely(wc->status != IB_WC_SUCCESS)) | |
825 | nvme_rdma_wr_error(cq, wc, "MEMREG"); | |
826 | } | |
827 | ||
828 | static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) | |
829 | { | |
830 | if (unlikely(wc->status != IB_WC_SUCCESS)) | |
831 | nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); | |
832 | } | |
833 | ||
834 | static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, | |
835 | struct nvme_rdma_request *req) | |
836 | { | |
837 | struct ib_send_wr *bad_wr; | |
838 | struct ib_send_wr wr = { | |
839 | .opcode = IB_WR_LOCAL_INV, | |
840 | .next = NULL, | |
841 | .num_sge = 0, | |
842 | .send_flags = 0, | |
843 | .ex.invalidate_rkey = req->mr->rkey, | |
844 | }; | |
845 | ||
846 | req->reg_cqe.done = nvme_rdma_inv_rkey_done; | |
847 | wr.wr_cqe = &req->reg_cqe; | |
848 | ||
849 | return ib_post_send(queue->qp, &wr, &bad_wr); | |
850 | } | |
851 | ||
852 | static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, | |
853 | struct request *rq) | |
854 | { | |
855 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
856 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; | |
857 | struct nvme_rdma_device *dev = queue->device; | |
858 | struct ib_device *ibdev = dev->dev; | |
859 | int res; | |
860 | ||
861 | if (!blk_rq_bytes(rq)) | |
862 | return; | |
863 | ||
f5b7b559 | 864 | if (req->mr->need_inval) { |
71102307 CH |
865 | res = nvme_rdma_inv_rkey(queue, req); |
866 | if (res < 0) { | |
867 | dev_err(ctrl->ctrl.device, | |
868 | "Queueing INV WR for rkey %#x failed (%d)\n", | |
869 | req->mr->rkey, res); | |
870 | nvme_rdma_error_recovery(queue->ctrl); | |
871 | } | |
872 | } | |
873 | ||
874 | ib_dma_unmap_sg(ibdev, req->sg_table.sgl, | |
875 | req->nents, rq_data_dir(rq) == | |
876 | WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
877 | ||
878 | nvme_cleanup_cmd(rq); | |
879 | sg_free_table_chained(&req->sg_table, true); | |
880 | } | |
881 | ||
882 | static int nvme_rdma_set_sg_null(struct nvme_command *c) | |
883 | { | |
884 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; | |
885 | ||
886 | sg->addr = 0; | |
887 | put_unaligned_le24(0, sg->length); | |
888 | put_unaligned_le32(0, sg->key); | |
889 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; | |
890 | return 0; | |
891 | } | |
892 | ||
893 | static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, | |
894 | struct nvme_rdma_request *req, struct nvme_command *c) | |
895 | { | |
896 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; | |
897 | ||
898 | req->sge[1].addr = sg_dma_address(req->sg_table.sgl); | |
899 | req->sge[1].length = sg_dma_len(req->sg_table.sgl); | |
900 | req->sge[1].lkey = queue->device->pd->local_dma_lkey; | |
901 | ||
902 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); | |
903 | sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); | |
904 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; | |
905 | ||
906 | req->inline_data = true; | |
907 | req->num_sge++; | |
908 | return 0; | |
909 | } | |
910 | ||
911 | static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, | |
912 | struct nvme_rdma_request *req, struct nvme_command *c) | |
913 | { | |
914 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; | |
915 | ||
916 | sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); | |
917 | put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); | |
918 | put_unaligned_le32(queue->device->mr->rkey, sg->key); | |
919 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; | |
920 | return 0; | |
921 | } | |
922 | ||
923 | static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, | |
924 | struct nvme_rdma_request *req, struct nvme_command *c, | |
925 | int count) | |
926 | { | |
927 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; | |
928 | int nr; | |
929 | ||
930 | nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE); | |
931 | if (nr < count) { | |
932 | if (nr < 0) | |
933 | return nr; | |
934 | return -EINVAL; | |
935 | } | |
936 | ||
937 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); | |
938 | ||
939 | req->reg_cqe.done = nvme_rdma_memreg_done; | |
940 | memset(&req->reg_wr, 0, sizeof(req->reg_wr)); | |
941 | req->reg_wr.wr.opcode = IB_WR_REG_MR; | |
942 | req->reg_wr.wr.wr_cqe = &req->reg_cqe; | |
943 | req->reg_wr.wr.num_sge = 0; | |
944 | req->reg_wr.mr = req->mr; | |
945 | req->reg_wr.key = req->mr->rkey; | |
946 | req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | | |
947 | IB_ACCESS_REMOTE_READ | | |
948 | IB_ACCESS_REMOTE_WRITE; | |
949 | ||
f5b7b559 | 950 | req->mr->need_inval = true; |
71102307 CH |
951 | |
952 | sg->addr = cpu_to_le64(req->mr->iova); | |
953 | put_unaligned_le24(req->mr->length, sg->length); | |
954 | put_unaligned_le32(req->mr->rkey, sg->key); | |
955 | sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | | |
956 | NVME_SGL_FMT_INVALIDATE; | |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
961 | static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, | |
962 | struct request *rq, unsigned int map_len, | |
963 | struct nvme_command *c) | |
964 | { | |
965 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
966 | struct nvme_rdma_device *dev = queue->device; | |
967 | struct ib_device *ibdev = dev->dev; | |
968 | int nents, count; | |
969 | int ret; | |
970 | ||
971 | req->num_sge = 1; | |
972 | req->inline_data = false; | |
f5b7b559 | 973 | req->mr->need_inval = false; |
71102307 CH |
974 | |
975 | c->common.flags |= NVME_CMD_SGL_METABUF; | |
976 | ||
977 | if (!blk_rq_bytes(rq)) | |
978 | return nvme_rdma_set_sg_null(c); | |
979 | ||
980 | req->sg_table.sgl = req->first_sgl; | |
981 | ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments, | |
982 | req->sg_table.sgl); | |
983 | if (ret) | |
984 | return -ENOMEM; | |
985 | ||
986 | nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); | |
987 | BUG_ON(nents > rq->nr_phys_segments); | |
988 | req->nents = nents; | |
989 | ||
990 | count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents, | |
991 | rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
992 | if (unlikely(count <= 0)) { | |
993 | sg_free_table_chained(&req->sg_table, true); | |
994 | return -EIO; | |
995 | } | |
996 | ||
997 | if (count == 1) { | |
998 | if (rq_data_dir(rq) == WRITE && | |
999 | map_len <= nvme_rdma_inline_data_size(queue) && | |
1000 | nvme_rdma_queue_idx(queue)) | |
1001 | return nvme_rdma_map_sg_inline(queue, req, c); | |
1002 | ||
1003 | if (!register_always) | |
1004 | return nvme_rdma_map_sg_single(queue, req, c); | |
1005 | } | |
1006 | ||
1007 | return nvme_rdma_map_sg_fr(queue, req, c, count); | |
1008 | } | |
1009 | ||
1010 | static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) | |
1011 | { | |
1012 | if (unlikely(wc->status != IB_WC_SUCCESS)) | |
1013 | nvme_rdma_wr_error(cq, wc, "SEND"); | |
1014 | } | |
1015 | ||
1016 | static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, | |
1017 | struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, | |
1018 | struct ib_send_wr *first, bool flush) | |
1019 | { | |
1020 | struct ib_send_wr wr, *bad_wr; | |
1021 | int ret; | |
1022 | ||
1023 | sge->addr = qe->dma; | |
1024 | sge->length = sizeof(struct nvme_command), | |
1025 | sge->lkey = queue->device->pd->local_dma_lkey; | |
1026 | ||
1027 | qe->cqe.done = nvme_rdma_send_done; | |
1028 | ||
1029 | wr.next = NULL; | |
1030 | wr.wr_cqe = &qe->cqe; | |
1031 | wr.sg_list = sge; | |
1032 | wr.num_sge = num_sge; | |
1033 | wr.opcode = IB_WR_SEND; | |
1034 | wr.send_flags = 0; | |
1035 | ||
1036 | /* | |
1037 | * Unsignalled send completions are another giant desaster in the | |
1038 | * IB Verbs spec: If we don't regularly post signalled sends | |
1039 | * the send queue will fill up and only a QP reset will rescue us. | |
1040 | * Would have been way to obvious to handle this in hardware or | |
1041 | * at least the RDMA stack.. | |
1042 | * | |
1043 | * This messy and racy code sniplet is copy and pasted from the iSER | |
1044 | * initiator, and the magic '32' comes from there as well. | |
1045 | * | |
1046 | * Always signal the flushes. The magic request used for the flush | |
1047 | * sequencer is not allocated in our driver's tagset and it's | |
1048 | * triggered to be freed by blk_cleanup_queue(). So we need to | |
1049 | * always mark it as signaled to ensure that the "wr_cqe", which is | |
1050 | * embeded in request's payload, is not freed when __ib_process_cq() | |
1051 | * calls wr_cqe->done(). | |
1052 | */ | |
1053 | if ((++queue->sig_count % 32) == 0 || flush) | |
1054 | wr.send_flags |= IB_SEND_SIGNALED; | |
1055 | ||
1056 | if (first) | |
1057 | first->next = ≀ | |
1058 | else | |
1059 | first = ≀ | |
1060 | ||
1061 | ret = ib_post_send(queue->qp, first, &bad_wr); | |
1062 | if (ret) { | |
1063 | dev_err(queue->ctrl->ctrl.device, | |
1064 | "%s failed with error code %d\n", __func__, ret); | |
1065 | } | |
1066 | return ret; | |
1067 | } | |
1068 | ||
1069 | static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, | |
1070 | struct nvme_rdma_qe *qe) | |
1071 | { | |
1072 | struct ib_recv_wr wr, *bad_wr; | |
1073 | struct ib_sge list; | |
1074 | int ret; | |
1075 | ||
1076 | list.addr = qe->dma; | |
1077 | list.length = sizeof(struct nvme_completion); | |
1078 | list.lkey = queue->device->pd->local_dma_lkey; | |
1079 | ||
1080 | qe->cqe.done = nvme_rdma_recv_done; | |
1081 | ||
1082 | wr.next = NULL; | |
1083 | wr.wr_cqe = &qe->cqe; | |
1084 | wr.sg_list = &list; | |
1085 | wr.num_sge = 1; | |
1086 | ||
1087 | ret = ib_post_recv(queue->qp, &wr, &bad_wr); | |
1088 | if (ret) { | |
1089 | dev_err(queue->ctrl->ctrl.device, | |
1090 | "%s failed with error code %d\n", __func__, ret); | |
1091 | } | |
1092 | return ret; | |
1093 | } | |
1094 | ||
1095 | static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) | |
1096 | { | |
1097 | u32 queue_idx = nvme_rdma_queue_idx(queue); | |
1098 | ||
1099 | if (queue_idx == 0) | |
1100 | return queue->ctrl->admin_tag_set.tags[queue_idx]; | |
1101 | return queue->ctrl->tag_set.tags[queue_idx - 1]; | |
1102 | } | |
1103 | ||
1104 | static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) | |
1105 | { | |
1106 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); | |
1107 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; | |
1108 | struct ib_device *dev = queue->device->dev; | |
1109 | struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; | |
1110 | struct nvme_command *cmd = sqe->data; | |
1111 | struct ib_sge sge; | |
1112 | int ret; | |
1113 | ||
1114 | if (WARN_ON_ONCE(aer_idx != 0)) | |
1115 | return; | |
1116 | ||
1117 | ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); | |
1118 | ||
1119 | memset(cmd, 0, sizeof(*cmd)); | |
1120 | cmd->common.opcode = nvme_admin_async_event; | |
1121 | cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; | |
1122 | cmd->common.flags |= NVME_CMD_SGL_METABUF; | |
1123 | nvme_rdma_set_sg_null(cmd); | |
1124 | ||
1125 | ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), | |
1126 | DMA_TO_DEVICE); | |
1127 | ||
1128 | ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); | |
1129 | WARN_ON_ONCE(ret); | |
1130 | } | |
1131 | ||
1132 | static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, | |
1133 | struct nvme_completion *cqe, struct ib_wc *wc, int tag) | |
1134 | { | |
1135 | u16 status = le16_to_cpu(cqe->status); | |
1136 | struct request *rq; | |
1137 | struct nvme_rdma_request *req; | |
1138 | int ret = 0; | |
1139 | ||
1140 | status >>= 1; | |
1141 | ||
1142 | rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); | |
1143 | if (!rq) { | |
1144 | dev_err(queue->ctrl->ctrl.device, | |
1145 | "tag 0x%x on QP %#x not found\n", | |
1146 | cqe->command_id, queue->qp->qp_num); | |
1147 | nvme_rdma_error_recovery(queue->ctrl); | |
1148 | return ret; | |
1149 | } | |
1150 | req = blk_mq_rq_to_pdu(rq); | |
1151 | ||
1152 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special) | |
1153 | memcpy(rq->special, cqe, sizeof(*cqe)); | |
1154 | ||
1155 | if (rq->tag == tag) | |
1156 | ret = 1; | |
1157 | ||
1158 | if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && | |
1159 | wc->ex.invalidate_rkey == req->mr->rkey) | |
f5b7b559 | 1160 | req->mr->need_inval = false; |
71102307 CH |
1161 | |
1162 | blk_mq_complete_request(rq, status); | |
1163 | ||
1164 | return ret; | |
1165 | } | |
1166 | ||
1167 | static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) | |
1168 | { | |
1169 | struct nvme_rdma_qe *qe = | |
1170 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); | |
1171 | struct nvme_rdma_queue *queue = cq->cq_context; | |
1172 | struct ib_device *ibdev = queue->device->dev; | |
1173 | struct nvme_completion *cqe = qe->data; | |
1174 | const size_t len = sizeof(struct nvme_completion); | |
1175 | int ret = 0; | |
1176 | ||
1177 | if (unlikely(wc->status != IB_WC_SUCCESS)) { | |
1178 | nvme_rdma_wr_error(cq, wc, "RECV"); | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); | |
1183 | /* | |
1184 | * AEN requests are special as they don't time out and can | |
1185 | * survive any kind of queue freeze and often don't respond to | |
1186 | * aborts. We don't even bother to allocate a struct request | |
1187 | * for them but rather special case them here. | |
1188 | */ | |
1189 | if (unlikely(nvme_rdma_queue_idx(queue) == 0 && | |
1190 | cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) | |
1191 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe); | |
1192 | else | |
1193 | ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); | |
1194 | ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); | |
1195 | ||
1196 | nvme_rdma_post_recv(queue, qe); | |
1197 | return ret; | |
1198 | } | |
1199 | ||
1200 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) | |
1201 | { | |
1202 | __nvme_rdma_recv_done(cq, wc, -1); | |
1203 | } | |
1204 | ||
1205 | static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) | |
1206 | { | |
1207 | int ret, i; | |
1208 | ||
1209 | for (i = 0; i < queue->queue_size; i++) { | |
1210 | ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); | |
1211 | if (ret) | |
1212 | goto out_destroy_queue_ib; | |
1213 | } | |
1214 | ||
1215 | return 0; | |
1216 | ||
1217 | out_destroy_queue_ib: | |
1218 | nvme_rdma_destroy_queue_ib(queue); | |
1219 | return ret; | |
1220 | } | |
1221 | ||
1222 | static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, | |
1223 | struct rdma_cm_event *ev) | |
1224 | { | |
1225 | if (ev->param.conn.private_data_len) { | |
1226 | struct nvme_rdma_cm_rej *rej = | |
1227 | (struct nvme_rdma_cm_rej *)ev->param.conn.private_data; | |
1228 | ||
1229 | dev_err(queue->ctrl->ctrl.device, | |
1230 | "Connect rejected, status %d.", le16_to_cpu(rej->sts)); | |
1231 | /* XXX: Think of something clever to do here... */ | |
1232 | } else { | |
1233 | dev_err(queue->ctrl->ctrl.device, | |
1234 | "Connect rejected, no private data.\n"); | |
1235 | } | |
1236 | ||
1237 | return -ECONNRESET; | |
1238 | } | |
1239 | ||
1240 | static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) | |
1241 | { | |
1242 | struct nvme_rdma_device *dev; | |
1243 | int ret; | |
1244 | ||
1245 | dev = nvme_rdma_find_get_device(queue->cm_id); | |
1246 | if (!dev) { | |
1247 | dev_err(queue->cm_id->device->dma_device, | |
1248 | "no client data found!\n"); | |
1249 | return -ECONNREFUSED; | |
1250 | } | |
1251 | ||
1252 | ret = nvme_rdma_create_queue_ib(queue, dev); | |
1253 | if (ret) { | |
1254 | nvme_rdma_dev_put(dev); | |
1255 | goto out; | |
1256 | } | |
1257 | ||
1258 | ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); | |
1259 | if (ret) { | |
1260 | dev_err(queue->ctrl->ctrl.device, | |
1261 | "rdma_resolve_route failed (%d).\n", | |
1262 | queue->cm_error); | |
1263 | goto out_destroy_queue; | |
1264 | } | |
1265 | ||
1266 | return 0; | |
1267 | ||
1268 | out_destroy_queue: | |
1269 | nvme_rdma_destroy_queue_ib(queue); | |
1270 | out: | |
1271 | return ret; | |
1272 | } | |
1273 | ||
1274 | static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) | |
1275 | { | |
1276 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; | |
1277 | struct rdma_conn_param param = { }; | |
0b857b44 | 1278 | struct nvme_rdma_cm_req priv = { }; |
71102307 CH |
1279 | int ret; |
1280 | ||
1281 | param.qp_num = queue->qp->qp_num; | |
1282 | param.flow_control = 1; | |
1283 | ||
1284 | param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; | |
2ac17c28 SG |
1285 | /* maximum retry count */ |
1286 | param.retry_count = 7; | |
71102307 CH |
1287 | param.rnr_retry_count = 7; |
1288 | param.private_data = &priv; | |
1289 | param.private_data_len = sizeof(priv); | |
1290 | ||
1291 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); | |
1292 | priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); | |
f994d9dc JF |
1293 | /* |
1294 | * set the admin queue depth to the minimum size | |
1295 | * specified by the Fabrics standard. | |
1296 | */ | |
1297 | if (priv.qid == 0) { | |
1298 | priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH); | |
1299 | priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1); | |
1300 | } else { | |
c5af8654 JF |
1301 | /* |
1302 | * current interpretation of the fabrics spec | |
1303 | * is at minimum you make hrqsize sqsize+1, or a | |
1304 | * 1's based representation of sqsize. | |
1305 | */ | |
f994d9dc | 1306 | priv.hrqsize = cpu_to_le16(queue->queue_size); |
c5af8654 | 1307 | priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); |
f994d9dc | 1308 | } |
71102307 CH |
1309 | |
1310 | ret = rdma_connect(queue->cm_id, ¶m); | |
1311 | if (ret) { | |
1312 | dev_err(ctrl->ctrl.device, | |
1313 | "rdma_connect failed (%d).\n", ret); | |
1314 | goto out_destroy_queue_ib; | |
1315 | } | |
1316 | ||
1317 | return 0; | |
1318 | ||
1319 | out_destroy_queue_ib: | |
1320 | nvme_rdma_destroy_queue_ib(queue); | |
1321 | return ret; | |
1322 | } | |
1323 | ||
71102307 CH |
1324 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
1325 | struct rdma_cm_event *ev) | |
1326 | { | |
1327 | struct nvme_rdma_queue *queue = cm_id->context; | |
1328 | int cm_error = 0; | |
1329 | ||
1330 | dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", | |
1331 | rdma_event_msg(ev->event), ev->event, | |
1332 | ev->status, cm_id); | |
1333 | ||
1334 | switch (ev->event) { | |
1335 | case RDMA_CM_EVENT_ADDR_RESOLVED: | |
1336 | cm_error = nvme_rdma_addr_resolved(queue); | |
1337 | break; | |
1338 | case RDMA_CM_EVENT_ROUTE_RESOLVED: | |
1339 | cm_error = nvme_rdma_route_resolved(queue); | |
1340 | break; | |
1341 | case RDMA_CM_EVENT_ESTABLISHED: | |
1342 | queue->cm_error = nvme_rdma_conn_established(queue); | |
1343 | /* complete cm_done regardless of success/failure */ | |
1344 | complete(&queue->cm_done); | |
1345 | return 0; | |
1346 | case RDMA_CM_EVENT_REJECTED: | |
1347 | cm_error = nvme_rdma_conn_rejected(queue, ev); | |
1348 | break; | |
1349 | case RDMA_CM_EVENT_ADDR_ERROR: | |
1350 | case RDMA_CM_EVENT_ROUTE_ERROR: | |
1351 | case RDMA_CM_EVENT_CONNECT_ERROR: | |
1352 | case RDMA_CM_EVENT_UNREACHABLE: | |
1353 | dev_dbg(queue->ctrl->ctrl.device, | |
1354 | "CM error event %d\n", ev->event); | |
1355 | cm_error = -ECONNRESET; | |
1356 | break; | |
1357 | case RDMA_CM_EVENT_DISCONNECTED: | |
1358 | case RDMA_CM_EVENT_ADDR_CHANGE: | |
1359 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: | |
1360 | dev_dbg(queue->ctrl->ctrl.device, | |
1361 | "disconnect received - connection closed\n"); | |
1362 | nvme_rdma_error_recovery(queue->ctrl); | |
1363 | break; | |
1364 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
e87a911f SW |
1365 | /* device removal is handled via the ib_client API */ |
1366 | break; | |
71102307 CH |
1367 | default: |
1368 | dev_err(queue->ctrl->ctrl.device, | |
1369 | "Unexpected RDMA CM event (%d)\n", ev->event); | |
1370 | nvme_rdma_error_recovery(queue->ctrl); | |
1371 | break; | |
1372 | } | |
1373 | ||
1374 | if (cm_error) { | |
1375 | queue->cm_error = cm_error; | |
1376 | complete(&queue->cm_done); | |
1377 | } | |
1378 | ||
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | static enum blk_eh_timer_return | |
1383 | nvme_rdma_timeout(struct request *rq, bool reserved) | |
1384 | { | |
1385 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
1386 | ||
1387 | /* queue error recovery */ | |
1388 | nvme_rdma_error_recovery(req->queue->ctrl); | |
1389 | ||
1390 | /* fail with DNR on cmd timeout */ | |
1391 | rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR; | |
1392 | ||
1393 | return BLK_EH_HANDLED; | |
1394 | } | |
1395 | ||
1396 | static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, | |
1397 | const struct blk_mq_queue_data *bd) | |
1398 | { | |
1399 | struct nvme_ns *ns = hctx->queue->queuedata; | |
1400 | struct nvme_rdma_queue *queue = hctx->driver_data; | |
1401 | struct request *rq = bd->rq; | |
1402 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
1403 | struct nvme_rdma_qe *sqe = &req->sqe; | |
1404 | struct nvme_command *c = sqe->data; | |
1405 | bool flush = false; | |
1406 | struct ib_device *dev; | |
1407 | unsigned int map_len; | |
1408 | int ret; | |
1409 | ||
1410 | WARN_ON_ONCE(rq->tag < 0); | |
1411 | ||
1412 | dev = queue->device->dev; | |
1413 | ib_dma_sync_single_for_cpu(dev, sqe->dma, | |
1414 | sizeof(struct nvme_command), DMA_TO_DEVICE); | |
1415 | ||
1416 | ret = nvme_setup_cmd(ns, rq, c); | |
1417 | if (ret) | |
1418 | return ret; | |
1419 | ||
1420 | c->common.command_id = rq->tag; | |
1421 | blk_mq_start_request(rq); | |
1422 | ||
1423 | map_len = nvme_map_len(rq); | |
1424 | ret = nvme_rdma_map_data(queue, rq, map_len, c); | |
1425 | if (ret < 0) { | |
1426 | dev_err(queue->ctrl->ctrl.device, | |
1427 | "Failed to map data (%d)\n", ret); | |
1428 | nvme_cleanup_cmd(rq); | |
1429 | goto err; | |
1430 | } | |
1431 | ||
1432 | ib_dma_sync_single_for_device(dev, sqe->dma, | |
1433 | sizeof(struct nvme_command), DMA_TO_DEVICE); | |
1434 | ||
1435 | if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH) | |
1436 | flush = true; | |
1437 | ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, | |
f5b7b559 | 1438 | req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); |
71102307 CH |
1439 | if (ret) { |
1440 | nvme_rdma_unmap_data(queue, rq); | |
1441 | goto err; | |
1442 | } | |
1443 | ||
1444 | return BLK_MQ_RQ_QUEUE_OK; | |
1445 | err: | |
1446 | return (ret == -ENOMEM || ret == -EAGAIN) ? | |
1447 | BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; | |
1448 | } | |
1449 | ||
1450 | static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) | |
1451 | { | |
1452 | struct nvme_rdma_queue *queue = hctx->driver_data; | |
1453 | struct ib_cq *cq = queue->ib_cq; | |
1454 | struct ib_wc wc; | |
1455 | int found = 0; | |
1456 | ||
1457 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); | |
1458 | while (ib_poll_cq(cq, 1, &wc) > 0) { | |
1459 | struct ib_cqe *cqe = wc.wr_cqe; | |
1460 | ||
1461 | if (cqe) { | |
1462 | if (cqe->done == nvme_rdma_recv_done) | |
1463 | found |= __nvme_rdma_recv_done(cq, &wc, tag); | |
1464 | else | |
1465 | cqe->done(cq, &wc); | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | return found; | |
1470 | } | |
1471 | ||
1472 | static void nvme_rdma_complete_rq(struct request *rq) | |
1473 | { | |
1474 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); | |
1475 | struct nvme_rdma_queue *queue = req->queue; | |
1476 | int error = 0; | |
1477 | ||
1478 | nvme_rdma_unmap_data(queue, rq); | |
1479 | ||
1480 | if (unlikely(rq->errors)) { | |
1481 | if (nvme_req_needs_retry(rq, rq->errors)) { | |
1482 | nvme_requeue_req(rq); | |
1483 | return; | |
1484 | } | |
1485 | ||
1486 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV) | |
1487 | error = rq->errors; | |
1488 | else | |
1489 | error = nvme_error_status(rq->errors); | |
1490 | } | |
1491 | ||
1492 | blk_mq_end_request(rq, error); | |
1493 | } | |
1494 | ||
1495 | static struct blk_mq_ops nvme_rdma_mq_ops = { | |
1496 | .queue_rq = nvme_rdma_queue_rq, | |
1497 | .complete = nvme_rdma_complete_rq, | |
1498 | .map_queue = blk_mq_map_queue, | |
1499 | .init_request = nvme_rdma_init_request, | |
1500 | .exit_request = nvme_rdma_exit_request, | |
1501 | .reinit_request = nvme_rdma_reinit_request, | |
1502 | .init_hctx = nvme_rdma_init_hctx, | |
1503 | .poll = nvme_rdma_poll, | |
1504 | .timeout = nvme_rdma_timeout, | |
1505 | }; | |
1506 | ||
1507 | static struct blk_mq_ops nvme_rdma_admin_mq_ops = { | |
1508 | .queue_rq = nvme_rdma_queue_rq, | |
1509 | .complete = nvme_rdma_complete_rq, | |
1510 | .map_queue = blk_mq_map_queue, | |
1511 | .init_request = nvme_rdma_init_admin_request, | |
1512 | .exit_request = nvme_rdma_exit_admin_request, | |
1513 | .reinit_request = nvme_rdma_reinit_request, | |
1514 | .init_hctx = nvme_rdma_init_admin_hctx, | |
1515 | .timeout = nvme_rdma_timeout, | |
1516 | }; | |
1517 | ||
1518 | static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl) | |
1519 | { | |
1520 | int error; | |
1521 | ||
1522 | error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); | |
1523 | if (error) | |
1524 | return error; | |
1525 | ||
1526 | ctrl->device = ctrl->queues[0].device; | |
1527 | ||
1528 | /* | |
1529 | * We need a reference on the device as long as the tag_set is alive, | |
1530 | * as the MRs in the request structures need a valid ib_device. | |
1531 | */ | |
1532 | error = -EINVAL; | |
1533 | if (!nvme_rdma_dev_get(ctrl->device)) | |
1534 | goto out_free_queue; | |
1535 | ||
1536 | ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS, | |
1537 | ctrl->device->dev->attrs.max_fast_reg_page_list_len); | |
1538 | ||
1539 | memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); | |
1540 | ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops; | |
1541 | ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH; | |
1542 | ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ | |
1543 | ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; | |
1544 | ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) + | |
1545 | SG_CHUNK_SIZE * sizeof(struct scatterlist); | |
1546 | ctrl->admin_tag_set.driver_data = ctrl; | |
1547 | ctrl->admin_tag_set.nr_hw_queues = 1; | |
1548 | ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; | |
1549 | ||
1550 | error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); | |
1551 | if (error) | |
1552 | goto out_put_dev; | |
1553 | ||
1554 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); | |
1555 | if (IS_ERR(ctrl->ctrl.admin_q)) { | |
1556 | error = PTR_ERR(ctrl->ctrl.admin_q); | |
1557 | goto out_free_tagset; | |
1558 | } | |
1559 | ||
1560 | error = nvmf_connect_admin_queue(&ctrl->ctrl); | |
1561 | if (error) | |
1562 | goto out_cleanup_queue; | |
1563 | ||
1564 | error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap); | |
1565 | if (error) { | |
1566 | dev_err(ctrl->ctrl.device, | |
1567 | "prop_get NVME_REG_CAP failed\n"); | |
1568 | goto out_cleanup_queue; | |
1569 | } | |
1570 | ||
1571 | ctrl->ctrl.sqsize = | |
1572 | min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize); | |
1573 | ||
1574 | error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); | |
1575 | if (error) | |
1576 | goto out_cleanup_queue; | |
1577 | ||
1578 | ctrl->ctrl.max_hw_sectors = | |
1579 | (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9); | |
1580 | ||
1581 | error = nvme_init_identify(&ctrl->ctrl); | |
1582 | if (error) | |
1583 | goto out_cleanup_queue; | |
1584 | ||
1585 | error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev, | |
1586 | &ctrl->async_event_sqe, sizeof(struct nvme_command), | |
1587 | DMA_TO_DEVICE); | |
1588 | if (error) | |
1589 | goto out_cleanup_queue; | |
1590 | ||
1591 | nvme_start_keep_alive(&ctrl->ctrl); | |
1592 | ||
1593 | return 0; | |
1594 | ||
1595 | out_cleanup_queue: | |
1596 | blk_cleanup_queue(ctrl->ctrl.admin_q); | |
1597 | out_free_tagset: | |
1598 | /* disconnect and drain the queue before freeing the tagset */ | |
1599 | nvme_rdma_stop_queue(&ctrl->queues[0]); | |
1600 | blk_mq_free_tag_set(&ctrl->admin_tag_set); | |
1601 | out_put_dev: | |
1602 | nvme_rdma_dev_put(ctrl->device); | |
1603 | out_free_queue: | |
1604 | nvme_rdma_free_queue(&ctrl->queues[0]); | |
1605 | return error; | |
1606 | } | |
1607 | ||
1608 | static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl) | |
1609 | { | |
1610 | nvme_stop_keep_alive(&ctrl->ctrl); | |
1611 | cancel_work_sync(&ctrl->err_work); | |
1612 | cancel_delayed_work_sync(&ctrl->reconnect_work); | |
1613 | ||
1614 | if (ctrl->queue_count > 1) { | |
1615 | nvme_stop_queues(&ctrl->ctrl); | |
1616 | blk_mq_tagset_busy_iter(&ctrl->tag_set, | |
1617 | nvme_cancel_request, &ctrl->ctrl); | |
1618 | nvme_rdma_free_io_queues(ctrl); | |
1619 | } | |
1620 | ||
45862ebc | 1621 | if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags)) |
71102307 CH |
1622 | nvme_shutdown_ctrl(&ctrl->ctrl); |
1623 | ||
1624 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); | |
1625 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, | |
1626 | nvme_cancel_request, &ctrl->ctrl); | |
1627 | nvme_rdma_destroy_admin_queue(ctrl); | |
1628 | } | |
1629 | ||
2461a8dd SG |
1630 | static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) |
1631 | { | |
1632 | nvme_uninit_ctrl(&ctrl->ctrl); | |
1633 | if (shutdown) | |
1634 | nvme_rdma_shutdown_ctrl(ctrl); | |
a34ca17a SG |
1635 | |
1636 | if (ctrl->ctrl.tagset) { | |
1637 | blk_cleanup_queue(ctrl->ctrl.connect_q); | |
1638 | blk_mq_free_tag_set(&ctrl->tag_set); | |
1639 | nvme_rdma_dev_put(ctrl->device); | |
1640 | } | |
1641 | ||
2461a8dd SG |
1642 | nvme_put_ctrl(&ctrl->ctrl); |
1643 | } | |
1644 | ||
71102307 CH |
1645 | static void nvme_rdma_del_ctrl_work(struct work_struct *work) |
1646 | { | |
1647 | struct nvme_rdma_ctrl *ctrl = container_of(work, | |
1648 | struct nvme_rdma_ctrl, delete_work); | |
1649 | ||
2461a8dd | 1650 | __nvme_rdma_remove_ctrl(ctrl, true); |
71102307 CH |
1651 | } |
1652 | ||
1653 | static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl) | |
1654 | { | |
1655 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) | |
1656 | return -EBUSY; | |
1657 | ||
1658 | if (!queue_work(nvme_rdma_wq, &ctrl->delete_work)) | |
1659 | return -EBUSY; | |
1660 | ||
1661 | return 0; | |
1662 | } | |
1663 | ||
1664 | static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl) | |
1665 | { | |
1666 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); | |
cdbecc8d | 1667 | int ret = 0; |
71102307 | 1668 | |
cdbecc8d SW |
1669 | /* |
1670 | * Keep a reference until all work is flushed since | |
1671 | * __nvme_rdma_del_ctrl can free the ctrl mem | |
1672 | */ | |
1673 | if (!kref_get_unless_zero(&ctrl->ctrl.kref)) | |
1674 | return -EBUSY; | |
71102307 | 1675 | ret = __nvme_rdma_del_ctrl(ctrl); |
cdbecc8d SW |
1676 | if (!ret) |
1677 | flush_work(&ctrl->delete_work); | |
1678 | nvme_put_ctrl(&ctrl->ctrl); | |
1679 | return ret; | |
71102307 CH |
1680 | } |
1681 | ||
1682 | static void nvme_rdma_remove_ctrl_work(struct work_struct *work) | |
1683 | { | |
1684 | struct nvme_rdma_ctrl *ctrl = container_of(work, | |
1685 | struct nvme_rdma_ctrl, delete_work); | |
1686 | ||
2461a8dd | 1687 | __nvme_rdma_remove_ctrl(ctrl, false); |
71102307 CH |
1688 | } |
1689 | ||
1690 | static void nvme_rdma_reset_ctrl_work(struct work_struct *work) | |
1691 | { | |
1692 | struct nvme_rdma_ctrl *ctrl = container_of(work, | |
1693 | struct nvme_rdma_ctrl, reset_work); | |
1694 | int ret; | |
1695 | bool changed; | |
1696 | ||
1697 | nvme_rdma_shutdown_ctrl(ctrl); | |
1698 | ||
1699 | ret = nvme_rdma_configure_admin_queue(ctrl); | |
1700 | if (ret) { | |
1701 | /* ctrl is already shutdown, just remove the ctrl */ | |
1702 | INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work); | |
1703 | goto del_dead_ctrl; | |
1704 | } | |
1705 | ||
1706 | if (ctrl->queue_count > 1) { | |
1707 | ret = blk_mq_reinit_tagset(&ctrl->tag_set); | |
1708 | if (ret) | |
1709 | goto del_dead_ctrl; | |
1710 | ||
1711 | ret = nvme_rdma_init_io_queues(ctrl); | |
1712 | if (ret) | |
1713 | goto del_dead_ctrl; | |
1714 | ||
1715 | ret = nvme_rdma_connect_io_queues(ctrl); | |
1716 | if (ret) | |
1717 | goto del_dead_ctrl; | |
1718 | } | |
1719 | ||
1720 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); | |
1721 | WARN_ON_ONCE(!changed); | |
1722 | ||
1723 | if (ctrl->queue_count > 1) { | |
1724 | nvme_start_queues(&ctrl->ctrl); | |
1725 | nvme_queue_scan(&ctrl->ctrl); | |
3ef1b4b2 | 1726 | nvme_queue_async_events(&ctrl->ctrl); |
71102307 CH |
1727 | } |
1728 | ||
1729 | return; | |
1730 | ||
1731 | del_dead_ctrl: | |
1732 | /* Deleting this dead controller... */ | |
1733 | dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); | |
1734 | WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work)); | |
1735 | } | |
1736 | ||
1737 | static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl) | |
1738 | { | |
1739 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); | |
1740 | ||
1741 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) | |
1742 | return -EBUSY; | |
1743 | ||
1744 | if (!queue_work(nvme_rdma_wq, &ctrl->reset_work)) | |
1745 | return -EBUSY; | |
1746 | ||
1747 | flush_work(&ctrl->reset_work); | |
1748 | ||
1749 | return 0; | |
1750 | } | |
1751 | ||
1752 | static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { | |
1753 | .name = "rdma", | |
1754 | .module = THIS_MODULE, | |
1755 | .is_fabrics = true, | |
1756 | .reg_read32 = nvmf_reg_read32, | |
1757 | .reg_read64 = nvmf_reg_read64, | |
1758 | .reg_write32 = nvmf_reg_write32, | |
1759 | .reset_ctrl = nvme_rdma_reset_ctrl, | |
1760 | .free_ctrl = nvme_rdma_free_ctrl, | |
1761 | .submit_async_event = nvme_rdma_submit_async_event, | |
1762 | .delete_ctrl = nvme_rdma_del_ctrl, | |
1763 | .get_subsysnqn = nvmf_get_subsysnqn, | |
1764 | .get_address = nvmf_get_address, | |
1765 | }; | |
1766 | ||
1767 | static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl) | |
1768 | { | |
1769 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; | |
1770 | int ret; | |
1771 | ||
1772 | ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues); | |
1773 | if (ret) | |
1774 | return ret; | |
1775 | ||
1776 | ctrl->queue_count = opts->nr_io_queues + 1; | |
1777 | if (ctrl->queue_count < 2) | |
1778 | return 0; | |
1779 | ||
1780 | dev_info(ctrl->ctrl.device, | |
1781 | "creating %d I/O queues.\n", opts->nr_io_queues); | |
1782 | ||
1783 | ret = nvme_rdma_init_io_queues(ctrl); | |
1784 | if (ret) | |
1785 | return ret; | |
1786 | ||
1787 | /* | |
1788 | * We need a reference on the device as long as the tag_set is alive, | |
1789 | * as the MRs in the request structures need a valid ib_device. | |
1790 | */ | |
1791 | ret = -EINVAL; | |
1792 | if (!nvme_rdma_dev_get(ctrl->device)) | |
1793 | goto out_free_io_queues; | |
1794 | ||
1795 | memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); | |
1796 | ctrl->tag_set.ops = &nvme_rdma_mq_ops; | |
c5af8654 | 1797 | ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; |
71102307 CH |
1798 | ctrl->tag_set.reserved_tags = 1; /* fabric connect */ |
1799 | ctrl->tag_set.numa_node = NUMA_NO_NODE; | |
1800 | ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; | |
1801 | ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) + | |
1802 | SG_CHUNK_SIZE * sizeof(struct scatterlist); | |
1803 | ctrl->tag_set.driver_data = ctrl; | |
1804 | ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1; | |
1805 | ctrl->tag_set.timeout = NVME_IO_TIMEOUT; | |
1806 | ||
1807 | ret = blk_mq_alloc_tag_set(&ctrl->tag_set); | |
1808 | if (ret) | |
1809 | goto out_put_dev; | |
1810 | ctrl->ctrl.tagset = &ctrl->tag_set; | |
1811 | ||
1812 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); | |
1813 | if (IS_ERR(ctrl->ctrl.connect_q)) { | |
1814 | ret = PTR_ERR(ctrl->ctrl.connect_q); | |
1815 | goto out_free_tag_set; | |
1816 | } | |
1817 | ||
1818 | ret = nvme_rdma_connect_io_queues(ctrl); | |
1819 | if (ret) | |
1820 | goto out_cleanup_connect_q; | |
1821 | ||
1822 | return 0; | |
1823 | ||
1824 | out_cleanup_connect_q: | |
1825 | blk_cleanup_queue(ctrl->ctrl.connect_q); | |
1826 | out_free_tag_set: | |
1827 | blk_mq_free_tag_set(&ctrl->tag_set); | |
1828 | out_put_dev: | |
1829 | nvme_rdma_dev_put(ctrl->device); | |
1830 | out_free_io_queues: | |
1831 | nvme_rdma_free_io_queues(ctrl); | |
1832 | return ret; | |
1833 | } | |
1834 | ||
1835 | static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p) | |
1836 | { | |
1837 | u8 *addr = (u8 *)&in_addr->sin_addr.s_addr; | |
1838 | size_t buflen = strlen(p); | |
1839 | ||
1840 | /* XXX: handle IPv6 addresses */ | |
1841 | ||
1842 | if (buflen > INET_ADDRSTRLEN) | |
1843 | return -EINVAL; | |
1844 | if (in4_pton(p, buflen, addr, '\0', NULL) == 0) | |
1845 | return -EINVAL; | |
1846 | in_addr->sin_family = AF_INET; | |
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, | |
1851 | struct nvmf_ctrl_options *opts) | |
1852 | { | |
1853 | struct nvme_rdma_ctrl *ctrl; | |
1854 | int ret; | |
1855 | bool changed; | |
1856 | ||
1857 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); | |
1858 | if (!ctrl) | |
1859 | return ERR_PTR(-ENOMEM); | |
1860 | ctrl->ctrl.opts = opts; | |
1861 | INIT_LIST_HEAD(&ctrl->list); | |
1862 | ||
1863 | ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr); | |
1864 | if (ret) { | |
1865 | pr_err("malformed IP address passed: %s\n", opts->traddr); | |
1866 | goto out_free_ctrl; | |
1867 | } | |
1868 | ||
1869 | if (opts->mask & NVMF_OPT_TRSVCID) { | |
1870 | u16 port; | |
1871 | ||
1872 | ret = kstrtou16(opts->trsvcid, 0, &port); | |
1873 | if (ret) | |
1874 | goto out_free_ctrl; | |
1875 | ||
1876 | ctrl->addr_in.sin_port = cpu_to_be16(port); | |
1877 | } else { | |
1878 | ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT); | |
1879 | } | |
1880 | ||
1881 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, | |
1882 | 0 /* no quirks, we're perfect! */); | |
1883 | if (ret) | |
1884 | goto out_free_ctrl; | |
1885 | ||
1886 | ctrl->reconnect_delay = opts->reconnect_delay; | |
1887 | INIT_DELAYED_WORK(&ctrl->reconnect_work, | |
1888 | nvme_rdma_reconnect_ctrl_work); | |
1889 | INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); | |
1890 | INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work); | |
1891 | INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work); | |
1892 | spin_lock_init(&ctrl->lock); | |
1893 | ||
1894 | ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ | |
c5af8654 | 1895 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
71102307 CH |
1896 | ctrl->ctrl.kato = opts->kato; |
1897 | ||
1898 | ret = -ENOMEM; | |
1899 | ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues), | |
1900 | GFP_KERNEL); | |
1901 | if (!ctrl->queues) | |
1902 | goto out_uninit_ctrl; | |
1903 | ||
1904 | ret = nvme_rdma_configure_admin_queue(ctrl); | |
1905 | if (ret) | |
1906 | goto out_kfree_queues; | |
1907 | ||
1908 | /* sanity check icdoff */ | |
1909 | if (ctrl->ctrl.icdoff) { | |
1910 | dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); | |
1911 | goto out_remove_admin_queue; | |
1912 | } | |
1913 | ||
1914 | /* sanity check keyed sgls */ | |
1915 | if (!(ctrl->ctrl.sgls & (1 << 20))) { | |
1916 | dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); | |
1917 | goto out_remove_admin_queue; | |
1918 | } | |
1919 | ||
1920 | if (opts->queue_size > ctrl->ctrl.maxcmd) { | |
1921 | /* warn if maxcmd is lower than queue_size */ | |
1922 | dev_warn(ctrl->ctrl.device, | |
1923 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", | |
1924 | opts->queue_size, ctrl->ctrl.maxcmd); | |
1925 | opts->queue_size = ctrl->ctrl.maxcmd; | |
1926 | } | |
1927 | ||
1928 | if (opts->nr_io_queues) { | |
1929 | ret = nvme_rdma_create_io_queues(ctrl); | |
1930 | if (ret) | |
1931 | goto out_remove_admin_queue; | |
1932 | } | |
1933 | ||
1934 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); | |
1935 | WARN_ON_ONCE(!changed); | |
1936 | ||
1937 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n", | |
1938 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); | |
1939 | ||
1940 | kref_get(&ctrl->ctrl.kref); | |
1941 | ||
1942 | mutex_lock(&nvme_rdma_ctrl_mutex); | |
1943 | list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); | |
1944 | mutex_unlock(&nvme_rdma_ctrl_mutex); | |
1945 | ||
1946 | if (opts->nr_io_queues) { | |
1947 | nvme_queue_scan(&ctrl->ctrl); | |
1948 | nvme_queue_async_events(&ctrl->ctrl); | |
1949 | } | |
1950 | ||
1951 | return &ctrl->ctrl; | |
1952 | ||
1953 | out_remove_admin_queue: | |
1954 | nvme_stop_keep_alive(&ctrl->ctrl); | |
1955 | nvme_rdma_destroy_admin_queue(ctrl); | |
1956 | out_kfree_queues: | |
1957 | kfree(ctrl->queues); | |
1958 | out_uninit_ctrl: | |
1959 | nvme_uninit_ctrl(&ctrl->ctrl); | |
1960 | nvme_put_ctrl(&ctrl->ctrl); | |
1961 | if (ret > 0) | |
1962 | ret = -EIO; | |
1963 | return ERR_PTR(ret); | |
1964 | out_free_ctrl: | |
1965 | kfree(ctrl); | |
1966 | return ERR_PTR(ret); | |
1967 | } | |
1968 | ||
1969 | static struct nvmf_transport_ops nvme_rdma_transport = { | |
1970 | .name = "rdma", | |
1971 | .required_opts = NVMF_OPT_TRADDR, | |
2ac17c28 | 1972 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY, |
71102307 CH |
1973 | .create_ctrl = nvme_rdma_create_ctrl, |
1974 | }; | |
1975 | ||
e87a911f SW |
1976 | static void nvme_rdma_add_one(struct ib_device *ib_device) |
1977 | { | |
1978 | } | |
1979 | ||
1980 | static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) | |
1981 | { | |
1982 | struct nvme_rdma_ctrl *ctrl; | |
1983 | ||
1984 | /* Delete all controllers using this device */ | |
1985 | mutex_lock(&nvme_rdma_ctrl_mutex); | |
1986 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { | |
1987 | if (ctrl->device->dev != ib_device) | |
1988 | continue; | |
1989 | dev_info(ctrl->ctrl.device, | |
1990 | "Removing ctrl: NQN \"%s\", addr %pISp\n", | |
1991 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); | |
1992 | __nvme_rdma_del_ctrl(ctrl); | |
1993 | } | |
1994 | mutex_unlock(&nvme_rdma_ctrl_mutex); | |
1995 | ||
1996 | flush_workqueue(nvme_rdma_wq); | |
1997 | } | |
1998 | ||
1999 | static struct ib_client nvme_rdma_ib_client = { | |
2000 | .name = "nvme_rdma", | |
2001 | .add = nvme_rdma_add_one, | |
2002 | .remove = nvme_rdma_remove_one | |
2003 | }; | |
2004 | ||
71102307 CH |
2005 | static int __init nvme_rdma_init_module(void) |
2006 | { | |
e87a911f SW |
2007 | int ret; |
2008 | ||
71102307 CH |
2009 | nvme_rdma_wq = create_workqueue("nvme_rdma_wq"); |
2010 | if (!nvme_rdma_wq) | |
2011 | return -ENOMEM; | |
2012 | ||
e87a911f SW |
2013 | ret = ib_register_client(&nvme_rdma_ib_client); |
2014 | if (ret) { | |
2015 | destroy_workqueue(nvme_rdma_wq); | |
2016 | return ret; | |
2017 | } | |
2018 | ||
71102307 CH |
2019 | nvmf_register_transport(&nvme_rdma_transport); |
2020 | return 0; | |
2021 | } | |
2022 | ||
2023 | static void __exit nvme_rdma_cleanup_module(void) | |
2024 | { | |
71102307 | 2025 | nvmf_unregister_transport(&nvme_rdma_transport); |
e87a911f | 2026 | ib_unregister_client(&nvme_rdma_ib_client); |
71102307 CH |
2027 | destroy_workqueue(nvme_rdma_wq); |
2028 | } | |
2029 | ||
2030 | module_init(nvme_rdma_init_module); | |
2031 | module_exit(nvme_rdma_cleanup_module); | |
2032 | ||
2033 | MODULE_LICENSE("GPL v2"); |