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