]>
Commit | Line | Data |
---|---|---|
1cfa0a75 OG |
1 | /* |
2 | * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved. | |
3 | * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. | |
3ee07d27 | 4 | * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved. |
1cfa0a75 OG |
5 | * |
6 | * This software is available to you under a choice of one of two | |
7 | * licenses. You may choose to be licensed under the terms of the GNU | |
8 | * General Public License (GPL) Version 2, available from the file | |
9 | * COPYING in the main directory of this source tree, or the | |
10 | * OpenIB.org BSD license below: | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or | |
13 | * without modification, are permitted provided that the following | |
14 | * conditions are met: | |
15 | * | |
16 | * - Redistributions of source code must retain the above | |
17 | * copyright notice, this list of conditions and the following | |
18 | * disclaimer. | |
19 | * | |
20 | * - Redistributions in binary form must reproduce the above | |
21 | * copyright notice, this list of conditions and the following | |
22 | * disclaimer in the documentation and/or other materials | |
23 | * provided with the distribution. | |
24 | * | |
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
29 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
30 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
31 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
32 | * SOFTWARE. | |
1cfa0a75 | 33 | */ |
1cfa0a75 OG |
34 | #include <linux/kernel.h> |
35 | #include <linux/module.h> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
1cfa0a75 | 37 | #include <linux/delay.h> |
1cfa0a75 OG |
38 | |
39 | #include "iscsi_iser.h" | |
40 | ||
41 | #define ISCSI_ISER_MAX_CONN 8 | |
6aabfa76 SG |
42 | #define ISER_MAX_RX_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN) |
43 | #define ISER_MAX_TX_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN) | |
ff3dd52d SG |
44 | #define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \ |
45 | ISCSI_ISER_MAX_CONN) | |
1cfa0a75 | 46 | |
183cfa43 SG |
47 | static int iser_cq_poll_limit = 512; |
48 | ||
1cfa0a75 OG |
49 | static void iser_cq_tasklet_fn(unsigned long data); |
50 | static void iser_cq_callback(struct ib_cq *cq, void *cq_context); | |
1cfa0a75 OG |
51 | |
52 | static void iser_cq_event_callback(struct ib_event *cause, void *context) | |
53 | { | |
54 | iser_err("got cq event %d \n", cause->event); | |
55 | } | |
56 | ||
57 | static void iser_qp_event_callback(struct ib_event *cause, void *context) | |
58 | { | |
59 | iser_err("got qp event %d\n",cause->event); | |
60 | } | |
61 | ||
2110f9bf OG |
62 | static void iser_event_handler(struct ib_event_handler *handler, |
63 | struct ib_event *event) | |
64 | { | |
65 | iser_err("async event %d on device %s port %d\n", event->event, | |
66 | event->device->name, event->element.port_num); | |
67 | } | |
68 | ||
1cfa0a75 OG |
69 | /** |
70 | * iser_create_device_ib_res - creates Protection Domain (PD), Completion | |
71 | * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with | |
72 | * the adapator. | |
73 | * | |
74 | * returns 0 on success, -1 on failure | |
75 | */ | |
76 | static int iser_create_device_ib_res(struct iser_device *device) | |
77 | { | |
65198d6b | 78 | struct ib_device_attr *dev_attr = &device->dev_attr; |
f4641ef7 | 79 | int ret, i, max_cqe; |
5a33a669 | 80 | |
65198d6b SG |
81 | ret = ib_query_device(device->ib_device, dev_attr); |
82 | if (ret) { | |
5587856c | 83 | pr_warn("Query device failed for %s\n", device->ib_device->name); |
65198d6b | 84 | return ret; |
5587856c SG |
85 | } |
86 | ||
87 | /* Assign function handles - based on FMR support */ | |
88 | if (device->ib_device->alloc_fmr && device->ib_device->dealloc_fmr && | |
89 | device->ib_device->map_phys_fmr && device->ib_device->unmap_fmr) { | |
90 | iser_info("FMR supported, using FMR for registration\n"); | |
91 | device->iser_alloc_rdma_reg_res = iser_create_fmr_pool; | |
92 | device->iser_free_rdma_reg_res = iser_free_fmr_pool; | |
93 | device->iser_reg_rdma_mem = iser_reg_rdma_mem_fmr; | |
94 | device->iser_unreg_rdma_mem = iser_unreg_mem_fmr; | |
95 | } else | |
96 | if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) { | |
7306b8fa SG |
97 | iser_info("FastReg supported, using FastReg for registration\n"); |
98 | device->iser_alloc_rdma_reg_res = iser_create_fastreg_pool; | |
99 | device->iser_free_rdma_reg_res = iser_free_fastreg_pool; | |
100 | device->iser_reg_rdma_mem = iser_reg_rdma_mem_fastreg; | |
101 | device->iser_unreg_rdma_mem = iser_unreg_mem_fastreg; | |
5587856c | 102 | } else { |
7306b8fa | 103 | iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n"); |
65198d6b | 104 | return -1; |
5587856c | 105 | } |
b4e155ff | 106 | |
da64bdb2 | 107 | device->comps_used = min_t(int, num_online_cpus(), |
bf175540 | 108 | device->ib_device->num_comp_vectors); |
f4641ef7 | 109 | |
da64bdb2 SG |
110 | device->comps = kcalloc(device->comps_used, sizeof(*device->comps), |
111 | GFP_KERNEL); | |
112 | if (!device->comps) | |
113 | goto comps_err; | |
114 | ||
f4641ef7 MT |
115 | max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe); |
116 | ||
117 | iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n", | |
bf175540 | 118 | device->comps_used, device->ib_device->name, |
f4641ef7 | 119 | device->ib_device->num_comp_vectors, max_cqe); |
5a33a669 | 120 | |
1cfa0a75 OG |
121 | device->pd = ib_alloc_pd(device->ib_device); |
122 | if (IS_ERR(device->pd)) | |
123 | goto pd_err; | |
124 | ||
bf175540 SG |
125 | for (i = 0; i < device->comps_used; i++) { |
126 | struct iser_comp *comp = &device->comps[i]; | |
127 | ||
128 | comp->device = device; | |
6aabfa76 SG |
129 | comp->cq = ib_create_cq(device->ib_device, |
130 | iser_cq_callback, | |
131 | iser_cq_event_callback, | |
132 | (void *)comp, | |
f4641ef7 | 133 | max_cqe, i); |
6aabfa76 SG |
134 | if (IS_ERR(comp->cq)) { |
135 | comp->cq = NULL; | |
5a33a669 | 136 | goto cq_err; |
c33b15f0 | 137 | } |
1cfa0a75 | 138 | |
6aabfa76 | 139 | if (ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP)) |
5a33a669 | 140 | goto cq_err; |
1cfa0a75 | 141 | |
bf175540 SG |
142 | tasklet_init(&comp->tasklet, iser_cq_tasklet_fn, |
143 | (unsigned long)comp); | |
5a33a669 | 144 | } |
1cfa0a75 | 145 | |
d8111028 EZ |
146 | device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE | |
147 | IB_ACCESS_REMOTE_WRITE | | |
148 | IB_ACCESS_REMOTE_READ); | |
1cfa0a75 OG |
149 | if (IS_ERR(device->mr)) |
150 | goto dma_mr_err; | |
151 | ||
2110f9bf OG |
152 | INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device, |
153 | iser_event_handler); | |
154 | if (ib_register_event_handler(&device->event_handler)) | |
155 | goto handler_err; | |
156 | ||
1cfa0a75 OG |
157 | return 0; |
158 | ||
2110f9bf OG |
159 | handler_err: |
160 | ib_dereg_mr(device->mr); | |
1cfa0a75 | 161 | dma_mr_err: |
bf175540 SG |
162 | for (i = 0; i < device->comps_used; i++) |
163 | tasklet_kill(&device->comps[i].tasklet); | |
5a33a669 | 164 | cq_err: |
bf175540 SG |
165 | for (i = 0; i < device->comps_used; i++) { |
166 | struct iser_comp *comp = &device->comps[i]; | |
167 | ||
6aabfa76 SG |
168 | if (comp->cq) |
169 | ib_destroy_cq(comp->cq); | |
5a33a669 | 170 | } |
1cfa0a75 OG |
171 | ib_dealloc_pd(device->pd); |
172 | pd_err: | |
da64bdb2 SG |
173 | kfree(device->comps); |
174 | comps_err: | |
1cfa0a75 OG |
175 | iser_err("failed to allocate an IB resource\n"); |
176 | return -1; | |
177 | } | |
178 | ||
179 | /** | |
38dc732f | 180 | * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR, |
1cfa0a75 OG |
181 | * CQ and PD created with the device associated with the adapator. |
182 | */ | |
183 | static void iser_free_device_ib_res(struct iser_device *device) | |
184 | { | |
5a33a669 | 185 | int i; |
1cfa0a75 OG |
186 | BUG_ON(device->mr == NULL); |
187 | ||
bf175540 SG |
188 | for (i = 0; i < device->comps_used; i++) { |
189 | struct iser_comp *comp = &device->comps[i]; | |
190 | ||
191 | tasklet_kill(&comp->tasklet); | |
6aabfa76 SG |
192 | ib_destroy_cq(comp->cq); |
193 | comp->cq = NULL; | |
5a33a669 AT |
194 | } |
195 | ||
2110f9bf | 196 | (void)ib_unregister_event_handler(&device->event_handler); |
1cfa0a75 | 197 | (void)ib_dereg_mr(device->mr); |
1cfa0a75 OG |
198 | (void)ib_dealloc_pd(device->pd); |
199 | ||
da64bdb2 SG |
200 | kfree(device->comps); |
201 | device->comps = NULL; | |
202 | ||
1cfa0a75 | 203 | device->mr = NULL; |
1cfa0a75 OG |
204 | device->pd = NULL; |
205 | } | |
206 | ||
207 | /** | |
986db0d6 | 208 | * iser_create_fmr_pool - Creates FMR pool and page_vector |
1cfa0a75 | 209 | * |
986db0d6 | 210 | * returns 0 on success, or errno code on failure |
1cfa0a75 | 211 | */ |
a4ee3539 | 212 | int iser_create_fmr_pool(struct ib_conn *ib_conn, unsigned cmds_max) |
1cfa0a75 | 213 | { |
a4ee3539 | 214 | struct iser_device *device = ib_conn->device; |
1cfa0a75 | 215 | struct ib_fmr_pool_param params; |
986db0d6 | 216 | int ret = -ENOMEM; |
bcc60c38 | 217 | |
a4ee3539 | 218 | ib_conn->fmr.page_vec = kmalloc(sizeof(*ib_conn->fmr.page_vec) + |
7306b8fa SG |
219 | (sizeof(u64)*(ISCSI_ISER_SG_TABLESIZE + 1)), |
220 | GFP_KERNEL); | |
a4ee3539 | 221 | if (!ib_conn->fmr.page_vec) |
986db0d6 | 222 | return ret; |
9fda1ac5 | 223 | |
a4ee3539 | 224 | ib_conn->fmr.page_vec->pages = (u64 *)(ib_conn->fmr.page_vec + 1); |
1cfa0a75 | 225 | |
8dfa0876 | 226 | params.page_shift = SHIFT_4K; |
1cfa0a75 OG |
227 | /* when the first/last SG element are not start/end * |
228 | * page aligned, the map whould be of N+1 pages */ | |
229 | params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1; | |
230 | /* make the pool size twice the max number of SCSI commands * | |
231 | * the ML is expected to queue, watermark for unmap at 50% */ | |
b7f04513 SP |
232 | params.pool_size = cmds_max * 2; |
233 | params.dirty_watermark = cmds_max; | |
1cfa0a75 OG |
234 | params.cache = 0; |
235 | params.flush_function = NULL; | |
236 | params.access = (IB_ACCESS_LOCAL_WRITE | | |
237 | IB_ACCESS_REMOTE_WRITE | | |
238 | IB_ACCESS_REMOTE_READ); | |
239 | ||
a4ee3539 SG |
240 | ib_conn->fmr.pool = ib_create_fmr_pool(device->pd, ¶ms); |
241 | if (!IS_ERR(ib_conn->fmr.pool)) | |
986db0d6 SP |
242 | return 0; |
243 | ||
244 | /* no FMR => no need for page_vec */ | |
a4ee3539 SG |
245 | kfree(ib_conn->fmr.page_vec); |
246 | ib_conn->fmr.page_vec = NULL; | |
986db0d6 | 247 | |
a4ee3539 SG |
248 | ret = PTR_ERR(ib_conn->fmr.pool); |
249 | ib_conn->fmr.pool = NULL; | |
986db0d6 SP |
250 | if (ret != -ENOSYS) { |
251 | iser_err("FMR allocation failed, err %d\n", ret); | |
252 | return ret; | |
253 | } else { | |
5525d210 | 254 | iser_warn("FMRs are not supported, using unaligned mode\n"); |
986db0d6 | 255 | return 0; |
1cfa0a75 | 256 | } |
986db0d6 SP |
257 | } |
258 | ||
259 | /** | |
260 | * iser_free_fmr_pool - releases the FMR pool and page vec | |
261 | */ | |
a4ee3539 | 262 | void iser_free_fmr_pool(struct ib_conn *ib_conn) |
986db0d6 SP |
263 | { |
264 | iser_info("freeing conn %p fmr pool %p\n", | |
a4ee3539 | 265 | ib_conn, ib_conn->fmr.pool); |
986db0d6 | 266 | |
a4ee3539 SG |
267 | if (ib_conn->fmr.pool != NULL) |
268 | ib_destroy_fmr_pool(ib_conn->fmr.pool); | |
986db0d6 | 269 | |
a4ee3539 | 270 | ib_conn->fmr.pool = NULL; |
986db0d6 | 271 | |
a4ee3539 SG |
272 | kfree(ib_conn->fmr.page_vec); |
273 | ib_conn->fmr.page_vec = NULL; | |
986db0d6 SP |
274 | } |
275 | ||
4dec2a27 SG |
276 | static int |
277 | iser_alloc_pi_ctx(struct ib_device *ib_device, struct ib_pd *pd, | |
278 | struct fast_reg_descriptor *desc) | |
279 | { | |
280 | struct iser_pi_context *pi_ctx = NULL; | |
281 | struct ib_mr_init_attr mr_init_attr = {.max_reg_descriptors = 2, | |
282 | .flags = IB_MR_SIGNATURE_EN}; | |
283 | int ret = 0; | |
284 | ||
285 | desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL); | |
286 | if (!desc->pi_ctx) | |
287 | return -ENOMEM; | |
288 | ||
289 | pi_ctx = desc->pi_ctx; | |
290 | ||
291 | pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device, | |
292 | ISCSI_ISER_SG_TABLESIZE); | |
293 | if (IS_ERR(pi_ctx->prot_frpl)) { | |
294 | ret = PTR_ERR(pi_ctx->prot_frpl); | |
295 | goto prot_frpl_failure; | |
296 | } | |
297 | ||
298 | pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, | |
299 | ISCSI_ISER_SG_TABLESIZE + 1); | |
300 | if (IS_ERR(pi_ctx->prot_mr)) { | |
301 | ret = PTR_ERR(pi_ctx->prot_mr); | |
302 | goto prot_mr_failure; | |
303 | } | |
304 | desc->reg_indicators |= ISER_PROT_KEY_VALID; | |
305 | ||
306 | pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr); | |
307 | if (IS_ERR(pi_ctx->sig_mr)) { | |
308 | ret = PTR_ERR(pi_ctx->sig_mr); | |
309 | goto sig_mr_failure; | |
310 | } | |
311 | desc->reg_indicators |= ISER_SIG_KEY_VALID; | |
312 | desc->reg_indicators &= ~ISER_FASTREG_PROTECTED; | |
313 | ||
314 | return 0; | |
315 | ||
316 | sig_mr_failure: | |
317 | ib_dereg_mr(desc->pi_ctx->prot_mr); | |
318 | prot_mr_failure: | |
319 | ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl); | |
320 | prot_frpl_failure: | |
321 | kfree(desc->pi_ctx); | |
322 | ||
323 | return ret; | |
324 | } | |
325 | ||
326 | static void | |
327 | iser_free_pi_ctx(struct iser_pi_context *pi_ctx) | |
328 | { | |
329 | ib_free_fast_reg_page_list(pi_ctx->prot_frpl); | |
330 | ib_dereg_mr(pi_ctx->prot_mr); | |
331 | ib_destroy_mr(pi_ctx->sig_mr); | |
332 | kfree(pi_ctx); | |
333 | } | |
334 | ||
310b347c SG |
335 | static int |
336 | iser_create_fastreg_desc(struct ib_device *ib_device, struct ib_pd *pd, | |
6b5a8fb0 | 337 | bool pi_enable, struct fast_reg_descriptor *desc) |
310b347c SG |
338 | { |
339 | int ret; | |
340 | ||
341 | desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device, | |
342 | ISCSI_ISER_SG_TABLESIZE + 1); | |
343 | if (IS_ERR(desc->data_frpl)) { | |
344 | ret = PTR_ERR(desc->data_frpl); | |
345 | iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n", | |
346 | ret); | |
347 | return PTR_ERR(desc->data_frpl); | |
348 | } | |
349 | ||
350 | desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE + 1); | |
351 | if (IS_ERR(desc->data_mr)) { | |
352 | ret = PTR_ERR(desc->data_mr); | |
353 | iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret); | |
354 | goto fast_reg_mr_failure; | |
355 | } | |
6b5a8fb0 AT |
356 | desc->reg_indicators |= ISER_DATA_KEY_VALID; |
357 | ||
358 | if (pi_enable) { | |
4dec2a27 SG |
359 | ret = iser_alloc_pi_ctx(ib_device, pd, desc); |
360 | if (ret) | |
6b5a8fb0 | 361 | goto pi_ctx_alloc_failure; |
6b5a8fb0 | 362 | } |
310b347c SG |
363 | |
364 | return 0; | |
6b5a8fb0 AT |
365 | pi_ctx_alloc_failure: |
366 | ib_dereg_mr(desc->data_mr); | |
310b347c SG |
367 | fast_reg_mr_failure: |
368 | ib_free_fast_reg_page_list(desc->data_frpl); | |
369 | ||
370 | return ret; | |
371 | } | |
372 | ||
5587856c | 373 | /** |
7306b8fa | 374 | * iser_create_fastreg_pool - Creates pool of fast_reg descriptors |
5587856c SG |
375 | * for fast registration work requests. |
376 | * returns 0 on success, or errno code on failure | |
377 | */ | |
a4ee3539 | 378 | int iser_create_fastreg_pool(struct ib_conn *ib_conn, unsigned cmds_max) |
5587856c | 379 | { |
a4ee3539 SG |
380 | struct iser_device *device = ib_conn->device; |
381 | struct fast_reg_descriptor *desc; | |
5587856c SG |
382 | int i, ret; |
383 | ||
a4ee3539 SG |
384 | INIT_LIST_HEAD(&ib_conn->fastreg.pool); |
385 | ib_conn->fastreg.pool_size = 0; | |
5587856c | 386 | for (i = 0; i < cmds_max; i++) { |
6b5a8fb0 | 387 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
5587856c SG |
388 | if (!desc) { |
389 | iser_err("Failed to allocate a new fast_reg descriptor\n"); | |
390 | ret = -ENOMEM; | |
391 | goto err; | |
392 | } | |
393 | ||
6b5a8fb0 | 394 | ret = iser_create_fastreg_desc(device->ib_device, device->pd, |
a4ee3539 | 395 | ib_conn->pi_support, desc); |
310b347c SG |
396 | if (ret) { |
397 | iser_err("Failed to create fastreg descriptor err=%d\n", | |
398 | ret); | |
399 | kfree(desc); | |
400 | goto err; | |
5587856c SG |
401 | } |
402 | ||
a4ee3539 SG |
403 | list_add_tail(&desc->list, &ib_conn->fastreg.pool); |
404 | ib_conn->fastreg.pool_size++; | |
5587856c SG |
405 | } |
406 | ||
407 | return 0; | |
27ae2d1e | 408 | |
5587856c | 409 | err: |
a4ee3539 | 410 | iser_free_fastreg_pool(ib_conn); |
5587856c SG |
411 | return ret; |
412 | } | |
413 | ||
414 | /** | |
7306b8fa | 415 | * iser_free_fastreg_pool - releases the pool of fast_reg descriptors |
5587856c | 416 | */ |
a4ee3539 | 417 | void iser_free_fastreg_pool(struct ib_conn *ib_conn) |
5587856c SG |
418 | { |
419 | struct fast_reg_descriptor *desc, *tmp; | |
420 | int i = 0; | |
421 | ||
a4ee3539 | 422 | if (list_empty(&ib_conn->fastreg.pool)) |
5587856c SG |
423 | return; |
424 | ||
a4ee3539 | 425 | iser_info("freeing conn %p fr pool\n", ib_conn); |
5587856c | 426 | |
a4ee3539 | 427 | list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.pool, list) { |
5587856c SG |
428 | list_del(&desc->list); |
429 | ib_free_fast_reg_page_list(desc->data_frpl); | |
430 | ib_dereg_mr(desc->data_mr); | |
4dec2a27 SG |
431 | if (desc->pi_ctx) |
432 | iser_free_pi_ctx(desc->pi_ctx); | |
5587856c SG |
433 | kfree(desc); |
434 | ++i; | |
435 | } | |
436 | ||
a4ee3539 | 437 | if (i < ib_conn->fastreg.pool_size) |
5587856c | 438 | iser_warn("pool still has %d regions registered\n", |
a4ee3539 | 439 | ib_conn->fastreg.pool_size - i); |
5587856c SG |
440 | } |
441 | ||
986db0d6 SP |
442 | /** |
443 | * iser_create_ib_conn_res - Queue-Pair (QP) | |
444 | * | |
445 | * returns 0 on success, -1 on failure | |
446 | */ | |
a4ee3539 | 447 | static int iser_create_ib_conn_res(struct ib_conn *ib_conn) |
986db0d6 | 448 | { |
f4641ef7 MT |
449 | struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn, |
450 | ib_conn); | |
986db0d6 | 451 | struct iser_device *device; |
f4641ef7 | 452 | struct ib_device_attr *dev_attr; |
986db0d6 SP |
453 | struct ib_qp_init_attr init_attr; |
454 | int ret = -ENOMEM; | |
455 | int index, min_index = 0; | |
456 | ||
a4ee3539 | 457 | BUG_ON(ib_conn->device == NULL); |
986db0d6 | 458 | |
a4ee3539 | 459 | device = ib_conn->device; |
f4641ef7 | 460 | dev_attr = &device->dev_attr; |
1cfa0a75 OG |
461 | |
462 | memset(&init_attr, 0, sizeof init_attr); | |
463 | ||
5a33a669 AT |
464 | mutex_lock(&ig.connlist_mutex); |
465 | /* select the CQ with the minimal number of usages */ | |
bf175540 SG |
466 | for (index = 0; index < device->comps_used; index++) { |
467 | if (device->comps[index].active_qps < | |
468 | device->comps[min_index].active_qps) | |
5a33a669 | 469 | min_index = index; |
bf175540 SG |
470 | } |
471 | ib_conn->comp = &device->comps[min_index]; | |
472 | ib_conn->comp->active_qps++; | |
5a33a669 | 473 | mutex_unlock(&ig.connlist_mutex); |
a4ee3539 | 474 | iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn); |
5a33a669 | 475 | |
1cfa0a75 | 476 | init_attr.event_handler = iser_qp_event_callback; |
a4ee3539 | 477 | init_attr.qp_context = (void *)ib_conn; |
6aabfa76 SG |
478 | init_attr.send_cq = ib_conn->comp->cq; |
479 | init_attr.recv_cq = ib_conn->comp->cq; | |
1cfa0a75 | 480 | init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS; |
f19624aa | 481 | init_attr.cap.max_send_sge = 2; |
bcc60c38 | 482 | init_attr.cap.max_recv_sge = 1; |
1cfa0a75 OG |
483 | init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
484 | init_attr.qp_type = IB_QPT_RC; | |
a4ee3539 | 485 | if (ib_conn->pi_support) { |
ff3dd52d | 486 | init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1; |
6b5a8fb0 | 487 | init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN; |
f4641ef7 MT |
488 | iser_conn->max_cmds = |
489 | ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS); | |
6b5a8fb0 | 490 | } else { |
f4641ef7 MT |
491 | if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) { |
492 | init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1; | |
493 | iser_conn->max_cmds = | |
494 | ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS); | |
495 | } else { | |
496 | init_attr.cap.max_send_wr = dev_attr->max_qp_wr; | |
497 | iser_conn->max_cmds = | |
498 | ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr); | |
499 | iser_dbg("device %s supports max_send_wr %d\n", | |
500 | device->ib_device->name, dev_attr->max_qp_wr); | |
501 | } | |
6b5a8fb0 | 502 | } |
1cfa0a75 | 503 | |
a4ee3539 | 504 | ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr); |
1cfa0a75 | 505 | if (ret) |
9fda1ac5 | 506 | goto out_err; |
1cfa0a75 | 507 | |
a4ee3539 | 508 | ib_conn->qp = ib_conn->cma_id->qp; |
986db0d6 | 509 | iser_info("setting conn %p cma_id %p qp %p\n", |
a4ee3539 SG |
510 | ib_conn, ib_conn->cma_id, |
511 | ib_conn->cma_id->qp); | |
1cfa0a75 OG |
512 | return ret; |
513 | ||
9fda1ac5 | 514 | out_err: |
93acb7bb SG |
515 | mutex_lock(&ig.connlist_mutex); |
516 | ib_conn->comp->active_qps--; | |
517 | mutex_unlock(&ig.connlist_mutex); | |
1cfa0a75 | 518 | iser_err("unable to alloc mem or create resource, err %d\n", ret); |
93acb7bb | 519 | |
1cfa0a75 OG |
520 | return ret; |
521 | } | |
522 | ||
1cfa0a75 OG |
523 | /** |
524 | * based on the resolved device node GUID see if there already allocated | |
525 | * device for this device. If there's no such, create one. | |
526 | */ | |
527 | static | |
528 | struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id) | |
529 | { | |
9a378270 | 530 | struct iser_device *device; |
1cfa0a75 OG |
531 | |
532 | mutex_lock(&ig.device_list_mutex); | |
533 | ||
9a378270 | 534 | list_for_each_entry(device, &ig.device_list, ig_list) |
1cfa0a75 OG |
535 | /* find if there's a match using the node GUID */ |
536 | if (device->ib_device->node_guid == cma_id->device->node_guid) | |
d33ed425 | 537 | goto inc_refcnt; |
9a378270 AR |
538 | |
539 | device = kzalloc(sizeof *device, GFP_KERNEL); | |
540 | if (device == NULL) | |
541 | goto out; | |
542 | ||
543 | /* assign this device to the device */ | |
544 | device->ib_device = cma_id->device; | |
545 | /* init the device and link it into ig device list */ | |
546 | if (iser_create_device_ib_res(device)) { | |
547 | kfree(device); | |
548 | device = NULL; | |
549 | goto out; | |
1cfa0a75 | 550 | } |
9a378270 AR |
551 | list_add(&device->ig_list, &ig.device_list); |
552 | ||
d33ed425 | 553 | inc_refcnt: |
1cfa0a75 | 554 | device->refcount++; |
d33ed425 | 555 | out: |
1cfa0a75 OG |
556 | mutex_unlock(&ig.device_list_mutex); |
557 | return device; | |
558 | } | |
559 | ||
560 | /* if there's no demand for this device, release it */ | |
561 | static void iser_device_try_release(struct iser_device *device) | |
562 | { | |
563 | mutex_lock(&ig.device_list_mutex); | |
564 | device->refcount--; | |
4f363882 | 565 | iser_info("device %p refcount %d\n", device, device->refcount); |
1cfa0a75 OG |
566 | if (!device->refcount) { |
567 | iser_free_device_ib_res(device); | |
568 | list_del(&device->ig_list); | |
569 | kfree(device); | |
570 | } | |
571 | mutex_unlock(&ig.device_list_mutex); | |
572 | } | |
573 | ||
504130c0 AN |
574 | /** |
575 | * Called with state mutex held | |
576 | **/ | |
5716af6e SG |
577 | static int iser_conn_state_comp_exch(struct iser_conn *iser_conn, |
578 | enum iser_conn_state comp, | |
579 | enum iser_conn_state exch) | |
1cfa0a75 OG |
580 | { |
581 | int ret; | |
582 | ||
5716af6e SG |
583 | ret = (iser_conn->state == comp); |
584 | if (ret) | |
585 | iser_conn->state = exch; | |
586 | ||
1cfa0a75 OG |
587 | return ret; |
588 | } | |
589 | ||
b73c3ada AN |
590 | void iser_release_work(struct work_struct *work) |
591 | { | |
5716af6e | 592 | struct iser_conn *iser_conn; |
b73c3ada | 593 | |
5716af6e | 594 | iser_conn = container_of(work, struct iser_conn, release_work); |
b73c3ada | 595 | |
c107a6c0 SG |
596 | /* Wait for conn_stop to complete */ |
597 | wait_for_completion(&iser_conn->stop_completion); | |
598 | /* Wait for IB resouces cleanup to complete */ | |
599 | wait_for_completion(&iser_conn->ib_completion); | |
b73c3ada | 600 | |
5716af6e SG |
601 | mutex_lock(&iser_conn->state_mutex); |
602 | iser_conn->state = ISER_CONN_DOWN; | |
603 | mutex_unlock(&iser_conn->state_mutex); | |
504130c0 | 604 | |
5716af6e | 605 | iser_conn_release(iser_conn); |
b73c3ada AN |
606 | } |
607 | ||
96f15198 SG |
608 | /** |
609 | * iser_free_ib_conn_res - release IB related resources | |
610 | * @iser_conn: iser connection struct | |
6606e6a2 SG |
611 | * @destroy: indicator if we need to try to release the |
612 | * iser device and memory regoins pool (only iscsi | |
613 | * shutdown and DEVICE_REMOVAL will use this). | |
96f15198 SG |
614 | * |
615 | * This routine is called with the iser state mutex held | |
616 | * so the cm_id removal is out of here. It is Safe to | |
617 | * be invoked multiple times. | |
618 | */ | |
c47a3c9e | 619 | static void iser_free_ib_conn_res(struct iser_conn *iser_conn, |
6606e6a2 | 620 | bool destroy) |
96f15198 SG |
621 | { |
622 | struct ib_conn *ib_conn = &iser_conn->ib_conn; | |
623 | struct iser_device *device = ib_conn->device; | |
624 | ||
625 | iser_info("freeing conn %p cma_id %p qp %p\n", | |
626 | iser_conn, ib_conn->cma_id, ib_conn->qp); | |
627 | ||
96f15198 | 628 | if (ib_conn->qp != NULL) { |
bf175540 | 629 | ib_conn->comp->active_qps--; |
96f15198 SG |
630 | rdma_destroy_qp(ib_conn->cma_id); |
631 | ib_conn->qp = NULL; | |
632 | } | |
633 | ||
6606e6a2 SG |
634 | if (destroy) { |
635 | if (iser_conn->rx_descs) | |
636 | iser_free_rx_descriptors(iser_conn); | |
637 | ||
638 | if (device != NULL) { | |
639 | iser_device_try_release(device); | |
640 | ib_conn->device = NULL; | |
641 | } | |
96f15198 SG |
642 | } |
643 | } | |
644 | ||
41179e2d RD |
645 | /** |
646 | * Frees all conn objects and deallocs conn descriptor | |
647 | */ | |
5716af6e | 648 | void iser_conn_release(struct iser_conn *iser_conn) |
41179e2d | 649 | { |
a4ee3539 | 650 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
41179e2d | 651 | |
41179e2d | 652 | mutex_lock(&ig.connlist_mutex); |
5716af6e | 653 | list_del(&iser_conn->conn_list); |
41179e2d | 654 | mutex_unlock(&ig.connlist_mutex); |
504130c0 | 655 | |
5716af6e | 656 | mutex_lock(&iser_conn->state_mutex); |
9a3119e4 | 657 | /* In case we endup here without ep_disconnect being invoked. */ |
5426b171 | 658 | if (iser_conn->state != ISER_CONN_DOWN) { |
aea8f4df AN |
659 | iser_warn("iser conn %p state %d, expected state down.\n", |
660 | iser_conn, iser_conn->state); | |
9a3119e4 | 661 | iscsi_destroy_endpoint(iser_conn->ep); |
5426b171 AN |
662 | iser_conn->state = ISER_CONN_DOWN; |
663 | } | |
c47a3c9e SG |
664 | /* |
665 | * In case we never got to bind stage, we still need to | |
666 | * release IB resources (which is safe to call more than once). | |
667 | */ | |
668 | iser_free_ib_conn_res(iser_conn, true); | |
5716af6e | 669 | mutex_unlock(&iser_conn->state_mutex); |
504130c0 | 670 | |
a4ee3539 SG |
671 | if (ib_conn->cma_id != NULL) { |
672 | rdma_destroy_id(ib_conn->cma_id); | |
673 | ib_conn->cma_id = NULL; | |
5b61ff43 | 674 | } |
a4ee3539 | 675 | |
5716af6e | 676 | kfree(iser_conn); |
41179e2d RD |
677 | } |
678 | ||
1cfa0a75 OG |
679 | /** |
680 | * triggers start of the disconnect procedures and wait for them to be done | |
c47a3c9e | 681 | * Called with state mutex held |
1cfa0a75 | 682 | */ |
c47a3c9e | 683 | int iser_conn_terminate(struct iser_conn *iser_conn) |
1cfa0a75 | 684 | { |
a4ee3539 | 685 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
ff3dd52d | 686 | struct ib_send_wr *bad_wr; |
1cfa0a75 OG |
687 | int err = 0; |
688 | ||
c47a3c9e SG |
689 | /* terminate the iser conn only if the conn state is UP */ |
690 | if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP, | |
691 | ISER_CONN_TERMINATING)) | |
692 | return 0; | |
693 | ||
694 | iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state); | |
695 | ||
696 | /* suspend queuing of new iscsi commands */ | |
697 | if (iser_conn->iscsi_conn) | |
698 | iscsi_suspend_queue(iser_conn->iscsi_conn); | |
699 | ||
700 | /* | |
701 | * In case we didn't already clean up the cma_id (peer initiated | |
702 | * a disconnection), we need to Cause the CMA to change the QP | |
703 | * state to ERROR. | |
1cfa0a75 | 704 | */ |
c47a3c9e SG |
705 | if (ib_conn->cma_id) { |
706 | err = rdma_disconnect(ib_conn->cma_id); | |
707 | if (err) | |
708 | iser_err("Failed to disconnect, conn: 0x%p err %d\n", | |
709 | iser_conn, err); | |
710 | ||
ff3dd52d SG |
711 | /* post an indication that all flush errors were consumed */ |
712 | err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr); | |
16df2a26 | 713 | if (err) { |
ff3dd52d | 714 | iser_err("conn %p failed to post beacon", ib_conn); |
16df2a26 SG |
715 | return 1; |
716 | } | |
ff3dd52d | 717 | |
6aabfa76 | 718 | wait_for_completion(&ib_conn->flush_comp); |
c47a3c9e | 719 | } |
1cfa0a75 | 720 | |
c47a3c9e | 721 | return 1; |
1cfa0a75 OG |
722 | } |
723 | ||
504130c0 AN |
724 | /** |
725 | * Called with state mutex held | |
726 | **/ | |
b73c3ada | 727 | static void iser_connect_error(struct rdma_cm_id *cma_id) |
1cfa0a75 | 728 | { |
5716af6e | 729 | struct iser_conn *iser_conn; |
b73c3ada | 730 | |
5716af6e | 731 | iser_conn = (struct iser_conn *)cma_id->context; |
c4de4663 | 732 | iser_conn->state = ISER_CONN_TERMINATING; |
1cfa0a75 OG |
733 | } |
734 | ||
504130c0 AN |
735 | /** |
736 | * Called with state mutex held | |
737 | **/ | |
b73c3ada | 738 | static void iser_addr_handler(struct rdma_cm_id *cma_id) |
1cfa0a75 OG |
739 | { |
740 | struct iser_device *device; | |
5716af6e | 741 | struct iser_conn *iser_conn; |
a4ee3539 | 742 | struct ib_conn *ib_conn; |
1cfa0a75 OG |
743 | int ret; |
744 | ||
5716af6e SG |
745 | iser_conn = (struct iser_conn *)cma_id->context; |
746 | if (iser_conn->state != ISER_CONN_PENDING) | |
504130c0 AN |
747 | /* bailout */ |
748 | return; | |
749 | ||
a4ee3539 | 750 | ib_conn = &iser_conn->ib_conn; |
1cfa0a75 | 751 | device = iser_device_find_by_ib_device(cma_id); |
d33ed425 AR |
752 | if (!device) { |
753 | iser_err("device lookup/creation failed\n"); | |
b73c3ada AN |
754 | iser_connect_error(cma_id); |
755 | return; | |
d33ed425 AR |
756 | } |
757 | ||
a4ee3539 | 758 | ib_conn->device = device; |
1cfa0a75 | 759 | |
7f733847 AT |
760 | /* connection T10-PI support */ |
761 | if (iser_pi_enable) { | |
762 | if (!(device->dev_attr.device_cap_flags & | |
763 | IB_DEVICE_SIGNATURE_HANDOVER)) { | |
764 | iser_warn("T10-PI requested but not supported on %s, " | |
765 | "continue without T10-PI\n", | |
a4ee3539 SG |
766 | ib_conn->device->ib_device->name); |
767 | ib_conn->pi_support = false; | |
7f733847 | 768 | } else { |
a4ee3539 | 769 | ib_conn->pi_support = true; |
7f733847 AT |
770 | } |
771 | } | |
772 | ||
1cfa0a75 OG |
773 | ret = rdma_resolve_route(cma_id, 1000); |
774 | if (ret) { | |
775 | iser_err("resolve route failed: %d\n", ret); | |
b73c3ada AN |
776 | iser_connect_error(cma_id); |
777 | return; | |
1cfa0a75 | 778 | } |
1cfa0a75 OG |
779 | } |
780 | ||
504130c0 AN |
781 | /** |
782 | * Called with state mutex held | |
783 | **/ | |
b73c3ada | 784 | static void iser_route_handler(struct rdma_cm_id *cma_id) |
1cfa0a75 OG |
785 | { |
786 | struct rdma_conn_param conn_param; | |
787 | int ret; | |
8d8399de | 788 | struct iser_cm_hdr req_hdr; |
5716af6e | 789 | struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; |
a4ee3539 SG |
790 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
791 | struct iser_device *device = ib_conn->device; | |
1cfa0a75 | 792 | |
5716af6e | 793 | if (iser_conn->state != ISER_CONN_PENDING) |
504130c0 AN |
794 | /* bailout */ |
795 | return; | |
796 | ||
a4ee3539 | 797 | ret = iser_create_ib_conn_res(ib_conn); |
1cfa0a75 OG |
798 | if (ret) |
799 | goto failure; | |
800 | ||
1cfa0a75 | 801 | memset(&conn_param, 0, sizeof conn_param); |
2ea32938 | 802 | conn_param.responder_resources = device->dev_attr.max_qp_rd_atom; |
1cfa0a75 OG |
803 | conn_param.initiator_depth = 1; |
804 | conn_param.retry_count = 7; | |
805 | conn_param.rnr_retry_count = 6; | |
806 | ||
8d8399de OG |
807 | memset(&req_hdr, 0, sizeof(req_hdr)); |
808 | req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED | | |
809 | ISER_SEND_W_INV_NOT_SUPPORTED); | |
810 | conn_param.private_data = (void *)&req_hdr; | |
811 | conn_param.private_data_len = sizeof(struct iser_cm_hdr); | |
812 | ||
1cfa0a75 OG |
813 | ret = rdma_connect(cma_id, &conn_param); |
814 | if (ret) { | |
815 | iser_err("failure connecting: %d\n", ret); | |
816 | goto failure; | |
817 | } | |
818 | ||
b73c3ada | 819 | return; |
1cfa0a75 | 820 | failure: |
b73c3ada | 821 | iser_connect_error(cma_id); |
1cfa0a75 OG |
822 | } |
823 | ||
824 | static void iser_connected_handler(struct rdma_cm_id *cma_id) | |
825 | { | |
5716af6e | 826 | struct iser_conn *iser_conn; |
4f9208ad OG |
827 | struct ib_qp_attr attr; |
828 | struct ib_qp_init_attr init_attr; | |
829 | ||
5716af6e SG |
830 | iser_conn = (struct iser_conn *)cma_id->context; |
831 | if (iser_conn->state != ISER_CONN_PENDING) | |
504130c0 AN |
832 | /* bailout */ |
833 | return; | |
834 | ||
4f9208ad OG |
835 | (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr); |
836 | iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num); | |
1cfa0a75 | 837 | |
5716af6e SG |
838 | iser_conn->state = ISER_CONN_UP; |
839 | complete(&iser_conn->up_completion); | |
1cfa0a75 OG |
840 | } |
841 | ||
b73c3ada | 842 | static void iser_disconnected_handler(struct rdma_cm_id *cma_id) |
1cfa0a75 | 843 | { |
c47a3c9e | 844 | struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; |
1cfa0a75 | 845 | |
c47a3c9e | 846 | if (iser_conn_terminate(iser_conn)) { |
5716af6e | 847 | if (iser_conn->iscsi_conn) |
c47a3c9e SG |
848 | iscsi_conn_failure(iser_conn->iscsi_conn, |
849 | ISCSI_ERR_CONN_FAILED); | |
7d9eacf9 RD |
850 | else |
851 | iser_err("iscsi_iser connection isn't bound\n"); | |
852 | } | |
c47a3c9e SG |
853 | } |
854 | ||
855 | static void iser_cleanup_handler(struct rdma_cm_id *cma_id, | |
6606e6a2 | 856 | bool destroy) |
c47a3c9e SG |
857 | { |
858 | struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; | |
1cfa0a75 | 859 | |
c47a3c9e SG |
860 | /* |
861 | * We are not guaranteed that we visited disconnected_handler | |
862 | * by now, call it here to be safe that we handle CM drep | |
863 | * and flush errors. | |
8d4aca7f | 864 | */ |
c47a3c9e | 865 | iser_disconnected_handler(cma_id); |
6606e6a2 | 866 | iser_free_ib_conn_res(iser_conn, destroy); |
c47a3c9e SG |
867 | complete(&iser_conn->ib_completion); |
868 | }; | |
1cfa0a75 OG |
869 | |
870 | static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) | |
871 | { | |
5716af6e | 872 | struct iser_conn *iser_conn; |
c47a3c9e | 873 | int ret = 0; |
504130c0 | 874 | |
5716af6e | 875 | iser_conn = (struct iser_conn *)cma_id->context; |
4f363882 RD |
876 | iser_info("event %d status %d conn %p id %p\n", |
877 | event->event, event->status, cma_id->context, cma_id); | |
1cfa0a75 | 878 | |
5716af6e | 879 | mutex_lock(&iser_conn->state_mutex); |
1cfa0a75 OG |
880 | switch (event->event) { |
881 | case RDMA_CM_EVENT_ADDR_RESOLVED: | |
b73c3ada | 882 | iser_addr_handler(cma_id); |
1cfa0a75 OG |
883 | break; |
884 | case RDMA_CM_EVENT_ROUTE_RESOLVED: | |
b73c3ada | 885 | iser_route_handler(cma_id); |
1cfa0a75 OG |
886 | break; |
887 | case RDMA_CM_EVENT_ESTABLISHED: | |
888 | iser_connected_handler(cma_id); | |
889 | break; | |
890 | case RDMA_CM_EVENT_ADDR_ERROR: | |
891 | case RDMA_CM_EVENT_ROUTE_ERROR: | |
892 | case RDMA_CM_EVENT_CONNECT_ERROR: | |
893 | case RDMA_CM_EVENT_UNREACHABLE: | |
894 | case RDMA_CM_EVENT_REJECTED: | |
b73c3ada | 895 | iser_connect_error(cma_id); |
1cfa0a75 OG |
896 | break; |
897 | case RDMA_CM_EVENT_DISCONNECTED: | |
2f5de151 | 898 | case RDMA_CM_EVENT_ADDR_CHANGE: |
5426b171 AN |
899 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
900 | iser_cleanup_handler(cma_id, false); | |
1cfa0a75 | 901 | break; |
c47a3c9e SG |
902 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
903 | /* | |
904 | * we *must* destroy the device as we cannot rely | |
905 | * on iscsid to be around to initiate error handling. | |
5426b171 AN |
906 | * also if we are not in state DOWN implicitly destroy |
907 | * the cma_id. | |
c47a3c9e SG |
908 | */ |
909 | iser_cleanup_handler(cma_id, true); | |
5426b171 AN |
910 | if (iser_conn->state != ISER_CONN_DOWN) { |
911 | iser_conn->ib_conn.cma_id = NULL; | |
912 | ret = 1; | |
913 | } | |
c47a3c9e | 914 | break; |
1cfa0a75 | 915 | default: |
a4ef1451 | 916 | iser_err("Unexpected RDMA CM event (%d)\n", event->event); |
1cfa0a75 OG |
917 | break; |
918 | } | |
5716af6e | 919 | mutex_unlock(&iser_conn->state_mutex); |
c47a3c9e SG |
920 | |
921 | return ret; | |
1cfa0a75 OG |
922 | } |
923 | ||
5716af6e | 924 | void iser_conn_init(struct iser_conn *iser_conn) |
1cfa0a75 | 925 | { |
5716af6e | 926 | iser_conn->state = ISER_CONN_INIT; |
a4ee3539 | 927 | iser_conn->ib_conn.post_recv_buf_count = 0; |
6aabfa76 | 928 | init_completion(&iser_conn->ib_conn.flush_comp); |
5716af6e | 929 | init_completion(&iser_conn->stop_completion); |
c47a3c9e | 930 | init_completion(&iser_conn->ib_completion); |
5716af6e SG |
931 | init_completion(&iser_conn->up_completion); |
932 | INIT_LIST_HEAD(&iser_conn->conn_list); | |
a4ee3539 | 933 | spin_lock_init(&iser_conn->ib_conn.lock); |
5716af6e | 934 | mutex_init(&iser_conn->state_mutex); |
1cfa0a75 OG |
935 | } |
936 | ||
937 | /** | |
938 | * starts the process of connecting to the target | |
94e2bd68 | 939 | * sleeps until the connection is established or rejected |
1cfa0a75 | 940 | */ |
5716af6e | 941 | int iser_connect(struct iser_conn *iser_conn, |
96ed02d4 RD |
942 | struct sockaddr *src_addr, |
943 | struct sockaddr *dst_addr, | |
1cfa0a75 OG |
944 | int non_blocking) |
945 | { | |
a4ee3539 | 946 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
1cfa0a75 OG |
947 | int err = 0; |
948 | ||
5716af6e | 949 | mutex_lock(&iser_conn->state_mutex); |
504130c0 | 950 | |
5716af6e | 951 | sprintf(iser_conn->name, "%pISp", dst_addr); |
96ed02d4 | 952 | |
5716af6e | 953 | iser_info("connecting to: %s\n", iser_conn->name); |
1cfa0a75 OG |
954 | |
955 | /* the device is known only --after-- address resolution */ | |
a4ee3539 | 956 | ib_conn->device = NULL; |
1cfa0a75 | 957 | |
5716af6e | 958 | iser_conn->state = ISER_CONN_PENDING; |
1cfa0a75 | 959 | |
ff3dd52d SG |
960 | ib_conn->beacon.wr_id = ISER_BEACON_WRID; |
961 | ib_conn->beacon.opcode = IB_WR_SEND; | |
962 | ||
a4ee3539 SG |
963 | ib_conn->cma_id = rdma_create_id(iser_cma_handler, |
964 | (void *)iser_conn, | |
965 | RDMA_PS_TCP, IB_QPT_RC); | |
966 | if (IS_ERR(ib_conn->cma_id)) { | |
967 | err = PTR_ERR(ib_conn->cma_id); | |
1cfa0a75 OG |
968 | iser_err("rdma_create_id failed: %d\n", err); |
969 | goto id_failure; | |
970 | } | |
971 | ||
a4ee3539 | 972 | err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000); |
1cfa0a75 OG |
973 | if (err) { |
974 | iser_err("rdma_resolve_addr failed: %d\n", err); | |
975 | goto addr_failure; | |
976 | } | |
977 | ||
978 | if (!non_blocking) { | |
5716af6e | 979 | wait_for_completion_interruptible(&iser_conn->up_completion); |
1cfa0a75 | 980 | |
5716af6e | 981 | if (iser_conn->state != ISER_CONN_UP) { |
1cfa0a75 OG |
982 | err = -EIO; |
983 | goto connect_failure; | |
984 | } | |
985 | } | |
5716af6e | 986 | mutex_unlock(&iser_conn->state_mutex); |
1cfa0a75 OG |
987 | |
988 | mutex_lock(&ig.connlist_mutex); | |
5716af6e | 989 | list_add(&iser_conn->conn_list, &ig.connlist); |
1cfa0a75 OG |
990 | mutex_unlock(&ig.connlist_mutex); |
991 | return 0; | |
992 | ||
993 | id_failure: | |
a4ee3539 | 994 | ib_conn->cma_id = NULL; |
1cfa0a75 | 995 | addr_failure: |
5716af6e | 996 | iser_conn->state = ISER_CONN_DOWN; |
1cfa0a75 | 997 | connect_failure: |
5716af6e SG |
998 | mutex_unlock(&iser_conn->state_mutex); |
999 | iser_conn_release(iser_conn); | |
1cfa0a75 OG |
1000 | return err; |
1001 | } | |
1002 | ||
5716af6e | 1003 | int iser_post_recvl(struct iser_conn *iser_conn) |
bcc60c38 OG |
1004 | { |
1005 | struct ib_recv_wr rx_wr, *rx_wr_failed; | |
a4ee3539 | 1006 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
bcc60c38 OG |
1007 | struct ib_sge sge; |
1008 | int ib_ret; | |
1009 | ||
5716af6e | 1010 | sge.addr = iser_conn->login_resp_dma; |
bcc60c38 | 1011 | sge.length = ISER_RX_LOGIN_SIZE; |
a4ee3539 | 1012 | sge.lkey = ib_conn->device->mr->lkey; |
bcc60c38 | 1013 | |
49df2781 | 1014 | rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf; |
bcc60c38 OG |
1015 | rx_wr.sg_list = &sge; |
1016 | rx_wr.num_sge = 1; | |
1017 | rx_wr.next = NULL; | |
1018 | ||
a4ee3539 SG |
1019 | ib_conn->post_recv_buf_count++; |
1020 | ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed); | |
bcc60c38 OG |
1021 | if (ib_ret) { |
1022 | iser_err("ib_post_recv failed ret=%d\n", ib_ret); | |
a4ee3539 | 1023 | ib_conn->post_recv_buf_count--; |
bcc60c38 OG |
1024 | } |
1025 | return ib_ret; | |
1026 | } | |
1027 | ||
5716af6e | 1028 | int iser_post_recvm(struct iser_conn *iser_conn, int count) |
bcc60c38 OG |
1029 | { |
1030 | struct ib_recv_wr *rx_wr, *rx_wr_failed; | |
1031 | int i, ib_ret; | |
a4ee3539 | 1032 | struct ib_conn *ib_conn = &iser_conn->ib_conn; |
5716af6e | 1033 | unsigned int my_rx_head = iser_conn->rx_desc_head; |
bcc60c38 OG |
1034 | struct iser_rx_desc *rx_desc; |
1035 | ||
a4ee3539 | 1036 | for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) { |
5716af6e | 1037 | rx_desc = &iser_conn->rx_descs[my_rx_head]; |
49df2781 | 1038 | rx_wr->wr_id = (uintptr_t)rx_desc; |
bcc60c38 OG |
1039 | rx_wr->sg_list = &rx_desc->rx_sg; |
1040 | rx_wr->num_sge = 1; | |
1041 | rx_wr->next = rx_wr + 1; | |
5716af6e | 1042 | my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask; |
bcc60c38 OG |
1043 | } |
1044 | ||
1045 | rx_wr--; | |
1046 | rx_wr->next = NULL; /* mark end of work requests list */ | |
1047 | ||
a4ee3539 SG |
1048 | ib_conn->post_recv_buf_count += count; |
1049 | ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed); | |
bcc60c38 OG |
1050 | if (ib_ret) { |
1051 | iser_err("ib_post_recv failed ret=%d\n", ib_ret); | |
a4ee3539 | 1052 | ib_conn->post_recv_buf_count -= count; |
bcc60c38 | 1053 | } else |
5716af6e | 1054 | iser_conn->rx_desc_head = my_rx_head; |
bcc60c38 OG |
1055 | return ib_ret; |
1056 | } | |
1057 | ||
1058 | ||
1cfa0a75 OG |
1059 | /** |
1060 | * iser_start_send - Initiate a Send DTO operation | |
1061 | * | |
1062 | * returns 0 on success, -1 on failure | |
1063 | */ | |
6df5a128 SG |
1064 | int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc, |
1065 | bool signal) | |
1cfa0a75 | 1066 | { |
f19624aa | 1067 | int ib_ret; |
1cfa0a75 | 1068 | struct ib_send_wr send_wr, *send_wr_failed; |
1cfa0a75 | 1069 | |
a4ee3539 | 1070 | ib_dma_sync_single_for_device(ib_conn->device->ib_device, |
5716af6e SG |
1071 | tx_desc->dma_addr, ISER_HEADERS_LEN, |
1072 | DMA_TO_DEVICE); | |
1cfa0a75 OG |
1073 | |
1074 | send_wr.next = NULL; | |
49df2781 | 1075 | send_wr.wr_id = (uintptr_t)tx_desc; |
f19624aa OG |
1076 | send_wr.sg_list = tx_desc->tx_sg; |
1077 | send_wr.num_sge = tx_desc->num_sge; | |
1cfa0a75 | 1078 | send_wr.opcode = IB_WR_SEND; |
6df5a128 | 1079 | send_wr.send_flags = signal ? IB_SEND_SIGNALED : 0; |
1cfa0a75 | 1080 | |
a4ee3539 | 1081 | ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed); |
ff3dd52d | 1082 | if (ib_ret) |
1cfa0a75 | 1083 | iser_err("ib_post_send failed, ret:%d\n", ib_ret); |
ff3dd52d | 1084 | |
f19624aa | 1085 | return ib_ret; |
1cfa0a75 OG |
1086 | } |
1087 | ||
6aabfa76 SG |
1088 | /** |
1089 | * is_iser_tx_desc - Indicate if the completion wr_id | |
1090 | * is a TX descriptor or not. | |
1091 | * @iser_conn: iser connection | |
1092 | * @wr_id: completion WR identifier | |
1093 | * | |
1094 | * Since we cannot rely on wc opcode in FLUSH errors | |
1095 | * we must work around it by checking if the wr_id address | |
1096 | * falls in the iser connection rx_descs buffer. If so | |
1097 | * it is an RX descriptor, otherwize it is a TX. | |
1098 | */ | |
1099 | static inline bool | |
1100 | is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id) | |
1101 | { | |
1102 | void *start = iser_conn->rx_descs; | |
1103 | int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs); | |
1104 | ||
1105 | if (wr_id >= start && wr_id < start + len) | |
1106 | return false; | |
1107 | ||
1108 | return true; | |
1109 | } | |
1110 | ||
8c204e69 SG |
1111 | /** |
1112 | * iser_handle_comp_error() - Handle error completion | |
8c204e69 SG |
1113 | * @ib_conn: connection RDMA resources |
1114 | * @wc: work completion | |
1115 | * | |
1116 | * Notes: We may handle a FLUSH error completion and in this case | |
1117 | * we only cleanup in case TX type was DATAOUT. For non-FLUSH | |
1118 | * error completion we should also notify iscsi layer that | |
1119 | * connection is failed (in case we passed bind stage). | |
1120 | */ | |
1121 | static void | |
6aabfa76 | 1122 | iser_handle_comp_error(struct ib_conn *ib_conn, |
8c204e69 | 1123 | struct ib_wc *wc) |
1cfa0a75 | 1124 | { |
49df2781 | 1125 | void *wr_id = (void *)(uintptr_t)wc->wr_id; |
8c204e69 SG |
1126 | struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn, |
1127 | ib_conn); | |
1128 | ||
1129 | if (wc->status != IB_WC_WR_FLUSH_ERR) | |
1130 | if (iser_conn->iscsi_conn) | |
1131 | iscsi_conn_failure(iser_conn->iscsi_conn, | |
1132 | ISCSI_ERR_CONN_FAILED); | |
1133 | ||
30bf1d58 SG |
1134 | if (wc->wr_id == ISER_FASTREG_LI_WRID) |
1135 | return; | |
1136 | ||
49df2781 SG |
1137 | if (is_iser_tx_desc(iser_conn, wr_id)) { |
1138 | struct iser_tx_desc *desc = wr_id; | |
6aabfa76 | 1139 | |
6aabfa76 SG |
1140 | if (desc->type == ISCSI_TX_DATAOUT) |
1141 | kmem_cache_free(ig.desc_cache, desc); | |
1142 | } else { | |
1143 | ib_conn->post_recv_buf_count--; | |
1144 | } | |
1cfa0a75 OG |
1145 | } |
1146 | ||
6aabfa76 SG |
1147 | /** |
1148 | * iser_handle_wc - handle a single work completion | |
1149 | * @wc: work completion | |
1150 | * | |
1151 | * Soft-IRQ context, work completion can be either | |
1152 | * SEND or RECV, and can turn out successful or | |
1153 | * with error (or flush error). | |
1154 | */ | |
1155 | static void iser_handle_wc(struct ib_wc *wc) | |
78ad0a34 | 1156 | { |
a4ee3539 | 1157 | struct ib_conn *ib_conn; |
6aabfa76 SG |
1158 | struct iser_tx_desc *tx_desc; |
1159 | struct iser_rx_desc *rx_desc; | |
78ad0a34 | 1160 | |
6aabfa76 | 1161 | ib_conn = wc->qp->qp_context; |
06c7fb67 | 1162 | if (likely(wc->status == IB_WC_SUCCESS)) { |
6aabfa76 | 1163 | if (wc->opcode == IB_WC_RECV) { |
49df2781 | 1164 | rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id; |
6aabfa76 SG |
1165 | iser_rcv_completion(rx_desc, wc->byte_len, |
1166 | ib_conn); | |
1167 | } else | |
1168 | if (wc->opcode == IB_WC_SEND) { | |
49df2781 | 1169 | tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id; |
6aabfa76 | 1170 | iser_snd_completion(tx_desc, ib_conn); |
78ad0a34 | 1171 | } else { |
6aabfa76 | 1172 | iser_err("Unknown wc opcode %d\n", wc->opcode); |
78ad0a34 | 1173 | } |
6aabfa76 SG |
1174 | } else { |
1175 | if (wc->status != IB_WC_WR_FLUSH_ERR) | |
1176 | iser_err("wr id %llx status %d vend_err %x\n", | |
1177 | wc->wr_id, wc->status, wc->vendor_err); | |
1178 | else | |
1179 | iser_dbg("flush error: wr id %llx\n", wc->wr_id); | |
1180 | ||
ff3dd52d | 1181 | if (wc->wr_id == ISER_BEACON_WRID) |
30bf1d58 | 1182 | /* all flush errors were consumed */ |
6aabfa76 | 1183 | complete(&ib_conn->flush_comp); |
30bf1d58 SG |
1184 | else |
1185 | iser_handle_comp_error(ib_conn, wc); | |
78ad0a34 | 1186 | } |
78ad0a34 OG |
1187 | } |
1188 | ||
6aabfa76 SG |
1189 | /** |
1190 | * iser_cq_tasklet_fn - iSER completion polling loop | |
1191 | * @data: iSER completion context | |
1192 | * | |
1193 | * Soft-IRQ context, polling connection CQ until | |
1194 | * either CQ was empty or we exausted polling budget | |
1195 | */ | |
1cfa0a75 OG |
1196 | static void iser_cq_tasklet_fn(unsigned long data) |
1197 | { | |
bf175540 | 1198 | struct iser_comp *comp = (struct iser_comp *)data; |
6aabfa76 | 1199 | struct ib_cq *cq = comp->cq; |
6e6fe2fb SG |
1200 | struct ib_wc *const wcs = comp->wcs; |
1201 | int i, n, completed = 0; | |
1cfa0a75 | 1202 | |
6e6fe2fb SG |
1203 | while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) { |
1204 | for (i = 0; i < n; i++) | |
1205 | iser_handle_wc(&wcs[i]); | |
6aabfa76 | 1206 | |
6e6fe2fb SG |
1207 | completed += n; |
1208 | if (completed >= iser_cq_poll_limit) | |
183cfa43 | 1209 | break; |
1cfa0a75 | 1210 | } |
6aabfa76 SG |
1211 | |
1212 | /* | |
1213 | * It is assumed here that arming CQ only once its empty | |
1214 | * would not cause interrupts to be missed. | |
1215 | */ | |
1cfa0a75 | 1216 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
78ad0a34 | 1217 | |
6aabfa76 | 1218 | iser_dbg("got %d completions\n", completed); |
1cfa0a75 OG |
1219 | } |
1220 | ||
1221 | static void iser_cq_callback(struct ib_cq *cq, void *cq_context) | |
1222 | { | |
bf175540 | 1223 | struct iser_comp *comp = cq_context; |
1cfa0a75 | 1224 | |
bf175540 | 1225 | tasklet_schedule(&comp->tasklet); |
1cfa0a75 | 1226 | } |
0a7a08ad SG |
1227 | |
1228 | u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task, | |
1229 | enum iser_data_dir cmd_dir, sector_t *sector) | |
1230 | { | |
b130eded | 1231 | struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir]; |
0a7a08ad SG |
1232 | struct fast_reg_descriptor *desc = reg->mem_h; |
1233 | unsigned long sector_size = iser_task->sc->device->sector_size; | |
1234 | struct ib_mr_status mr_status; | |
1235 | int ret; | |
1236 | ||
1237 | if (desc && desc->reg_indicators & ISER_FASTREG_PROTECTED) { | |
1238 | desc->reg_indicators &= ~ISER_FASTREG_PROTECTED; | |
1239 | ret = ib_check_mr_status(desc->pi_ctx->sig_mr, | |
1240 | IB_MR_CHECK_SIG_STATUS, &mr_status); | |
1241 | if (ret) { | |
1242 | pr_err("ib_check_mr_status failed, ret %d\n", ret); | |
1243 | goto err; | |
1244 | } | |
1245 | ||
1246 | if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { | |
1247 | sector_t sector_off = mr_status.sig_err.sig_err_offset; | |
1248 | ||
1249 | do_div(sector_off, sector_size + 8); | |
1250 | *sector = scsi_get_lba(iser_task->sc) + sector_off; | |
1251 | ||
39c978cd | 1252 | pr_err("PI error found type %d at sector %llx " |
0a7a08ad | 1253 | "expected %x vs actual %x\n", |
39c978cd RD |
1254 | mr_status.sig_err.err_type, |
1255 | (unsigned long long)*sector, | |
0a7a08ad SG |
1256 | mr_status.sig_err.expected, |
1257 | mr_status.sig_err.actual); | |
1258 | ||
1259 | switch (mr_status.sig_err.err_type) { | |
1260 | case IB_SIG_BAD_GUARD: | |
1261 | return 0x1; | |
1262 | case IB_SIG_BAD_REFTAG: | |
1263 | return 0x3; | |
1264 | case IB_SIG_BAD_APPTAG: | |
1265 | return 0x2; | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | return 0; | |
1271 | err: | |
1272 | /* Not alot we can do here, return ambiguous guard error */ | |
1273 | return 0x1; | |
1274 | } |