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