blob: 5e49a36c72749762043175978713ebf809aac432 [file] [log] [blame]
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
Or Gerlitz28f292e2013-05-08 12:21:18 +00004 * Copyright (c) 2013 Mellanox Technologies. All rights reserved.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03005 *
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.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030033 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030034#include <linux/kernel.h>
35#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030037#include <linux/delay.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030038
39#include "iscsi_iser.h"
40
41#define ISCSI_ISER_MAX_CONN 8
Or Gerlitz78ad0a32010-02-08 13:19:21 +000042#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)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030044
45static void iser_cq_tasklet_fn(unsigned long data);
46static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030047
48static void iser_cq_event_callback(struct ib_event *cause, void *context)
49{
50 iser_err("got cq event %d \n", cause->event);
51}
52
53static void iser_qp_event_callback(struct ib_event *cause, void *context)
54{
55 iser_err("got qp event %d\n",cause->event);
56}
57
Or Gerlitz2110f9b2010-05-05 17:30:10 +030058static void iser_event_handler(struct ib_event_handler *handler,
59 struct ib_event *event)
60{
61 iser_err("async event %d on device %s port %d\n", event->event,
62 event->device->name, event->element.port_num);
63}
64
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030065/**
66 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
67 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
68 * the adapator.
69 *
70 * returns 0 on success, -1 on failure
71 */
72static int iser_create_device_ib_res(struct iser_device *device)
73{
Alex Tabachnik5a33a662012-09-23 15:17:44 +000074 int i, j;
75 struct iser_cq_desc *cq_desc;
76
77 device->cqs_used = min(ISER_MAX_CQ, device->ib_device->num_comp_vectors);
Roi Dayan4f363882013-05-01 13:25:25 +000078 iser_info("using %d CQs, device %s supports %d vectors\n",
79 device->cqs_used, device->ib_device->name,
80 device->ib_device->num_comp_vectors);
Alex Tabachnik5a33a662012-09-23 15:17:44 +000081
82 device->cq_desc = kmalloc(sizeof(struct iser_cq_desc) * device->cqs_used,
83 GFP_KERNEL);
84 if (device->cq_desc == NULL)
85 goto cq_desc_err;
86 cq_desc = device->cq_desc;
87
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030088 device->pd = ib_alloc_pd(device->ib_device);
89 if (IS_ERR(device->pd))
90 goto pd_err;
91
Alex Tabachnik5a33a662012-09-23 15:17:44 +000092 for (i = 0; i < device->cqs_used; i++) {
93 cq_desc[i].device = device;
94 cq_desc[i].cq_index = i;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030095
Alex Tabachnik5a33a662012-09-23 15:17:44 +000096 device->rx_cq[i] = ib_create_cq(device->ib_device,
97 iser_cq_callback,
98 iser_cq_event_callback,
99 (void *)&cq_desc[i],
100 ISER_MAX_RX_CQ_LEN, i);
101 if (IS_ERR(device->rx_cq[i]))
102 goto cq_err;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000103
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000104 device->tx_cq[i] = ib_create_cq(device->ib_device,
105 NULL, iser_cq_event_callback,
106 (void *)&cq_desc[i],
107 ISER_MAX_TX_CQ_LEN, i);
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000108
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000109 if (IS_ERR(device->tx_cq[i]))
110 goto cq_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300111
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000112 if (ib_req_notify_cq(device->rx_cq[i], IB_CQ_NEXT_COMP))
113 goto cq_err;
114
115 tasklet_init(&device->cq_tasklet[i],
116 iser_cq_tasklet_fn,
117 (unsigned long)&cq_desc[i]);
118 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300119
Erez Zilberd8111022006-09-11 12:26:33 +0300120 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
121 IB_ACCESS_REMOTE_WRITE |
122 IB_ACCESS_REMOTE_READ);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300123 if (IS_ERR(device->mr))
124 goto dma_mr_err;
125
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300126 INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
127 iser_event_handler);
128 if (ib_register_event_handler(&device->event_handler))
129 goto handler_err;
130
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300131 return 0;
132
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300133handler_err:
134 ib_dereg_mr(device->mr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300135dma_mr_err:
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000136 for (j = 0; j < device->cqs_used; j++)
137 tasklet_kill(&device->cq_tasklet[j]);
138cq_err:
139 for (j = 0; j < i; j++) {
140 if (device->tx_cq[j])
141 ib_destroy_cq(device->tx_cq[j]);
142 if (device->rx_cq[j])
143 ib_destroy_cq(device->rx_cq[j]);
144 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300145 ib_dealloc_pd(device->pd);
146pd_err:
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000147 kfree(device->cq_desc);
148cq_desc_err:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300149 iser_err("failed to allocate an IB resource\n");
150 return -1;
151}
152
153/**
Oliver Pinter38dc7322008-01-25 14:15:32 -0800154 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300155 * CQ and PD created with the device associated with the adapator.
156 */
157static void iser_free_device_ib_res(struct iser_device *device)
158{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000159 int i;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300160 BUG_ON(device->mr == NULL);
161
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000162 for (i = 0; i < device->cqs_used; i++) {
163 tasklet_kill(&device->cq_tasklet[i]);
164 (void)ib_destroy_cq(device->tx_cq[i]);
165 (void)ib_destroy_cq(device->rx_cq[i]);
166 device->tx_cq[i] = NULL;
167 device->rx_cq[i] = NULL;
168 }
169
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300170 (void)ib_unregister_event_handler(&device->event_handler);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300171 (void)ib_dereg_mr(device->mr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300172 (void)ib_dealloc_pd(device->pd);
173
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000174 kfree(device->cq_desc);
175
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300176 device->mr = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300177 device->pd = NULL;
178}
179
180/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300181 * iser_create_fmr_pool - Creates FMR pool and page_vector
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300182 *
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300183 * returns 0 on success, or errno code on failure
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300184 */
Shlomo Pongratzb7f04512013-07-28 12:35:38 +0300185int iser_create_fmr_pool(struct iser_conn *ib_conn, unsigned cmds_max)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300186{
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300187 struct iser_device *device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300188 struct ib_fmr_pool_param params;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300189 int ret = -ENOMEM;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000190
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300191 ib_conn->page_vec = kmalloc(sizeof(struct iser_page_vec) +
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300192 (sizeof(u64)*(ISCSI_ISER_SG_TABLESIZE+1)),
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300193 GFP_KERNEL);
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300194 if (!ib_conn->page_vec)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300195 return ret;
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300196
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300197 ib_conn->page_vec->pages = (u64 *)(ib_conn->page_vec + 1);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300198
Erez Zilber8dfa0872006-09-11 12:22:30 +0300199 params.page_shift = SHIFT_4K;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300200 /* when the first/last SG element are not start/end *
201 * page aligned, the map whould be of N+1 pages */
202 params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1;
203 /* make the pool size twice the max number of SCSI commands *
204 * the ML is expected to queue, watermark for unmap at 50% */
Shlomo Pongratzb7f04512013-07-28 12:35:38 +0300205 params.pool_size = cmds_max * 2;
206 params.dirty_watermark = cmds_max;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300207 params.cache = 0;
208 params.flush_function = NULL;
209 params.access = (IB_ACCESS_LOCAL_WRITE |
210 IB_ACCESS_REMOTE_WRITE |
211 IB_ACCESS_REMOTE_READ);
212
213 ib_conn->fmr_pool = ib_create_fmr_pool(device->pd, &params);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300214 if (!IS_ERR(ib_conn->fmr_pool))
215 return 0;
216
217 /* no FMR => no need for page_vec */
218 kfree(ib_conn->page_vec);
219 ib_conn->page_vec = NULL;
220
Or Gerlitz5525d212013-02-21 14:50:10 +0000221 ret = PTR_ERR(ib_conn->fmr_pool);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300222 ib_conn->fmr_pool = NULL;
223 if (ret != -ENOSYS) {
224 iser_err("FMR allocation failed, err %d\n", ret);
225 return ret;
226 } else {
Or Gerlitz5525d212013-02-21 14:50:10 +0000227 iser_warn("FMRs are not supported, using unaligned mode\n");
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300228 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300229 }
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300230}
231
232/**
233 * iser_free_fmr_pool - releases the FMR pool and page vec
234 */
235void iser_free_fmr_pool(struct iser_conn *ib_conn)
236{
237 iser_info("freeing conn %p fmr pool %p\n",
238 ib_conn, ib_conn->fmr_pool);
239
240 if (ib_conn->fmr_pool != NULL)
241 ib_destroy_fmr_pool(ib_conn->fmr_pool);
242
243 ib_conn->fmr_pool = NULL;
244
245 kfree(ib_conn->page_vec);
246 ib_conn->page_vec = NULL;
247}
248
249/**
250 * iser_create_ib_conn_res - Queue-Pair (QP)
251 *
252 * returns 0 on success, -1 on failure
253 */
254static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
255{
256 struct iser_device *device;
257 struct ib_qp_init_attr init_attr;
258 int ret = -ENOMEM;
259 int index, min_index = 0;
260
261 BUG_ON(ib_conn->device == NULL);
262
263 device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300264
265 memset(&init_attr, 0, sizeof init_attr);
266
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000267 mutex_lock(&ig.connlist_mutex);
268 /* select the CQ with the minimal number of usages */
269 for (index = 0; index < device->cqs_used; index++)
270 if (device->cq_active_qps[index] <
271 device->cq_active_qps[min_index])
272 min_index = index;
273 device->cq_active_qps[min_index]++;
274 mutex_unlock(&ig.connlist_mutex);
Roi Dayan4f363882013-05-01 13:25:25 +0000275 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000276
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300277 init_attr.event_handler = iser_qp_event_callback;
278 init_attr.qp_context = (void *)ib_conn;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000279 init_attr.send_cq = device->tx_cq[min_index];
280 init_attr.recv_cq = device->rx_cq[min_index];
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300281 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS;
282 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000283 init_attr.cap.max_send_sge = 2;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000284 init_attr.cap.max_recv_sge = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300285 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
286 init_attr.qp_type = IB_QPT_RC;
287
288 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
289 if (ret)
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300290 goto out_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300291
292 ib_conn->qp = ib_conn->cma_id->qp;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300293 iser_info("setting conn %p cma_id %p qp %p\n",
Roi Dayan4f363882013-05-01 13:25:25 +0000294 ib_conn, ib_conn->cma_id,
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300295 ib_conn->cma_id->qp);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300296 return ret;
297
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300298out_err:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300299 iser_err("unable to alloc mem or create resource, err %d\n", ret);
300 return ret;
301}
302
303/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300304 * releases the QP objects, returns 0 on success,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300305 * -1 on failure
306 */
Roi Dayan5b61ff42013-05-08 12:21:17 +0000307static int iser_free_ib_conn_res(struct iser_conn *ib_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300308{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000309 int cq_index;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300310 BUG_ON(ib_conn == NULL);
311
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300312 iser_info("freeing conn %p cma_id %p qp %p\n",
Roi Dayan4f363882013-05-01 13:25:25 +0000313 ib_conn, ib_conn->cma_id,
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300314 ib_conn->qp);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300315
316 /* qp is created only once both addr & route are resolved */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300317
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000318 if (ib_conn->qp != NULL) {
319 cq_index = ((struct iser_cq_desc *)ib_conn->qp->recv_cq->cq_context)->cq_index;
320 ib_conn->device->cq_active_qps[cq_index]--;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300321
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000322 rdma_destroy_qp(ib_conn->cma_id);
323 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300324
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300325 ib_conn->qp = NULL;
Doug Ledfordd4741862012-03-01 19:55:21 +0200326
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300327 return 0;
328}
329
330/**
331 * based on the resolved device node GUID see if there already allocated
332 * device for this device. If there's no such, create one.
333 */
334static
335struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
336{
Arne Redlich9a378272008-03-04 14:07:22 +0200337 struct iser_device *device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300338
339 mutex_lock(&ig.device_list_mutex);
340
Arne Redlich9a378272008-03-04 14:07:22 +0200341 list_for_each_entry(device, &ig.device_list, ig_list)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300342 /* find if there's a match using the node GUID */
343 if (device->ib_device->node_guid == cma_id->device->node_guid)
Arne Redlichd33ed422008-03-04 14:11:54 +0200344 goto inc_refcnt;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300345
Arne Redlich9a378272008-03-04 14:07:22 +0200346 device = kzalloc(sizeof *device, GFP_KERNEL);
347 if (device == NULL)
348 goto out;
349
350 /* assign this device to the device */
351 device->ib_device = cma_id->device;
352 /* init the device and link it into ig device list */
353 if (iser_create_device_ib_res(device)) {
354 kfree(device);
355 device = NULL;
356 goto out;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300357 }
Arne Redlich9a378272008-03-04 14:07:22 +0200358 list_add(&device->ig_list, &ig.device_list);
359
Arne Redlichd33ed422008-03-04 14:11:54 +0200360inc_refcnt:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300361 device->refcount++;
Arne Redlichd33ed422008-03-04 14:11:54 +0200362out:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300363 mutex_unlock(&ig.device_list_mutex);
364 return device;
365}
366
367/* if there's no demand for this device, release it */
368static void iser_device_try_release(struct iser_device *device)
369{
370 mutex_lock(&ig.device_list_mutex);
371 device->refcount--;
Roi Dayan4f363882013-05-01 13:25:25 +0000372 iser_info("device %p refcount %d\n", device, device->refcount);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300373 if (!device->refcount) {
374 iser_free_device_ib_res(device);
375 list_del(&device->ig_list);
376 kfree(device);
377 }
378 mutex_unlock(&ig.device_list_mutex);
379}
380
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300381static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
382 enum iser_ib_conn_state comp,
383 enum iser_ib_conn_state exch)
384{
385 int ret;
386
387 spin_lock_bh(&ib_conn->lock);
388 if ((ret = (ib_conn->state == comp)))
389 ib_conn->state = exch;
390 spin_unlock_bh(&ib_conn->lock);
391 return ret;
392}
393
394/**
Roland Dreier41179e22007-07-17 18:37:42 -0700395 * Frees all conn objects and deallocs conn descriptor
396 */
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300397static void iser_conn_release(struct iser_conn *ib_conn, int can_destroy_id)
Roland Dreier41179e22007-07-17 18:37:42 -0700398{
399 struct iser_device *device = ib_conn->device;
400
401 BUG_ON(ib_conn->state != ISER_CONN_DOWN);
402
403 mutex_lock(&ig.connlist_mutex);
404 list_del(&ib_conn->conn_list);
405 mutex_unlock(&ig.connlist_mutex);
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000406 iser_free_rx_descriptors(ib_conn);
Roi Dayan5b61ff42013-05-08 12:21:17 +0000407 iser_free_ib_conn_res(ib_conn);
Roland Dreier41179e22007-07-17 18:37:42 -0700408 ib_conn->device = NULL;
409 /* on EVENT_ADDR_ERROR there's no device yet for this conn */
410 if (device != NULL)
411 iser_device_try_release(device);
Roi Dayan5b61ff42013-05-08 12:21:17 +0000412 /* if cma handler context, the caller actually destroy the id */
413 if (ib_conn->cma_id != NULL && can_destroy_id) {
414 rdma_destroy_id(ib_conn->cma_id);
415 ib_conn->cma_id = NULL;
416 }
Mike Christie412eeaf2008-05-21 15:54:14 -0500417 iscsi_destroy_endpoint(ib_conn->ep);
Roland Dreier41179e22007-07-17 18:37:42 -0700418}
419
Mike Christieb40977d2008-05-21 15:54:03 -0500420void iser_conn_get(struct iser_conn *ib_conn)
421{
422 atomic_inc(&ib_conn->refcount);
423}
424
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300425int iser_conn_put(struct iser_conn *ib_conn, int can_destroy_id)
Mike Christieb40977d2008-05-21 15:54:03 -0500426{
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300427 if (atomic_dec_and_test(&ib_conn->refcount)) {
428 iser_conn_release(ib_conn, can_destroy_id);
429 return 1;
430 }
431 return 0;
Roland Dreier41179e22007-07-17 18:37:42 -0700432}
433
434/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300435 * triggers start of the disconnect procedures and wait for them to be done
436 */
437void iser_conn_terminate(struct iser_conn *ib_conn)
438{
439 int err = 0;
440
441 /* change the ib conn state only if the conn is UP, however always call
442 * rdma_disconnect since this is the only way to cause the CMA to change
443 * the QP state to ERROR
444 */
445
446 iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP, ISER_CONN_TERMINATING);
447 err = rdma_disconnect(ib_conn->cma_id);
448 if (err)
449 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
450 ib_conn,err);
451
452 wait_event_interruptible(ib_conn->wait,
453 ib_conn->state == ISER_CONN_DOWN);
454
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300455 iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300456}
457
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300458static int iser_connect_error(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300459{
460 struct iser_conn *ib_conn;
461 ib_conn = (struct iser_conn *)cma_id->context;
462
463 ib_conn->state = ISER_CONN_DOWN;
464 wake_up_interruptible(&ib_conn->wait);
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300465 return iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300466}
467
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300468static int iser_addr_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300469{
470 struct iser_device *device;
471 struct iser_conn *ib_conn;
472 int ret;
473
474 device = iser_device_find_by_ib_device(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200475 if (!device) {
476 iser_err("device lookup/creation failed\n");
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300477 return iser_connect_error(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200478 }
479
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300480 ib_conn = (struct iser_conn *)cma_id->context;
481 ib_conn->device = device;
482
483 ret = rdma_resolve_route(cma_id, 1000);
484 if (ret) {
485 iser_err("resolve route failed: %d\n", ret);
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300486 return iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300487 }
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300488
489 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300490}
491
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300492static int iser_route_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300493{
494 struct rdma_conn_param conn_param;
495 int ret;
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000496 struct iser_cm_hdr req_hdr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300497
498 ret = iser_create_ib_conn_res((struct iser_conn *)cma_id->context);
499 if (ret)
500 goto failure;
501
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300502 memset(&conn_param, 0, sizeof conn_param);
503 conn_param.responder_resources = 4;
504 conn_param.initiator_depth = 1;
505 conn_param.retry_count = 7;
506 conn_param.rnr_retry_count = 6;
507
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000508 memset(&req_hdr, 0, sizeof(req_hdr));
509 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
510 ISER_SEND_W_INV_NOT_SUPPORTED);
511 conn_param.private_data = (void *)&req_hdr;
512 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
513
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300514 ret = rdma_connect(cma_id, &conn_param);
515 if (ret) {
516 iser_err("failure connecting: %d\n", ret);
517 goto failure;
518 }
519
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300520 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300521failure:
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300522 return iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300523}
524
525static void iser_connected_handler(struct rdma_cm_id *cma_id)
526{
527 struct iser_conn *ib_conn;
528
529 ib_conn = (struct iser_conn *)cma_id->context;
530 ib_conn->state = ISER_CONN_UP;
531 wake_up_interruptible(&ib_conn->wait);
532}
533
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300534static int iser_disconnected_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300535{
536 struct iser_conn *ib_conn;
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300537 int ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300538
539 ib_conn = (struct iser_conn *)cma_id->context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300540
541 /* getting here when the state is UP means that the conn is being *
542 * terminated asynchronously from the iSCSI layer's perspective. */
543 if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
544 ISER_CONN_TERMINATING))
545 iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
546 ISCSI_ERR_CONN_FAILED);
547
548 /* Complete the termination process if no posts are pending */
Or Gerlitz704315f2010-02-08 13:18:39 +0000549 if (ib_conn->post_recv_buf_count == 0 &&
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300550 (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
551 ib_conn->state = ISER_CONN_DOWN;
552 wake_up_interruptible(&ib_conn->wait);
553 }
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300554
555 ret = iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
556 return ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300557}
558
559static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
560{
561 int ret = 0;
562
Roi Dayan4f363882013-05-01 13:25:25 +0000563 iser_info("event %d status %d conn %p id %p\n",
564 event->event, event->status, cma_id->context, cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300565
566 switch (event->event) {
567 case RDMA_CM_EVENT_ADDR_RESOLVED:
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300568 ret = iser_addr_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300569 break;
570 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300571 ret = iser_route_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300572 break;
573 case RDMA_CM_EVENT_ESTABLISHED:
574 iser_connected_handler(cma_id);
575 break;
576 case RDMA_CM_EVENT_ADDR_ERROR:
577 case RDMA_CM_EVENT_ROUTE_ERROR:
578 case RDMA_CM_EVENT_CONNECT_ERROR:
579 case RDMA_CM_EVENT_UNREACHABLE:
580 case RDMA_CM_EVENT_REJECTED:
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300581 ret = iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300582 break;
583 case RDMA_CM_EVENT_DISCONNECTED:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300584 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Or Gerlitz2f5de152008-07-22 14:16:21 -0700585 case RDMA_CM_EVENT_ADDR_CHANGE:
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300586 ret = iser_disconnected_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300587 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300588 default:
Erez Zilbera4ef1452008-01-17 11:51:58 +0200589 iser_err("Unexpected RDMA CM event (%d)\n", event->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300590 break;
591 }
592 return ret;
593}
594
Mike Christie412eeaf2008-05-21 15:54:14 -0500595void iser_conn_init(struct iser_conn *ib_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300596{
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300597 ib_conn->state = ISER_CONN_INIT;
598 init_waitqueue_head(&ib_conn->wait);
Or Gerlitz704315f2010-02-08 13:18:39 +0000599 ib_conn->post_recv_buf_count = 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300600 atomic_set(&ib_conn->post_send_buf_count, 0);
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300601 atomic_set(&ib_conn->refcount, 1); /* ref ib conn allocation */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300602 INIT_LIST_HEAD(&ib_conn->conn_list);
603 spin_lock_init(&ib_conn->lock);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300604}
605
606 /**
607 * starts the process of connecting to the target
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200608 * sleeps until the connection is established or rejected
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300609 */
610int iser_connect(struct iser_conn *ib_conn,
611 struct sockaddr_in *src_addr,
612 struct sockaddr_in *dst_addr,
613 int non_blocking)
614{
615 struct sockaddr *src, *dst;
616 int err = 0;
617
Harvey Harrison63779432008-10-31 00:56:00 -0700618 sprintf(ib_conn->name, "%pI4:%d",
619 &dst_addr->sin_addr.s_addr, dst_addr->sin_port);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300620
621 /* the device is known only --after-- address resolution */
622 ib_conn->device = NULL;
623
Roi Dayan4f363882013-05-01 13:25:25 +0000624 iser_info("connecting to: %pI4, port 0x%x\n",
625 &dst_addr->sin_addr, dst_addr->sin_port);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300626
627 ib_conn->state = ISER_CONN_PENDING;
628
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300629 iser_conn_get(ib_conn); /* ref ib conn's cma id */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300630 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
631 (void *)ib_conn,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000632 RDMA_PS_TCP, IB_QPT_RC);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300633 if (IS_ERR(ib_conn->cma_id)) {
634 err = PTR_ERR(ib_conn->cma_id);
635 iser_err("rdma_create_id failed: %d\n", err);
636 goto id_failure;
637 }
638
639 src = (struct sockaddr *)src_addr;
640 dst = (struct sockaddr *)dst_addr;
641 err = rdma_resolve_addr(ib_conn->cma_id, src, dst, 1000);
642 if (err) {
643 iser_err("rdma_resolve_addr failed: %d\n", err);
644 goto addr_failure;
645 }
646
647 if (!non_blocking) {
648 wait_event_interruptible(ib_conn->wait,
649 (ib_conn->state != ISER_CONN_PENDING));
650
651 if (ib_conn->state != ISER_CONN_UP) {
652 err = -EIO;
653 goto connect_failure;
654 }
655 }
656
657 mutex_lock(&ig.connlist_mutex);
658 list_add(&ib_conn->conn_list, &ig.connlist);
659 mutex_unlock(&ig.connlist_mutex);
660 return 0;
661
662id_failure:
663 ib_conn->cma_id = NULL;
664addr_failure:
665 ib_conn->state = ISER_CONN_DOWN;
Or Gerlitz7d9c0de2012-04-29 17:04:21 +0300666 iser_conn_put(ib_conn, 1); /* deref ib conn's cma id */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300667connect_failure:
Or Gerlitz7d9c0de2012-04-29 17:04:21 +0300668 iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300669 return err;
670}
671
672/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300673 * iser_reg_page_vec - Register physical memory
674 *
675 * returns: 0 on success, errno code on failure
676 */
677int iser_reg_page_vec(struct iser_conn *ib_conn,
678 struct iser_page_vec *page_vec,
679 struct iser_mem_reg *mem_reg)
680{
681 struct ib_pool_fmr *mem;
682 u64 io_addr;
683 u64 *page_list;
684 int status;
685
686 page_list = page_vec->pages;
687 io_addr = page_list[0];
688
689 mem = ib_fmr_pool_map_phys(ib_conn->fmr_pool,
690 page_list,
691 page_vec->length,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700692 io_addr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300693
694 if (IS_ERR(mem)) {
695 status = (int)PTR_ERR(mem);
696 iser_err("ib_fmr_pool_map_phys failed: %d\n", status);
697 return status;
698 }
699
700 mem_reg->lkey = mem->fmr->lkey;
701 mem_reg->rkey = mem->fmr->rkey;
Erez Zilber8dfa0872006-09-11 12:22:30 +0300702 mem_reg->len = page_vec->length * SIZE_4K;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300703 mem_reg->va = io_addr;
Erez Zilberd8111022006-09-11 12:26:33 +0300704 mem_reg->is_fmr = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300705 mem_reg->mem_h = (void *)mem;
706
707 mem_reg->va += page_vec->offset;
708 mem_reg->len = page_vec->data_size;
709
710 iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
711 "entry[0]: (0x%08lx,%ld)] -> "
712 "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
713 page_vec, page_vec->length,
714 (unsigned long)page_vec->pages[0],
715 (unsigned long)page_vec->data_size,
716 (unsigned int)mem_reg->lkey, mem_reg->mem_h,
717 (unsigned long)mem_reg->va, (unsigned long)mem_reg->len);
718 return 0;
719}
720
721/**
722 * Unregister (previosuly registered) memory.
723 */
724void iser_unreg_mem(struct iser_mem_reg *reg)
725{
726 int ret;
727
728 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg->mem_h);
729
730 ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
731 if (ret)
732 iser_err("ib_fmr_pool_unmap failed %d\n", ret);
733
734 reg->mem_h = NULL;
735}
736
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000737int iser_post_recvl(struct iser_conn *ib_conn)
738{
739 struct ib_recv_wr rx_wr, *rx_wr_failed;
740 struct ib_sge sge;
741 int ib_ret;
742
Or Gerlitz2c4ce602011-11-04 00:19:46 +0200743 sge.addr = ib_conn->login_resp_dma;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000744 sge.length = ISER_RX_LOGIN_SIZE;
745 sge.lkey = ib_conn->device->mr->lkey;
746
Or Gerlitz2c4ce602011-11-04 00:19:46 +0200747 rx_wr.wr_id = (unsigned long)ib_conn->login_resp_buf;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000748 rx_wr.sg_list = &sge;
749 rx_wr.num_sge = 1;
750 rx_wr.next = NULL;
751
Or Gerlitz704315f2010-02-08 13:18:39 +0000752 ib_conn->post_recv_buf_count++;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000753 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
754 if (ib_ret) {
755 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Or Gerlitz704315f2010-02-08 13:18:39 +0000756 ib_conn->post_recv_buf_count--;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000757 }
758 return ib_ret;
759}
760
761int iser_post_recvm(struct iser_conn *ib_conn, int count)
762{
763 struct ib_recv_wr *rx_wr, *rx_wr_failed;
764 int i, ib_ret;
765 unsigned int my_rx_head = ib_conn->rx_desc_head;
766 struct iser_rx_desc *rx_desc;
767
768 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
769 rx_desc = &ib_conn->rx_descs[my_rx_head];
770 rx_wr->wr_id = (unsigned long)rx_desc;
771 rx_wr->sg_list = &rx_desc->rx_sg;
772 rx_wr->num_sge = 1;
773 rx_wr->next = rx_wr + 1;
Shlomo Pongratzb7f04512013-07-28 12:35:38 +0300774 my_rx_head = (my_rx_head + 1) & ib_conn->qp_max_recv_dtos_mask;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000775 }
776
777 rx_wr--;
778 rx_wr->next = NULL; /* mark end of work requests list */
779
Or Gerlitz704315f2010-02-08 13:18:39 +0000780 ib_conn->post_recv_buf_count += count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000781 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
782 if (ib_ret) {
783 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Or Gerlitz704315f2010-02-08 13:18:39 +0000784 ib_conn->post_recv_buf_count -= count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000785 } else
786 ib_conn->rx_desc_head = my_rx_head;
787 return ib_ret;
788}
789
790
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300791/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300792 * iser_start_send - Initiate a Send DTO operation
793 *
794 * returns 0 on success, -1 on failure
795 */
Or Gerlitzf19624a2010-02-08 13:19:56 +0000796int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300797{
Or Gerlitzf19624a2010-02-08 13:19:56 +0000798 int ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300799 struct ib_send_wr send_wr, *send_wr_failed;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300800
Or Gerlitzf19624a2010-02-08 13:19:56 +0000801 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
802 tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300803
804 send_wr.next = NULL;
805 send_wr.wr_id = (unsigned long)tx_desc;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000806 send_wr.sg_list = tx_desc->tx_sg;
807 send_wr.num_sge = tx_desc->num_sge;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300808 send_wr.opcode = IB_WR_SEND;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000809 send_wr.send_flags = IB_SEND_SIGNALED;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300810
811 atomic_inc(&ib_conn->post_send_buf_count);
812
813 ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
814 if (ib_ret) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300815 iser_err("ib_post_send failed, ret:%d\n", ib_ret);
816 atomic_dec(&ib_conn->post_send_buf_count);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300817 }
Or Gerlitzf19624a2010-02-08 13:19:56 +0000818 return ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300819}
820
Or Gerlitzf19624a2010-02-08 13:19:56 +0000821static void iser_handle_comp_error(struct iser_tx_desc *desc,
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000822 struct iser_conn *ib_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300823{
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000824 if (desc && desc->type == ISCSI_TX_DATAOUT)
825 kmem_cache_free(ig.desc_cache, desc);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300826
Or Gerlitz704315f2010-02-08 13:18:39 +0000827 if (ib_conn->post_recv_buf_count == 0 &&
Erez Zilber1d426d62007-04-01 12:53:43 +0200828 atomic_read(&ib_conn->post_send_buf_count) == 0) {
829 /* getting here when the state is UP means that the conn is *
830 * being terminated asynchronously from the iSCSI layer's *
831 * perspective. */
832 if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
833 ISER_CONN_TERMINATING))
834 iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
835 ISCSI_ERR_CONN_FAILED);
836
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300837 /* no more non completed posts to the QP, complete the
838 * termination process w.o worrying on disconnect event */
839 ib_conn->state = ISER_CONN_DOWN;
840 wake_up_interruptible(&ib_conn->wait);
Erez Zilber1d426d62007-04-01 12:53:43 +0200841 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300842}
843
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000844static int iser_drain_tx_cq(struct iser_device *device, int cq_index)
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000845{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000846 struct ib_cq *cq = device->tx_cq[cq_index];
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000847 struct ib_wc wc;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000848 struct iser_tx_desc *tx_desc;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000849 struct iser_conn *ib_conn;
850 int completed_tx = 0;
851
852 while (ib_poll_cq(cq, 1, &wc) == 1) {
Or Gerlitzf19624a2010-02-08 13:19:56 +0000853 tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000854 ib_conn = wc.qp->qp_context;
855 if (wc.status == IB_WC_SUCCESS) {
856 if (wc.opcode == IB_WC_SEND)
Or Gerlitzf19624a2010-02-08 13:19:56 +0000857 iser_snd_completion(tx_desc, ib_conn);
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000858 else
859 iser_err("expected opcode %d got %d\n",
860 IB_WC_SEND, wc.opcode);
861 } else {
862 iser_err("tx id %llx status %d vend_err %x\n",
863 wc.wr_id, wc.status, wc.vendor_err);
864 atomic_dec(&ib_conn->post_send_buf_count);
865 iser_handle_comp_error(tx_desc, ib_conn);
866 }
867 completed_tx++;
868 }
869 return completed_tx;
870}
871
872
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300873static void iser_cq_tasklet_fn(unsigned long data)
874{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000875 struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)data;
876 struct iser_device *device = cq_desc->device;
877 int cq_index = cq_desc->cq_index;
878 struct ib_cq *cq = device->rx_cq[cq_index];
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300879 struct ib_wc wc;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000880 struct iser_rx_desc *desc;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300881 unsigned long xfer_len;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000882 struct iser_conn *ib_conn;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000883 int completed_tx, completed_rx;
884 completed_tx = completed_rx = 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300885
886 while (ib_poll_cq(cq, 1, &wc) == 1) {
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000887 desc = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300888 BUG_ON(desc == NULL);
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000889 ib_conn = wc.qp->qp_context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300890 if (wc.status == IB_WC_SUCCESS) {
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000891 if (wc.opcode == IB_WC_RECV) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300892 xfer_len = (unsigned long)wc.byte_len;
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000893 iser_rcv_completion(desc, xfer_len, ib_conn);
894 } else
895 iser_err("expected opcode %d got %d\n",
896 IB_WC_RECV, wc.opcode);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300897 } else {
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000898 if (wc.status != IB_WC_WR_FLUSH_ERR)
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000899 iser_err("rx id %llx status %d vend_err %x\n",
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000900 wc.wr_id, wc.status, wc.vendor_err);
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000901 ib_conn->post_recv_buf_count--;
902 iser_handle_comp_error(NULL, ib_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300903 }
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000904 completed_rx++;
905 if (!(completed_rx & 63))
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000906 completed_tx += iser_drain_tx_cq(device, cq_index);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300907 }
908 /* #warning "it is assumed here that arming CQ only once its empty" *
909 * " would not cause interrupts to be missed" */
910 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000911
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000912 completed_tx += iser_drain_tx_cq(device, cq_index);
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000913 iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300914}
915
916static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
917{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000918 struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)cq_context;
919 struct iser_device *device = cq_desc->device;
920 int cq_index = cq_desc->cq_index;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300921
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000922 tasklet_schedule(&device->cq_tasklet[cq_index]);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300923}