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