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