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