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