blob: 4269f22ccfbf2b10690003045a7c52b86b2d2c59 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032#include "ib_isert.h"
33
34#define ISERT_MAX_CONN 8
35#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
36#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020037#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
38 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080039
Sagi Grimberg45678b62015-01-25 19:11:19 +020040static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020041module_param_named(debug_level, isert_debug_level, int, 0644);
42MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
43
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044static DEFINE_MUTEX(device_list_mutex);
45static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080046static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +020047static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080048
49static void
Vu Phamd40945d2013-08-28 23:23:34 +030050isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
51static int
52isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030054static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020055isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030056static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020057isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
58 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020059static int
60isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020061static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +020062isert_login_post_recv(struct isert_conn *isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020063static int
64isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020065struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030066
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030067static void isert_release_work(struct work_struct *work);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +020068static void isert_wait4flush(struct isert_conn *isert_conn);
Christoph Hellwig9679cc52016-02-24 19:24:06 +020069static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
70static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
71static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
72static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030073
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020074static inline bool
75isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
76{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020077 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020078 cmd->prot_op != TARGET_PROT_NORMAL);
79}
80
81
Vu Phamd40945d2013-08-28 23:23:34 +030082static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080083isert_qp_event_callback(struct ib_event *e, void *context)
84{
Sagi Grimberg67004252015-03-29 15:52:09 +030085 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080086
Sagi Grimbergea8a1612015-05-18 13:40:31 +030087 isert_err("%s (%d): conn %p\n",
88 ib_event_msg(e->event), e->event, isert_conn);
89
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080090 switch (e->event) {
91 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030092 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080093 break;
94 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020095 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080096 break;
97 default:
98 break;
99 }
100}
101
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300102static struct isert_comp *
103isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300105 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200106 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300107 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800108
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800109 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200110 for (i = 0; i < device->comps_used; i++)
111 if (device->comps[i].active_qps <
112 device->comps[min].active_qps)
113 min = i;
114 comp = &device->comps[min];
115 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300116 mutex_unlock(&device_list_mutex);
117
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200118 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200119 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300120
121 return comp;
122}
123
124static void
125isert_comp_put(struct isert_comp *comp)
126{
127 mutex_lock(&device_list_mutex);
128 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800129 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300130}
131
132static struct ib_qp *
133isert_create_qp(struct isert_conn *isert_conn,
134 struct isert_comp *comp,
135 struct rdma_cm_id *cma_id)
136{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300137 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300138 struct ib_qp_init_attr attr;
139 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800140
141 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
142 attr.event_handler = isert_qp_event_callback;
143 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200144 attr.send_cq = comp->cq;
145 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800146 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200147 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200148 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
149 isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
150 device->ib_device->attrs.max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800151 attr.cap.max_recv_sge = 1;
152 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
153 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200154 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200155 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800156
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300157 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200159 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300160 return ERR_PTR(ret);
161 }
162
163 return cma_id->qp;
164}
165
166static int
167isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
168{
169 struct isert_comp *comp;
170 int ret;
171
172 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300173 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
174 if (IS_ERR(isert_conn->qp)) {
175 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200176 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800177 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800178
179 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200180err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300181 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200182 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800183}
184
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800185static int
186isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
187{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300188 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300189 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800190 struct iser_rx_desc *rx_desc;
191 struct ib_sge *rx_sg;
192 u64 dma_addr;
193 int i, j;
194
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300195 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800196 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300197 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800198 goto fail;
199
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300200 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800201
202 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
203 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
204 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
205 if (ib_dma_mapping_error(ib_dev, dma_addr))
206 goto dma_map_fail;
207
208 rx_desc->dma_addr = dma_addr;
209
210 rx_sg = &rx_desc->rx_sg;
211 rx_sg->addr = rx_desc->dma_addr;
212 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600213 rx_sg->lkey = device->pd->local_dma_lkey;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200214 rx_desc->rx_cqe.done = isert_recv_done;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800215 }
216
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800217 return 0;
218
219dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300220 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 for (j = 0; j < i; j++, rx_desc++) {
222 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
223 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
224 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300225 kfree(isert_conn->rx_descs);
226 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800227fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200228 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
229
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800230 return -ENOMEM;
231}
232
233static void
234isert_free_rx_descriptors(struct isert_conn *isert_conn)
235{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300236 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800237 struct iser_rx_desc *rx_desc;
238 int i;
239
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300240 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 return;
242
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300243 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
245 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
246 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
247 }
248
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300249 kfree(isert_conn->rx_descs);
250 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251}
252
Sagi Grimberg172369c2015-03-29 15:52:11 +0300253static void
254isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800255{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300256 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800257
Sagi Grimberg172369c2015-03-29 15:52:11 +0300258 for (i = 0; i < device->comps_used; i++) {
259 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300260
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200261 if (comp->cq)
262 ib_free_cq(comp->cq);
Vu Pham59464ef2013-08-28 23:23:35 +0300263 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300264 kfree(device->comps);
265}
Vu Phamd40945d2013-08-28 23:23:34 +0300266
Sagi Grimberg172369c2015-03-29 15:52:11 +0300267static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200268isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300269{
270 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200271
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200272 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300273 device->ib_device->num_comp_vectors));
274
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200275 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200276 "Fast registration %d pi_capable %d\n",
277 device->comps_used, device->ib_device->name,
278 device->ib_device->num_comp_vectors, device->use_fastreg,
279 device->pi_capable);
280
281 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
282 GFP_KERNEL);
283 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200284 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800285 return -ENOMEM;
286 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800287
Or Gerlitz4a061b22015-12-18 10:59:46 +0200288 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300289
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200290 for (i = 0; i < device->comps_used; i++) {
291 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800292
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200293 comp->device = device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200294 comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
295 IB_POLL_WORKQUEUE);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200296 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300297 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200298 ret = PTR_ERR(comp->cq);
299 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800300 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800301 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800302 }
303
Sagi Grimberg172369c2015-03-29 15:52:11 +0300304 return 0;
305out_cq:
306 isert_free_comps(device);
307 return ret;
308}
309
310static int
311isert_create_device_ib_res(struct isert_device *device)
312{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200313 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300314 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300315
Or Gerlitz4a061b22015-12-18 10:59:46 +0200316 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
317 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300318
319 /* asign function handlers */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200320 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
321 ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300322 device->use_fastreg = 1;
323 device->reg_rdma_mem = isert_reg_rdma;
324 device->unreg_rdma_mem = isert_unreg_rdma;
325 } else {
326 device->use_fastreg = 0;
327 device->reg_rdma_mem = isert_map_rdma;
328 device->unreg_rdma_mem = isert_unmap_cmd;
329 }
330
Or Gerlitz4a061b22015-12-18 10:59:46 +0200331 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300332 if (ret)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800333 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300334
Or Gerlitz4a061b22015-12-18 10:59:46 +0200335 device->pd = ib_alloc_pd(ib_dev);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300336 if (IS_ERR(device->pd)) {
337 ret = PTR_ERR(device->pd);
338 isert_err("failed to allocate pd, device %p, ret=%d\n",
339 device, ret);
340 goto out_cq;
341 }
342
Sagi Grimberg172369c2015-03-29 15:52:11 +0300343 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200344 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300345 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300346
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800347 return 0;
348
349out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300350 isert_free_comps(device);
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800351out:
352 if (ret > 0)
353 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800354 return ret;
355}
356
357static void
358isert_free_device_ib_res(struct isert_device *device)
359{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200360 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800361
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300362 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300363 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800364}
365
366static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300367isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800368{
369 mutex_lock(&device_list_mutex);
370 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200371 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800372 if (!device->refcount) {
373 isert_free_device_ib_res(device);
374 list_del(&device->dev_node);
375 kfree(device);
376 }
377 mutex_unlock(&device_list_mutex);
378}
379
380static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300381isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800382{
383 struct isert_device *device;
384 int ret;
385
386 mutex_lock(&device_list_mutex);
387 list_for_each_entry(device, &device_list, dev_node) {
388 if (device->ib_device->node_guid == cma_id->device->node_guid) {
389 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200390 isert_info("Found iser device %p refcount %d\n",
391 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800392 mutex_unlock(&device_list_mutex);
393 return device;
394 }
395 }
396
397 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
398 if (!device) {
399 mutex_unlock(&device_list_mutex);
400 return ERR_PTR(-ENOMEM);
401 }
402
403 INIT_LIST_HEAD(&device->dev_node);
404
405 device->ib_device = cma_id->device;
406 ret = isert_create_device_ib_res(device);
407 if (ret) {
408 kfree(device);
409 mutex_unlock(&device_list_mutex);
410 return ERR_PTR(ret);
411 }
412
413 device->refcount++;
414 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200415 isert_info("Created a new iser device %p refcount %d\n",
416 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800417 mutex_unlock(&device_list_mutex);
418
419 return device;
420}
421
Vu Pham59464ef2013-08-28 23:23:35 +0300422static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200423isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300424{
425 struct fast_reg_descriptor *fr_desc, *tmp;
426 int i = 0;
427
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300428 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300429 return;
430
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200431 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300432
433 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300434 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300435 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300436 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200437 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200438 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300439 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200440 kfree(fr_desc->pi_ctx);
441 }
Vu Pham59464ef2013-08-28 23:23:35 +0300442 kfree(fr_desc);
443 ++i;
444 }
445
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300446 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200447 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300448 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300449}
450
451static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200452isert_create_pi_ctx(struct fast_reg_descriptor *desc,
453 struct ib_device *device,
454 struct ib_pd *pd)
455{
Sagi Grimberg570db172014-12-02 16:57:31 +0200456 struct pi_context *pi_ctx;
457 int ret;
458
459 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
460 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200461 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200462 return -ENOMEM;
463 }
464
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300465 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
466 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200467 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200468 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200469 PTR_ERR(pi_ctx->prot_mr));
470 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300471 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200472 }
473 desc->ind |= ISERT_PROT_KEY_VALID;
474
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300475 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200476 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200477 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200478 PTR_ERR(pi_ctx->sig_mr));
479 ret = PTR_ERR(pi_ctx->sig_mr);
480 goto err_prot_mr;
481 }
482
483 desc->pi_ctx = pi_ctx;
484 desc->ind |= ISERT_SIG_KEY_VALID;
485 desc->ind &= ~ISERT_PROTECTED;
486
487 return 0;
488
489err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700490 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200491err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700492 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200493
494 return ret;
495}
496
497static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200498isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200499 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200500{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300501 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
502 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200503 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200504 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200505 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300506 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200507 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200508 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200509
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200510 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200511
512 return 0;
513}
514
515static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200516isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300517{
518 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300519 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700520 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
521 struct se_node_acl *se_nacl = se_sess->se_node_acl;
522 int i, ret, tag_num;
523 /*
524 * Setup the number of FRMRs based upon the number of tags
525 * available to session in iscsi_target_locate_portal().
526 */
527 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
528 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300529
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300530 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700531 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300532 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
533 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200534 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300535 ret = -ENOMEM;
536 goto err;
537 }
538
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200539 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300540 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200541 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200542 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200543 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800544 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300545 goto err;
546 }
547
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300548 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
549 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300550 }
551
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200552 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300553 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300554
555 return 0;
556
557err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200558 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300559 return ret;
560}
561
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300562static void
563isert_init_conn(struct isert_conn *isert_conn)
564{
565 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300566 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300567 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300568 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300569 kref_init(&isert_conn->kref);
570 mutex_init(&isert_conn->mutex);
571 spin_lock_init(&isert_conn->pool_lock);
572 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300573 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300574}
575
576static void
577isert_free_login_buf(struct isert_conn *isert_conn)
578{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300579 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300580
581 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200582 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200583 kfree(isert_conn->login_rsp_buf);
584
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300585 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200586 ISER_RX_PAYLOAD_SIZE,
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300587 DMA_FROM_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200588 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300589}
590
591static int
592isert_alloc_login_buf(struct isert_conn *isert_conn,
593 struct ib_device *ib_dev)
594{
595 int ret;
596
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200597 isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
598 GFP_KERNEL);
599 if (!isert_conn->login_req_buf) {
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300600 isert_err("Unable to allocate isert_conn->login_buf\n");
601 return -ENOMEM;
602 }
603
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300604 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200605 isert_conn->login_req_buf,
606 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300607 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
608 if (ret) {
609 isert_err("login_req_dma mapping error: %d\n", ret);
610 isert_conn->login_req_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200611 goto out_free_login_req_buf;
612 }
613
614 isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
615 if (!isert_conn->login_rsp_buf) {
616 isert_err("Unable to allocate isert_conn->login_rspbuf\n");
617 goto out_unmap_login_req_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300618 }
619
620 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200621 isert_conn->login_rsp_buf,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200622 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300623 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
624 if (ret) {
625 isert_err("login_rsp_dma mapping error: %d\n", ret);
626 isert_conn->login_rsp_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200627 goto out_free_login_rsp_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300628 }
629
630 return 0;
631
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200632out_free_login_rsp_buf:
633 kfree(isert_conn->login_rsp_buf);
634out_unmap_login_req_buf:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300635 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200636 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
637out_free_login_req_buf:
638 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300639 return ret;
640}
641
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200642static void
643isert_set_nego_params(struct isert_conn *isert_conn,
644 struct rdma_conn_param *param)
645{
646 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
647
648 /* Set max inflight RDMA READ requests */
649 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
650 attr->max_qp_init_rd_atom);
651 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
652
653 if (param->private_data) {
654 u8 flags = *(u8 *)param->private_data;
655
656 /*
657 * use remote invalidation if the both initiator
658 * and the HCA support it
659 */
660 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
661 (attr->device_cap_flags &
662 IB_DEVICE_MEM_MGT_EXTENSIONS);
663 if (isert_conn->snd_w_inv)
664 isert_info("Using remote invalidation\n");
665 }
666}
667
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800668static int
669isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
670{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200671 struct isert_np *isert_np = cma_id->context;
672 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800673 struct isert_conn *isert_conn;
674 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800675 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300676
677 spin_lock_bh(&np->np_thread_lock);
678 if (!np->enabled) {
679 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200680 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300681 return rdma_reject(cma_id, NULL, 0);
682 }
683 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800684
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200685 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 cma_id, cma_id->context);
687
688 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300689 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800690 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800691
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300692 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300693 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800694
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300695 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
696 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800697 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300699 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800700 if (IS_ERR(device)) {
701 ret = PTR_ERR(device);
702 goto out_rsp_dma_map;
703 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300704 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800705
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200706 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300707
Sagi Grimberg570db172014-12-02 16:57:31 +0200708 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800709 if (ret)
710 goto out_conn_dev;
711
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200712 ret = isert_login_post_recv(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200713 if (ret)
714 goto out_conn_dev;
715
716 ret = isert_rdma_accept(isert_conn);
717 if (ret)
718 goto out_conn_dev;
719
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300720 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300721 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300722 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800723
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800724 return 0;
725
726out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300727 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800728out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300729 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800730out:
731 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200732 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800733 return ret;
734}
735
736static void
737isert_connect_release(struct isert_conn *isert_conn)
738{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300739 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800740
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200741 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800742
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300743 BUG_ON(!device);
744
745 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200746 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300747
Sagi Grimberg19e20902014-12-02 16:57:26 +0200748 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300749 if (isert_conn->cm_id)
750 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200751
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300752 if (isert_conn->qp) {
753 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200754
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300755 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300756 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800757 }
758
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200759 if (isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300760 isert_free_login_buf(isert_conn);
761
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300762 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800763
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300764 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800765}
766
767static void
768isert_connected_handler(struct rdma_cm_id *cma_id)
769{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200770 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300771 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300772
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200773 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200774
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300775 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300776 isert_conn->state = ISER_CONN_UP;
777 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300778 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300779
780 mutex_lock(&isert_np->mutex);
781 list_move_tail(&isert_conn->node, &isert_np->pending);
782 mutex_unlock(&isert_np->mutex);
783
784 isert_info("np %p: Allow accept_np to continue\n", isert_np);
785 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800786}
787
788static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300789isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800790{
791 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300792 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800793
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200794 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
795 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800796
797 isert_connect_release(isert_conn);
798}
799
800static void
801isert_put_conn(struct isert_conn *isert_conn)
802{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300803 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800804}
805
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200806static void
807isert_handle_unbound_conn(struct isert_conn *isert_conn)
808{
809 struct isert_np *isert_np = isert_conn->cm_id->context;
810
811 mutex_lock(&isert_np->mutex);
812 if (!list_empty(&isert_conn->node)) {
813 /*
814 * This means iscsi doesn't know this connection
815 * so schedule a cleanup ourselves
816 */
817 list_del_init(&isert_conn->node);
818 isert_put_conn(isert_conn);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200819 queue_work(isert_release_wq, &isert_conn->release_work);
820 }
821 mutex_unlock(&isert_np->mutex);
822}
823
Sagi Grimberg954f2372014-12-02 16:57:17 +0200824/**
825 * isert_conn_terminate() - Initiate connection termination
826 * @isert_conn: isert connection struct
827 *
828 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200829 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200830 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200831 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200832 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300833 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200834 * safe to call multiple times.
835 */
836static void
837isert_conn_terminate(struct isert_conn *isert_conn)
838{
839 int err;
840
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200841 if (isert_conn->state >= ISER_CONN_TERMINATING)
842 return;
843
844 isert_info("Terminating conn %p state %d\n",
845 isert_conn, isert_conn->state);
846 isert_conn->state = ISER_CONN_TERMINATING;
847 err = rdma_disconnect(isert_conn->cm_id);
848 if (err)
849 isert_warn("Failed rdma_disconnect isert_conn %p\n",
850 isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200851}
852
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700853static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200854isert_np_cma_handler(struct isert_np *isert_np,
855 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800856{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300857 isert_dbg("%s (%d): isert np %p\n",
858 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200859
860 switch (event) {
861 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300862 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200863 break;
864 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300865 isert_np->cm_id = isert_setup_id(isert_np);
866 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200867 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300868 isert_np, PTR_ERR(isert_np->cm_id));
869 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200870 }
871 break;
872 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200873 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200874 isert_np, event);
875 }
876
877 return -1;
878}
879
880static int
881isert_disconnected_handler(struct rdma_cm_id *cma_id,
882 enum rdma_cm_event_type event)
883{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200884 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700885
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300886 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200887 switch (isert_conn->state) {
888 case ISER_CONN_TERMINATING:
889 break;
890 case ISER_CONN_UP:
891 isert_conn_terminate(isert_conn);
892 isert_wait4flush(isert_conn);
893 isert_handle_unbound_conn(isert_conn);
894 break;
895 case ISER_CONN_BOUND:
896 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
897 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
898 break;
899 default:
900 isert_warn("conn %p teminating in state %d\n",
901 isert_conn, isert_conn->state);
902 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300903 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200904
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700905 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800906}
907
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300908static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200909isert_connect_error(struct rdma_cm_id *cma_id)
910{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200911 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200912
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300913 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300914 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200915 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300916
917 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200918}
919
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800920static int
921isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
922{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200923 struct isert_np *isert_np = cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800924 int ret = 0;
925
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300926 isert_info("%s (%d): status %d id %p np %p\n",
927 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200928 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800929
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200930 if (isert_np->cm_id == cma_id)
931 return isert_np_cma_handler(cma_id->context, event->event);
932
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 switch (event->event) {
934 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800935 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700936 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200937 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800938 break;
939 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800940 isert_connected_handler(cma_id);
941 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300942 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
943 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
944 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300945 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200946 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800947 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200948 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
949 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800950 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300951 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200952 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800953 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200954 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800955 break;
956 }
957
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800958 return ret;
959}
960
961static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300962isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800963{
964 struct ib_recv_wr *rx_wr, *rx_wr_failed;
965 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800966 struct iser_rx_desc *rx_desc;
967
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300968 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300969 rx_desc = &isert_conn->rx_descs[i];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200970
971 rx_wr->wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300972 rx_wr->sg_list = &rx_desc->rx_sg;
973 rx_wr->num_sge = 1;
974 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800975 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800976 rx_wr--;
977 rx_wr->next = NULL; /* mark end of work requests list */
978
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300979 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300980 &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200981 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200982 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300983
984 return ret;
985}
986
987static int
988isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
989{
990 struct ib_recv_wr *rx_wr_failed, rx_wr;
991 int ret;
992
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200993 rx_wr.wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300994 rx_wr.sg_list = &rx_desc->rx_sg;
995 rx_wr.num_sge = 1;
996 rx_wr.next = NULL;
997
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300998 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200999 if (ret)
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001000 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001001
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001002 return ret;
1003}
1004
1005static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001006isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001007{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001008 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001009 struct ib_send_wr send_wr, *send_wr_failed;
1010 int ret;
1011
1012 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1013 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1014
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001015 tx_desc->tx_cqe.done = isert_login_send_done;
1016
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001017 send_wr.next = NULL;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001018 send_wr.wr_cqe = &tx_desc->tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001019 send_wr.sg_list = tx_desc->tx_sg;
1020 send_wr.num_sge = tx_desc->num_sge;
1021 send_wr.opcode = IB_WR_SEND;
1022 send_wr.send_flags = IB_SEND_SIGNALED;
1023
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001024 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001025 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001026 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001027
1028 return ret;
1029}
1030
1031static void
1032isert_create_send_desc(struct isert_conn *isert_conn,
1033 struct isert_cmd *isert_cmd,
1034 struct iser_tx_desc *tx_desc)
1035{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001036 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001037 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001038
1039 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1040 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1041
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001042 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1043 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001044
1045 tx_desc->num_sge = 1;
1046 tx_desc->isert_cmd = isert_cmd;
1047
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001048 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1049 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001050 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001051 }
1052}
1053
1054static int
1055isert_init_tx_hdrs(struct isert_conn *isert_conn,
1056 struct iser_tx_desc *tx_desc)
1057{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001058 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001059 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001060 u64 dma_addr;
1061
1062 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1063 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1064 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001065 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001066 return -ENOMEM;
1067 }
1068
1069 tx_desc->dma_addr = dma_addr;
1070 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1071 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001072 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001073
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001074 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1075 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1076 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001077
1078 return 0;
1079}
1080
1081static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001082isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001083 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001084{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001085 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1086
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001087 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001088 tx_desc->tx_cqe.done = isert_send_done;
1089 send_wr->wr_cqe = &tx_desc->tx_cqe;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001090
1091 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1092 send_wr->opcode = IB_WR_SEND_WITH_INV;
1093 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1094 } else {
1095 send_wr->opcode = IB_WR_SEND;
1096 }
1097
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001098 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001099 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001100 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001101}
1102
1103static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001104isert_login_post_recv(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001105{
1106 struct ib_recv_wr rx_wr, *rx_wr_fail;
1107 struct ib_sge sge;
1108 int ret;
1109
1110 memset(&sge, 0, sizeof(struct ib_sge));
1111 sge.addr = isert_conn->login_req_dma;
Christoph Hellwiged1083b2016-02-24 19:24:04 +02001112 sge.length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001113 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001114
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001115 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001116 sge.addr, sge.length, sge.lkey);
1117
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001118 isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
1119
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001120 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001121 rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001122 rx_wr.sg_list = &sge;
1123 rx_wr.num_sge = 1;
1124
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001125 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +02001126 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001127 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001128
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001129 return ret;
1130}
1131
1132static int
1133isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1134 u32 length)
1135{
1136 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001137 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001138 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001139 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001140 int ret;
1141
1142 isert_create_send_desc(isert_conn, NULL, tx_desc);
1143
1144 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1145 sizeof(struct iscsi_hdr));
1146
1147 isert_init_tx_hdrs(isert_conn, tx_desc);
1148
1149 if (length > 0) {
1150 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1151
1152 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1153 length, DMA_TO_DEVICE);
1154
1155 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1156
1157 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1158 length, DMA_TO_DEVICE);
1159
1160 tx_dsg->addr = isert_conn->login_rsp_dma;
1161 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001162 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001163 tx_desc->num_sge = 2;
1164 }
1165 if (!login->login_failed) {
1166 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001167 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001168 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001169 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001170 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001171 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001172 " fastreg pool\n", isert_conn);
1173 return ret;
1174 }
1175 }
1176
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001177 ret = isert_alloc_rx_descriptors(isert_conn);
1178 if (ret)
1179 return ret;
1180
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001181 ret = isert_post_recvm(isert_conn,
1182 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001183 if (ret)
1184 return ret;
1185
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001186 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001187 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001188 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001189 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001190 goto post_send;
1191 }
1192
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001193 ret = isert_login_post_recv(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001194 if (ret)
1195 return ret;
1196 }
1197post_send:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001198 ret = isert_login_post_send(isert_conn, tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001199 if (ret)
1200 return ret;
1201
1202 return 0;
1203}
1204
1205static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001206isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001207{
Christoph Hellwig5adabdd2016-02-24 19:24:05 +02001208 struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001209 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001210 struct iscsi_conn *conn = isert_conn->conn;
1211 struct iscsi_login *login = conn->conn_login;
1212 int size;
1213
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001214 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001215
1216 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001217
1218 if (login->first_request) {
1219 struct iscsi_login_req *login_req =
1220 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1221 /*
1222 * Setup the initial iscsi_login values from the leading
1223 * login request PDU.
1224 */
1225 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1226 login->current_stage =
1227 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1228 >> 2;
1229 login->version_min = login_req->min_version;
1230 login->version_max = login_req->max_version;
1231 memcpy(login->isid, login_req->isid, 6);
1232 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1233 login->init_task_tag = login_req->itt;
1234 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1235 login->cid = be16_to_cpu(login_req->cid);
1236 login->tsih = be16_to_cpu(login_req->tsih);
1237 }
1238
1239 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1240
1241 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001242 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1243 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1244 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001245 memcpy(login->req_buf, &rx_desc->data[0], size);
1246
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001247 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001248 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001249 return;
1250 }
1251 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252}
1253
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001255*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001256{
Sagi Grimberg67004252015-03-29 15:52:09 +03001257 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001258 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001259 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001260
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001261 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001262 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001263 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264 return NULL;
1265 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001266 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001267 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001268 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001269 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001270
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001271 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001272}
1273
1274static int
1275isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001276 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1277 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001278{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001279 struct iscsi_conn *conn = isert_conn->conn;
1280 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001281 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1282 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001283 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001284
1285 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1286 if (rc < 0)
1287 return rc;
1288
1289 imm_data = cmd->immediate_data;
1290 imm_data_len = cmd->first_burst_len;
1291 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001292 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001294 if (imm_data && imm_data_len == data_len)
1295 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001296 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1297 if (rc < 0) {
1298 return 0;
1299 } else if (rc > 0) {
1300 dump_payload = true;
1301 goto sequence_cmd;
1302 }
1303
1304 if (!imm_data)
1305 return 0;
1306
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001307 if (imm_data_len != data_len) {
1308 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1309 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1310 &rx_desc->data[0], imm_data_len);
1311 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1312 sg_nents, imm_data_len);
1313 } else {
1314 sg_init_table(&isert_cmd->sg, 1);
1315 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1316 cmd->se_cmd.t_data_nents = 1;
1317 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1318 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1319 imm_data_len);
1320 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001321
1322 cmd->write_data_done += imm_data_len;
1323
1324 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1325 spin_lock_bh(&cmd->istate_lock);
1326 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1327 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1328 spin_unlock_bh(&cmd->istate_lock);
1329 }
1330
1331sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001332 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001333
1334 if (!rc && dump_payload == false && unsol_data)
1335 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001336 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001337 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001338
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001339 return 0;
1340}
1341
1342static int
1343isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1344 struct iser_rx_desc *rx_desc, unsigned char *buf)
1345{
1346 struct scatterlist *sg_start;
1347 struct iscsi_conn *conn = isert_conn->conn;
1348 struct iscsi_cmd *cmd = NULL;
1349 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1350 u32 unsol_data_len = ntoh24(hdr->dlength);
1351 int rc, sg_nents, sg_off, page_off;
1352
1353 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1354 if (rc < 0)
1355 return rc;
1356 else if (!cmd)
1357 return 0;
1358 /*
1359 * FIXME: Unexpected unsolicited_data out
1360 */
1361 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001362 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001363 dump_stack();
1364 return -1;
1365 }
1366
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001367 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1368 "write_data_done: %u, data_length: %u\n",
1369 unsol_data_len, cmd->write_data_done,
1370 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001371
1372 sg_off = cmd->write_data_done / PAGE_SIZE;
1373 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1374 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1375 page_off = cmd->write_data_done % PAGE_SIZE;
1376 /*
1377 * FIXME: Non page-aligned unsolicited_data out
1378 */
1379 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001380 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001381 dump_stack();
1382 return -1;
1383 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001384 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1385 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1386 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001387
1388 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1389 unsol_data_len);
1390
1391 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1392 if (rc < 0)
1393 return rc;
1394
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001395 /*
1396 * multiple data-outs on the same command can arrive -
1397 * so post the buffer before hand
1398 */
1399 rc = isert_post_recv(isert_conn, rx_desc);
1400 if (rc) {
1401 isert_err("ib_post_recv failed with %d\n", rc);
1402 return rc;
1403 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001404 return 0;
1405}
1406
1407static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001408isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001409 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1410 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001411{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001412 struct iscsi_conn *conn = isert_conn->conn;
1413 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1414 int rc;
1415
1416 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1417 if (rc < 0)
1418 return rc;
1419 /*
1420 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1421 */
1422
1423 return iscsit_process_nop_out(conn, cmd, hdr);
1424}
1425
1426static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001427isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001428 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1429 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001430{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001431 struct iscsi_conn *conn = isert_conn->conn;
1432 u32 payload_length = ntoh24(hdr->dlength);
1433 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001434 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001435
1436 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1437 if (rc < 0)
1438 return rc;
1439
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001440 if (payload_length) {
1441 text_in = kzalloc(payload_length, GFP_KERNEL);
1442 if (!text_in) {
1443 isert_err("Unable to allocate text_in of payload_length: %u\n",
1444 payload_length);
1445 return -ENOMEM;
1446 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001447 }
1448 cmd->text_in_ptr = text_in;
1449
1450 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1451
1452 return iscsit_process_text_cmd(conn, cmd, hdr);
1453}
1454
1455static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001456isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1457 uint32_t read_stag, uint64_t read_va,
1458 uint32_t write_stag, uint64_t write_va)
1459{
1460 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1461 struct iscsi_conn *conn = isert_conn->conn;
1462 struct iscsi_cmd *cmd;
1463 struct isert_cmd *isert_cmd;
1464 int ret = -EINVAL;
1465 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1466
Sagi Grimbergfb140272015-03-29 15:52:08 +03001467 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001468 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001469 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001470 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001471 return 0;
1472 }
1473
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474 switch (opcode) {
1475 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001476 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001477 if (!cmd)
1478 break;
1479
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001480 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001481 isert_cmd->read_stag = read_stag;
1482 isert_cmd->read_va = read_va;
1483 isert_cmd->write_stag = write_stag;
1484 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001485 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001486
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001487 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001488 rx_desc, (unsigned char *)hdr);
1489 break;
1490 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001491 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001492 if (!cmd)
1493 break;
1494
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001495 isert_cmd = iscsit_priv_cmd(cmd);
1496 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001497 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001498 break;
1499 case ISCSI_OP_SCSI_DATA_OUT:
1500 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1501 (unsigned char *)hdr);
1502 break;
1503 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001504 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001505 if (!cmd)
1506 break;
1507
1508 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1509 (unsigned char *)hdr);
1510 break;
1511 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001512 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001513 if (!cmd)
1514 break;
1515
1516 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001517 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001518 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001519 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001520 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001521 else
1522 cmd = isert_allocate_cmd(conn, rx_desc);
1523
1524 if (!cmd)
1525 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001526
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001527 isert_cmd = iscsit_priv_cmd(cmd);
1528 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001529 rx_desc, (struct iscsi_text *)hdr);
1530 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001531 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001532 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001533 dump_stack();
1534 break;
1535 }
1536
1537 return ret;
1538}
1539
1540static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001541isert_print_wc(struct ib_wc *wc, const char *type)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001542{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001543 if (wc->status != IB_WC_WR_FLUSH_ERR)
1544 isert_err("%s failure: %s (%d) vend_err %x\n", type,
1545 ib_wc_status_msg(wc->status), wc->status,
1546 wc->vendor_err);
1547 else
1548 isert_dbg("%s failure: %s (%d)\n", type,
1549 ib_wc_status_msg(wc->status), wc->status);
1550}
1551
1552static void
1553isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1554{
1555 struct isert_conn *isert_conn = wc->qp->qp_context;
1556 struct ib_device *ib_dev = isert_conn->cm_id->device;
1557 struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
1558 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001559 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001560 uint64_t read_va = 0, write_va = 0;
1561 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001562
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001563 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1564 isert_print_wc(wc, "recv");
1565 if (wc->status != IB_WC_WR_FLUSH_ERR)
1566 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1567 return;
1568 }
1569
1570 ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
1571 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1572
1573 isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1574 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
1575 (int)(wc->byte_len - ISER_HEADERS_LEN));
1576
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001577 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001578 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001579 if (iser_ctrl->flags & ISER_RSV) {
1580 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1581 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001582 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1583 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001584 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001585 if (iser_ctrl->flags & ISER_WSV) {
1586 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1587 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001588 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1589 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590 }
1591
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001592 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593 break;
1594 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001595 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 break;
1597 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001598 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599 break;
1600 }
1601
Bart Van Assche6c760b32015-10-22 11:14:18 -07001602 isert_rx_opcode(isert_conn, rx_desc,
1603 read_stag, read_va, write_stag, write_va);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001604
1605 ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
1606 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001607}
1608
1609static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001610isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001612 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001613 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001614
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001615 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1616 isert_print_wc(wc, "login recv");
1617 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001618 }
1619
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001620 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
1621 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001622
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001623 isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001624
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001625 if (isert_conn->conn) {
1626 struct iscsi_login *login = isert_conn->conn->conn_login;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001627
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001628 if (login && !login->first_request)
1629 isert_rx_login_req(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001630 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001631
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001632 mutex_lock(&isert_conn->mutex);
1633 complete(&isert_conn->login_req_comp);
1634 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001635
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001636 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
1637 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638}
1639
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001640static int
1641isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1642 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1643 enum iser_ib_op_code op, struct isert_data_buf *data)
1644{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001645 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001646
1647 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1648 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1649
1650 data->len = length - offset;
1651 data->offset = offset;
1652 data->sg_off = data->offset / PAGE_SIZE;
1653
1654 data->sg = &sg[data->sg_off];
1655 data->nents = min_t(unsigned int, nents - data->sg_off,
1656 ISCSI_ISER_SG_TABLESIZE);
1657 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1658 PAGE_SIZE);
1659
1660 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1661 data->dma_dir);
1662 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001663 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001664 return -EINVAL;
1665 }
1666
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001667 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001668 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001669
1670 return 0;
1671}
1672
1673static void
1674isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1675{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001676 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001677
1678 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1679 memset(data, 0, sizeof(*data));
1680}
1681
1682
1683
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001684static void
1685isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1686{
1687 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001688
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001689 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001690
1691 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001692 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001693 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001694 }
1695
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001696 if (wr->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001697 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001698 kfree(wr->rdma_wr);
1699 wr->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001700 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701
Vu Pham90ecc6e2013-08-28 23:23:33 +03001702 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001703 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001704 kfree(wr->ib_sge);
1705 wr->ib_sge = NULL;
1706 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707}
1708
1709static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001710isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001711{
1712 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001713
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001714 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001715
1716 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001717 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001718 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1719 isert_unmap_data_buf(isert_conn, &wr->prot);
1720 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1721 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001722 spin_lock_bh(&isert_conn->pool_lock);
1723 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1724 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001725 wr->fr_desc = NULL;
1726 }
1727
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001728 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001729 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001730 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001731 }
1732
1733 wr->ib_sge = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001734 wr->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001735}
1736
1737static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001738isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001740 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001741 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001742 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001743 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001744 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001746 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747
1748 switch (cmd->iscsi_opcode) {
1749 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 spin_lock_bh(&conn->cmd_lock);
1751 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001752 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753 spin_unlock_bh(&conn->cmd_lock);
1754
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001755 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001756 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001757 /*
1758 * Check for special case during comp_err where
1759 * WRITE_PENDING has been handed off from core,
1760 * but requires an extra target_put_sess_cmd()
1761 * before transport_generic_free_cmd() below.
1762 */
1763 if (comp_err &&
1764 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1765 struct se_cmd *se_cmd = &cmd->se_cmd;
1766
Bart Van Asscheafc16602015-04-27 13:52:36 +02001767 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001768 }
1769 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001770
Vu Phamd40945d2013-08-28 23:23:34 +03001771 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001772 transport_generic_free_cmd(&cmd->se_cmd, 0);
1773 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001774 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001775 spin_lock_bh(&conn->cmd_lock);
1776 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001777 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001778 spin_unlock_bh(&conn->cmd_lock);
1779
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001780 transport_generic_free_cmd(&cmd->se_cmd, 0);
1781 break;
1782 case ISCSI_OP_REJECT:
1783 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001784 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001785 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1786 /* If the continue bit is on, keep the command alive */
1787 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1788 break;
1789
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001790 spin_lock_bh(&conn->cmd_lock);
1791 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001792 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001793 spin_unlock_bh(&conn->cmd_lock);
1794
1795 /*
1796 * Handle special case for REJECT when iscsi_add_reject*() has
1797 * overwritten the original iscsi_opcode assignment, and the
1798 * associated cmd->se_cmd needs to be released.
1799 */
1800 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001801 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001802 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803 transport_generic_free_cmd(&cmd->se_cmd, 0);
1804 break;
1805 }
1806 /*
1807 * Fall-through
1808 */
1809 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001810 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001811 break;
1812 }
1813}
1814
1815static void
1816isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1817{
1818 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001819 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001820 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1821 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1822 tx_desc->dma_addr = 0;
1823 }
1824}
1825
1826static void
1827isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001828 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001829{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001830 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001831 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001832 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1833 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1834 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835 }
1836
1837 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001838 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001839}
1840
Sagi Grimberg96b79732014-03-17 12:52:18 +02001841static int
1842isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1843{
1844 struct ib_mr_status mr_status;
1845 int ret;
1846
1847 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1848 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001849 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001850 goto fail_mr_status;
1851 }
1852
1853 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1854 u64 sec_offset_err;
1855 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1856
1857 switch (mr_status.sig_err.err_type) {
1858 case IB_SIG_BAD_GUARD:
1859 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1860 break;
1861 case IB_SIG_BAD_REFTAG:
1862 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1863 break;
1864 case IB_SIG_BAD_APPTAG:
1865 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1866 break;
1867 }
1868 sec_offset_err = mr_status.sig_err.sig_err_offset;
1869 do_div(sec_offset_err, block_size);
1870 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1871
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001872 isert_err("PI error found type %d at sector 0x%llx "
1873 "expected 0x%x vs actual 0x%x\n",
1874 mr_status.sig_err.err_type,
1875 (unsigned long long)se_cmd->bad_sector,
1876 mr_status.sig_err.expected,
1877 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001878 ret = 1;
1879 }
1880
1881fail_mr_status:
1882 return ret;
1883}
1884
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001885static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001886isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001887{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001888 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001889 struct isert_device *device = isert_conn->device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001890 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1891 struct isert_cmd *isert_cmd = desc->isert_cmd;
1892 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1893 struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001894 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001895
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001896 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1897 isert_print_wc(wc, "rdma write");
1898 if (wc->status != IB_WC_WR_FLUSH_ERR)
1899 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1900 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1901 return;
1902 }
1903
1904 isert_dbg("Cmd %p\n", isert_cmd);
1905
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001906 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001907 ret = isert_check_pi_status(cmd, wr->fr_desc->pi_ctx->sig_mr);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001908 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001909 }
1910
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001911 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001912 wr->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001913 if (ret)
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001914 transport_send_check_condition_and_sense(cmd, cmd->pi_err, 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001915 else
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001916 isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001917}
1918
1919static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001920isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001922 struct isert_conn *isert_conn = wc->qp->qp_context;
1923 struct isert_device *device = isert_conn->device;
1924 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1925 struct isert_cmd *isert_cmd = desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001926 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001927 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001928 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001929 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001930
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001931 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1932 isert_print_wc(wc, "rdma read");
1933 if (wc->status != IB_WC_WR_FLUSH_ERR)
1934 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1935 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1936 return;
1937 }
1938
1939 isert_dbg("Cmd %p\n", isert_cmd);
1940
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001941 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001942 ret = isert_check_pi_status(se_cmd,
1943 wr->fr_desc->pi_ctx->sig_mr);
1944 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001945 }
1946
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001947 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001948 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001949 cmd->write_data_done = wr->data.len;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001950 wr->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001951
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001952 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001953 spin_lock_bh(&cmd->istate_lock);
1954 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1955 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1956 spin_unlock_bh(&cmd->istate_lock);
1957
Sagi Grimberg364189f2015-03-29 15:52:03 +03001958 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001959 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001960 transport_send_check_condition_and_sense(se_cmd,
1961 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001962 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001963 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001964 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001965}
1966
1967static void
1968isert_do_control_comp(struct work_struct *work)
1969{
1970 struct isert_cmd *isert_cmd = container_of(work,
1971 struct isert_cmd, comp_work);
1972 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001973 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001974 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001975
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001976 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1977
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001978 switch (cmd->i_state) {
1979 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001980 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001981 case ISTATE_SEND_REJECT: /* FALLTHRU */
1982 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001983 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001984 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1985 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001986 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001987 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001988 iscsit_logout_post_handler(cmd, cmd->conn);
1989 break;
1990 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001991 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001992 dump_stack();
1993 break;
1994 }
1995}
1996
1997static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001998isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001999{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002000 struct isert_conn *isert_conn = wc->qp->qp_context;
2001 struct ib_device *ib_dev = isert_conn->cm_id->device;
2002 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002004 if (unlikely(wc->status != IB_WC_SUCCESS)) {
2005 isert_print_wc(wc, "login send");
2006 if (wc->status != IB_WC_WR_FLUSH_ERR)
2007 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2008 }
2009
2010 isert_unmap_tx_desc(tx_desc, ib_dev);
2011}
2012
2013static void
2014isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
2015{
2016 struct isert_conn *isert_conn = wc->qp->qp_context;
2017 struct ib_device *ib_dev = isert_conn->cm_id->device;
2018 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
2019 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2020
2021 if (unlikely(wc->status != IB_WC_SUCCESS)) {
2022 isert_print_wc(wc, "send");
2023 if (wc->status != IB_WC_WR_FLUSH_ERR)
2024 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2025 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2026 return;
2027 }
2028
2029 isert_dbg("Cmd %p\n", isert_cmd);
2030
2031 switch (isert_cmd->iscsi_cmd->i_state) {
2032 case ISTATE_SEND_TASKMGTRSP:
2033 case ISTATE_SEND_LOGOUTRSP:
2034 case ISTATE_SEND_REJECT:
2035 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002036 isert_unmap_tx_desc(tx_desc, ib_dev);
2037
2038 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
2039 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2040 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002041 default:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002042 isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
2043 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002044 break;
2045 }
2046}
2047
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002048static int
2049isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2050{
2051 struct ib_send_wr *wr_failed;
2052 int ret;
2053
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002054 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2055 if (ret) {
2056 isert_err("ib_post_recv failed with %d\n", ret);
2057 return ret;
2058 }
2059
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002060 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002061 &wr_failed);
2062 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002063 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002064 return ret;
2065 }
2066 return ret;
2067}
2068
2069static int
2070isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2071{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002072 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002073 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002074 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2075 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2076 &isert_cmd->tx_desc.iscsi_header;
2077
2078 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2079 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2080 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2081 /*
2082 * Attach SENSE DATA payload to iSCSI Response PDU
2083 */
2084 if (cmd->se_cmd.sense_buffer &&
2085 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2086 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002087 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002088 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002089 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002090 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091
2092 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2093 cmd->sense_buffer);
2094 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2095
2096 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2097 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002098 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002099
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002100 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2101 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002102 DMA_TO_DEVICE);
2103
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002104 isert_cmd->pdu_buf_len = pdu_len;
2105 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2106 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002107 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108 isert_cmd->tx_desc.num_sge = 2;
2109 }
2110
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002111 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002112
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002113 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002114
2115 return isert_post_response(isert_conn, isert_cmd);
2116}
2117
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002118static void
2119isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2120{
2121 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002122 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002123 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002124
2125 spin_lock_bh(&conn->cmd_lock);
2126 if (!list_empty(&cmd->i_conn_node))
2127 list_del_init(&cmd->i_conn_node);
2128 spin_unlock_bh(&conn->cmd_lock);
2129
2130 if (cmd->data_direction == DMA_TO_DEVICE)
2131 iscsit_stop_dataout_timer(cmd);
2132
2133 device->unreg_rdma_mem(isert_cmd, isert_conn);
2134}
2135
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002136static enum target_prot_op
2137isert_get_sup_prot_ops(struct iscsi_conn *conn)
2138{
Sagi Grimberg67004252015-03-29 15:52:09 +03002139 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002140 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002141
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002142 if (conn->tpg->tpg_attrib.t10_pi) {
2143 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002144 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002145 isert_conn->pi_support = true;
2146 return TARGET_PROT_ALL;
2147 }
2148 }
2149
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002150 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002151 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002152
2153 return TARGET_PROT_NORMAL;
2154}
2155
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002156static int
2157isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2158 bool nopout_response)
2159{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002160 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002161 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002162 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2163
2164 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2165 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2166 &isert_cmd->tx_desc.iscsi_header,
2167 nopout_response);
2168 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002169 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002170
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002171 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002172
2173 return isert_post_response(isert_conn, isert_cmd);
2174}
2175
2176static int
2177isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2178{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002179 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002180 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002181 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2182
2183 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2184 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2185 &isert_cmd->tx_desc.iscsi_header);
2186 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002187 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002188
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002189 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002190
2191 return isert_post_response(isert_conn, isert_cmd);
2192}
2193
2194static int
2195isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2196{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002197 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002198 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2200
2201 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2202 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2203 &isert_cmd->tx_desc.iscsi_header);
2204 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002205 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002206
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002207 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002208
2209 return isert_post_response(isert_conn, isert_cmd);
2210}
2211
2212static int
2213isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2214{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002215 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002216 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002217 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002218 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002219 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002220 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2221 struct iscsi_reject *hdr =
2222 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002223
2224 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002225 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002226 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002227
2228 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002229 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002230 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2231 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002232 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2233 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002234 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002235 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002236 isert_cmd->tx_desc.num_sge = 2;
2237
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002238 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002239
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002240 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002241
2242 return isert_post_response(isert_conn, isert_cmd);
2243}
2244
2245static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002246isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2247{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002248 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002249 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002250 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2251 struct iscsi_text_rsp *hdr =
2252 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2253 u32 txt_rsp_len;
2254 int rc;
2255
2256 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002257 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002258 if (rc < 0)
2259 return rc;
2260
2261 txt_rsp_len = rc;
2262 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2263
2264 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002265 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002266 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002267 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2268 void *txt_rsp_buf = cmd->buf_ptr;
2269
2270 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2271 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2272
2273 isert_cmd->pdu_buf_len = txt_rsp_len;
2274 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2275 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002276 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002277 isert_cmd->tx_desc.num_sge = 2;
2278 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002279 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002280
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002281 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002282
2283 return isert_post_response(isert_conn, isert_cmd);
2284}
2285
2286static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002287isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002288 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289 u32 data_left, u32 offset)
2290{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002291 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002292 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002293 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002294 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002295 u32 sg_off, page_off;
2296 int i = 0, sg_nents;
2297
2298 sg_off = offset / PAGE_SIZE;
2299 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2300 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2301 page_off = offset % PAGE_SIZE;
2302
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002303 rdma_wr->wr.sg_list = ib_sge;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002304 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
2305
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002306 /*
2307 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2308 */
2309 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002310 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2311 "page_off: %u\n",
2312 (unsigned long long)tmp_sg->dma_address,
2313 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314
2315 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2316 ib_sge->length = min_t(u32, data_left,
2317 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002318 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002319
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002320 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2321 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002322 page_off = 0;
2323 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002324 if (!data_left)
2325 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002326 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002327 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002328 }
2329
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002330 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002331 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002332 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002333
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002334 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002335}
2336
2337static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002338isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2339 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340{
2341 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002342 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002343 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002344 struct isert_data_buf *data = &wr->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002345 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002346 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002347 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2348 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002350 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002351
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002352 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2353 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2354 se_cmd->t_data_nents, se_cmd->data_length,
2355 offset, wr->iser_ib_op, &wr->data);
2356 if (ret)
2357 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002358
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002359 data_left = data->len;
2360 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002361
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002362 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002363 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002364 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002365 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002366 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002367 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002368 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002370 wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2371 wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002372 GFP_KERNEL);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002373 if (!wr->rdma_wr) {
2374 isert_dbg("Unable to allocate wr->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002375 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002376 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002377 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002378
2379 wr->isert_cmd = isert_cmd;
2380 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002381
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002382 for (i = 0; i < wr->rdma_wr_num; i++) {
2383 rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002384 data_len = min(data_left, rdma_write_max);
2385
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002386 rdma_wr->wr.send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002387 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002388 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2389
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002390 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2391 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2392 rdma_wr->rkey = isert_cmd->read_stag;
2393 if (i + 1 == wr->rdma_wr_num)
2394 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002395 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002396 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002397 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002398 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2399
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002400 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2401 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2402 rdma_wr->rkey = isert_cmd->write_stag;
2403 if (i + 1 == wr->rdma_wr_num)
2404 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002405 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002406 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002407 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002408
2409 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002410 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411 ib_sge += ib_sge_cnt;
2412
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002413 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002414 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002415 data_left -= data_len;
2416 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002417
2418 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002419unmap_cmd:
2420 isert_unmap_data_buf(isert_conn, data);
2421
Vu Pham90ecc6e2013-08-28 23:23:33 +03002422 return ret;
2423}
2424
Sagi Grimberg10633c32014-12-07 13:12:04 +02002425static inline void
2426isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2427{
2428 u32 rkey;
2429
2430 memset(inv_wr, 0, sizeof(*inv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002431 inv_wr->wr_cqe = NULL;
Sagi Grimberg10633c32014-12-07 13:12:04 +02002432 inv_wr->opcode = IB_WR_LOCAL_INV;
2433 inv_wr->ex.invalidate_rkey = mr->rkey;
2434
2435 /* Bump the key */
2436 rkey = ib_inc_rkey(mr->rkey);
2437 ib_update_fast_reg_key(mr, rkey);
2438}
2439
Vu Pham59464ef2013-08-28 23:23:35 +03002440static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002441isert_fast_reg_mr(struct isert_conn *isert_conn,
2442 struct fast_reg_descriptor *fr_desc,
2443 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002444 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002445 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002446{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002447 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002448 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002449 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002450 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002451 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002452 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002453
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002454 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002455 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002456 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2457 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002458 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2459 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002460 return 0;
2461 }
2462
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002463 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002464 /* Registering data buffer */
2465 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002466 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002467 /* Registering protection buffer */
2468 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002469
Sagi Grimberg10633c32014-12-07 13:12:04 +02002470 if (!(fr_desc->ind & ind)) {
2471 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002472 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002473 }
2474
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002475 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2476 if (unlikely(n != mem->nents)) {
2477 isert_err("failed to map mr sg (%d/%d)\n",
2478 n, mem->nents);
2479 return n < 0 ? n : -EINVAL;
2480 }
2481
2482 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2483 fr_desc, mem->nents, mem->offset);
2484
2485 reg_wr.wr.next = NULL;
2486 reg_wr.wr.opcode = IB_WR_REG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002487 reg_wr.wr.wr_cqe = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002488 reg_wr.wr.send_flags = 0;
2489 reg_wr.wr.num_sge = 0;
2490 reg_wr.mr = mr;
2491 reg_wr.key = mr->lkey;
2492 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002493
2494 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002495 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002496 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002497 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002498
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002499 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002500 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002501 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002502 return ret;
2503 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002504 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002505
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002506 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002507 sge->addr = mr->iova;
2508 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002509
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002510 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2511 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002512
2513 return ret;
2514}
2515
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002516static inline void
2517isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2518 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002519{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002520 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002521 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2522 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2523 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002524 /*
2525 * At the moment we hard code those, but if in the future
2526 * the target core would like to use it, we will take it
2527 * from se_cmd.
2528 */
2529 domain->sig.dif.apptag_check_mask = 0xffff;
2530 domain->sig.dif.app_escape = true;
2531 domain->sig.dif.ref_escape = true;
2532 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2533 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2534 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002535};
2536
2537static int
2538isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2539{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002540 switch (se_cmd->prot_op) {
2541 case TARGET_PROT_DIN_INSERT:
2542 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002543 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002544 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002545 break;
2546 case TARGET_PROT_DOUT_INSERT:
2547 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002548 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002549 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002550 break;
2551 case TARGET_PROT_DIN_PASS:
2552 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002553 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2554 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002555 break;
2556 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002557 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002558 return -EINVAL;
2559 }
2560
2561 return 0;
2562}
2563
2564static inline u8
2565isert_set_prot_checks(u8 prot_checks)
2566{
2567 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2568 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2569 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2570}
2571
2572static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002573isert_reg_sig_mr(struct isert_conn *isert_conn,
2574 struct se_cmd *se_cmd,
2575 struct isert_rdma_wr *rdma_wr,
2576 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002577{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002578 struct ib_sig_handover_wr sig_wr;
2579 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002580 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2581 struct ib_sig_attrs sig_attrs;
2582 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002583
2584 memset(&sig_attrs, 0, sizeof(sig_attrs));
2585 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2586 if (ret)
2587 goto err;
2588
2589 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2590
2591 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002592 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002593 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002594 }
2595
2596 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002597 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002598 sig_wr.wr.wr_cqe = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002599 sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2600 sig_wr.wr.num_sge = 1;
2601 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2602 sig_wr.sig_attrs = &sig_attrs;
2603 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002604 if (se_cmd->t_prot_sg)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002605 sig_wr.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002606
2607 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002608 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002609 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002610 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002611
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002612 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002613 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002614 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002615 goto err;
2616 }
2617 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2618
Sagi Grimberg570db172014-12-02 16:57:31 +02002619 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2620 rdma_wr->ib_sg[SIG].addr = 0;
2621 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002622 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2623 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2624 /*
2625 * We have protection guards on the wire
2626 * so we need to set a larget transfer
2627 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002628 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002629
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002630 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002631 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2632 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002633err:
2634 return ret;
2635}
2636
Vu Pham59464ef2013-08-28 23:23:35 +03002637static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002638isert_handle_prot_cmd(struct isert_conn *isert_conn,
2639 struct isert_cmd *isert_cmd,
2640 struct isert_rdma_wr *wr)
2641{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002642 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002643 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2644 int ret;
2645
2646 if (!wr->fr_desc->pi_ctx) {
2647 ret = isert_create_pi_ctx(wr->fr_desc,
2648 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002649 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002650 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002651 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002652 isert_conn);
2653 return ret;
2654 }
2655 }
2656
2657 if (se_cmd->t_prot_sg) {
2658 ret = isert_map_data_buf(isert_conn, isert_cmd,
2659 se_cmd->t_prot_sg,
2660 se_cmd->t_prot_nents,
2661 se_cmd->prot_length,
2662 0, wr->iser_ib_op, &wr->prot);
2663 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002664 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002665 isert_conn);
2666 return ret;
2667 }
2668
2669 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2670 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2671 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2672 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002673 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002674 isert_conn);
2675 goto unmap_prot_cmd;
2676 }
2677 }
2678
2679 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2680 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002681 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002682 isert_conn);
2683 goto unmap_prot_cmd;
2684 }
2685 wr->fr_desc->ind |= ISERT_PROTECTED;
2686
2687 return 0;
2688
2689unmap_prot_cmd:
2690 if (se_cmd->t_prot_sg)
2691 isert_unmap_data_buf(isert_conn, &wr->prot);
2692
2693 return ret;
2694}
2695
2696static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002697isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2698 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002699{
2700 struct se_cmd *se_cmd = &cmd->se_cmd;
2701 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002702 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002703 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002704 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002705 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002706 u32 offset;
2707 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002708 unsigned long flags;
2709
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002710 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2711
2712 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2713 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2714 se_cmd->t_data_nents, se_cmd->data_length,
2715 offset, wr->iser_ib_op, &wr->data);
2716 if (ret)
2717 return ret;
2718
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002719 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002720 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2721 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002722 struct fast_reg_descriptor, list);
2723 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002724 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002725 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002726 }
2727
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002728 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002729 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002730 if (ret)
2731 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002732
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002733 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002734 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002735 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002736 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002737
Sagi Grimberg570db172014-12-02 16:57:31 +02002738 ib_sg = &wr->ib_sg[SIG];
2739 } else {
2740 ib_sg = &wr->ib_sg[DATA];
2741 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002742
Sagi Grimberg570db172014-12-02 16:57:31 +02002743 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002744 wr->ib_sge = &wr->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002745 wr->rdma_wr_num = 1;
2746 memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2747 wr->rdma_wr = &wr->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002748 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002749
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002750 rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2751 rdma_wr->wr.sg_list = &wr->s_ib_sge;
2752 rdma_wr->wr.num_sge = 1;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002753 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
Vu Pham59464ef2013-08-28 23:23:35 +03002754 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002755 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2756
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002757 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2758 rdma_wr->remote_addr = isert_cmd->read_va;
2759 rdma_wr->rkey = isert_cmd->read_stag;
2760 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002761 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002762 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002763 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2764
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002765 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2766 rdma_wr->remote_addr = isert_cmd->write_va;
2767 rdma_wr->rkey = isert_cmd->write_stag;
2768 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002769 }
2770
Vu Pham59464ef2013-08-28 23:23:35 +03002771 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002772
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002773unmap_cmd:
2774 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002775 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2776 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2777 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002778 }
2779 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002780
Vu Pham59464ef2013-08-28 23:23:35 +03002781 return ret;
2782}
2783
2784static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002785isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2786{
2787 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002788 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002789 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002790 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002791 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002792 struct ib_send_wr *wr_failed;
2793 int rc;
2794
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002795 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002796 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002797
Vu Pham90ecc6e2013-08-28 23:23:33 +03002798 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002799 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002800 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002801 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002802 return rc;
2803 }
2804
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002805 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002806 /*
2807 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2808 */
2809 isert_create_send_desc(isert_conn, isert_cmd,
2810 &isert_cmd->tx_desc);
2811 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2812 &isert_cmd->tx_desc.iscsi_header);
2813 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2814 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002815 &isert_cmd->tx_desc.send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002816 isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2817 wr->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002818
2819 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2820 if (rc) {
2821 isert_err("ib_post_recv failed with %d\n", rc);
2822 return rc;
2823 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002824 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002825
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002826 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002827 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002828 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002829
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002830 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002831 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002832 "READ\n", isert_cmd);
2833 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002834 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002835 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002836
Vu Pham90ecc6e2013-08-28 23:23:33 +03002837 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002838}
2839
2840static int
2841isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2842{
2843 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002844 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002845 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002846 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002847 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002848 struct ib_send_wr *wr_failed;
2849 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002850
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002851 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002852 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002853 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002854 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002855 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002856 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002857 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002858 }
2859
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002860 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002861 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002862 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002863
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002864 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002865 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002866
Vu Pham90ecc6e2013-08-28 23:23:33 +03002867 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002868}
2869
2870static int
2871isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2872{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002873 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2874 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002875
2876 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002877 case ISTATE_REMOVE:
2878 spin_lock_bh(&conn->cmd_lock);
2879 list_del_init(&cmd->i_conn_node);
2880 spin_unlock_bh(&conn->cmd_lock);
2881 isert_put_cmd(isert_cmd, true);
2882 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002883 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2884 ret = isert_put_nopin(cmd, conn, false);
2885 break;
2886 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002887 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002888 ret = -EINVAL;
2889 break;
2890 }
2891
2892 return ret;
2893}
2894
2895static int
2896isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2897{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002898 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002899 int ret;
2900
2901 switch (state) {
2902 case ISTATE_SEND_LOGOUTRSP:
2903 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002904 if (!ret)
2905 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002906 break;
2907 case ISTATE_SEND_NOPIN:
2908 ret = isert_put_nopin(cmd, conn, true);
2909 break;
2910 case ISTATE_SEND_TASKMGTRSP:
2911 ret = isert_put_tm_rsp(cmd, conn);
2912 break;
2913 case ISTATE_SEND_REJECT:
2914 ret = isert_put_reject(cmd, conn);
2915 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002916 case ISTATE_SEND_TEXTRSP:
2917 ret = isert_put_text_rsp(cmd, conn);
2918 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002919 case ISTATE_SEND_STATUS:
2920 /*
2921 * Special case for sending non GOOD SCSI status from TX thread
2922 * context during pre se_cmd excecution failure.
2923 */
2924 ret = isert_put_response(conn, cmd);
2925 break;
2926 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002927 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002928 ret = -EINVAL;
2929 break;
2930 }
2931
2932 return ret;
2933}
2934
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002935struct rdma_cm_id *
2936isert_setup_id(struct isert_np *isert_np)
2937{
2938 struct iscsi_np *np = isert_np->np;
2939 struct rdma_cm_id *id;
2940 struct sockaddr *sa;
2941 int ret;
2942
2943 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002944 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002945
Guy Shapirofa201052015-10-22 15:20:10 +03002946 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002947 RDMA_PS_TCP, IB_QPT_RC);
2948 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002949 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002950 ret = PTR_ERR(id);
2951 goto out;
2952 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002953 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002954
2955 ret = rdma_bind_addr(id, sa);
2956 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002957 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002958 goto out_id;
2959 }
2960
Sagi Grimberg992607e2015-03-29 15:52:18 +03002961 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002962 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002963 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002964 goto out_id;
2965 }
2966
2967 return id;
2968out_id:
2969 rdma_destroy_id(id);
2970out:
2971 return ERR_PTR(ret);
2972}
2973
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002974static int
2975isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07002976 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002977{
2978 struct isert_np *isert_np;
2979 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002980 int ret;
2981
2982 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2983 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002984 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002985 return -ENOMEM;
2986 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002987 sema_init(&isert_np->sem, 0);
2988 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002989 INIT_LIST_HEAD(&isert_np->accepted);
2990 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002991 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002992
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002993 /*
2994 * Setup the np->np_sockaddr from the passed sockaddr setup
2995 * in iscsi_target_configfs.c code..
2996 */
2997 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07002998 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002999
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003000 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003001 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003002 ret = PTR_ERR(isert_lid);
3003 goto out;
3004 }
3005
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003006 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003007 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003008
3009 return 0;
3010
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003011out:
3012 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003013
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003014 return ret;
3015}
3016
3017static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003018isert_rdma_accept(struct isert_conn *isert_conn)
3019{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003020 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003021 struct rdma_conn_param cp;
3022 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003023 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003024
3025 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003026 cp.initiator_depth = isert_conn->initiator_depth;
3027 cp.retry_count = 7;
3028 cp.rnr_retry_count = 7;
3029
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003030 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02003031 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3032 if (!isert_conn->snd_w_inv)
3033 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003034 cp.private_data = (void *)&rsp_hdr;
3035 cp.private_data_len = sizeof(rsp_hdr);
3036
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003037 ret = rdma_accept(cm_id, &cp);
3038 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003039 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003040 return ret;
3041 }
3042
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003043 return 0;
3044}
3045
3046static int
3047isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3048{
Sagi Grimberg67004252015-03-29 15:52:09 +03003049 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003050 int ret;
3051
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003052 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003053 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3054 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003055 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003056 isert_conn);
3057 return ret;
3058 }
3059 reinit_completion(&isert_conn->login_req_comp);
3060
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003061 /*
3062 * For login requests after the first PDU, isert_rx_login_req() will
3063 * kick schedule_delayed_work(&conn->login_work) as the packet is
3064 * received, which turns this callback from iscsi_target_do_login_rx()
3065 * into a NOP.
3066 */
3067 if (!login->first_request)
3068 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003069
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003070 isert_rx_login_req(isert_conn);
3071
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003072 isert_info("before login_comp conn: %p\n", conn);
3073 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003074 if (ret)
3075 return ret;
3076
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003077 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003078
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003079 return 0;
3080}
3081
3082static void
3083isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3084 struct isert_conn *isert_conn)
3085{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003086 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003087 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003088
3089 conn->login_family = np->np_sockaddr.ss_family;
3090
Andy Groverdc58f762015-08-24 10:26:05 -07003091 conn->login_sockaddr = cm_route->addr.dst_addr;
3092 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003093}
3094
3095static int
3096isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3097{
Sagi Grimberg67004252015-03-29 15:52:09 +03003098 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003100 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003101
3102accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003103 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003104 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003105 return -ENODEV;
3106
3107 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003108 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003109 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003110 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003111 np->np_thread_state);
3112 /**
3113 * No point in stalling here when np_thread
3114 * is in state RESET/SHUTDOWN/EXIT - bail
3115 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003116 return -ENODEV;
3117 }
3118 spin_unlock_bh(&np->np_thread_lock);
3119
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003120 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003121 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003122 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003123 goto accept_wait;
3124 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003125 isert_conn = list_first_entry(&isert_np->pending,
3126 struct isert_conn, node);
3127 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003128 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003129
3130 conn->context = isert_conn;
3131 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02003132 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003133
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003134 isert_set_conn_info(np, conn, isert_conn);
3135
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003136 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003137
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003138 return 0;
3139}
3140
3141static void
3142isert_free_np(struct iscsi_np *np)
3143{
Sagi Grimberg67004252015-03-29 15:52:09 +03003144 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003145 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003146
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003147 if (isert_np->cm_id)
3148 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003149
Sagi Grimberg268e6812014-12-02 16:57:36 +02003150 /*
3151 * FIXME: At this point we don't have a good way to insure
3152 * that at this point we don't have hanging connections that
3153 * completed RDMA establishment but didn't start iscsi login
3154 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003155 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003156 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003157 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003158 if (!list_empty(&isert_np->pending)) {
3159 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003160 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003161 &isert_np->pending,
3162 node) {
3163 isert_info("cleaning isert_conn %p state (%d)\n",
3164 isert_conn, isert_conn->state);
3165 isert_connect_release(isert_conn);
3166 }
3167 }
3168
3169 if (!list_empty(&isert_np->accepted)) {
3170 isert_info("Still have isert accepted connections\n");
3171 list_for_each_entry_safe(isert_conn, n,
3172 &isert_np->accepted,
3173 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003174 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003175 isert_conn, isert_conn->state);
3176 isert_connect_release(isert_conn);
3177 }
3178 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003179 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003180
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003181 np->np_context = NULL;
3182 kfree(isert_np);
3183}
3184
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003185static void isert_release_work(struct work_struct *work)
3186{
3187 struct isert_conn *isert_conn = container_of(work,
3188 struct isert_conn,
3189 release_work);
3190
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003191 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003192
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003193 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003194 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003195 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003196
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003197 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003198 isert_put_conn(isert_conn);
3199}
3200
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003201static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003202isert_wait4logout(struct isert_conn *isert_conn)
3203{
3204 struct iscsi_conn *conn = isert_conn->conn;
3205
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003206 isert_info("conn %p\n", isert_conn);
3207
Sagi Grimberg991bb762014-12-07 13:12:01 +02003208 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003209 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003210 wait_for_completion_timeout(&conn->conn_logout_comp,
3211 SECONDS_FOR_LOGOUT_COMP * HZ);
3212 }
3213}
3214
3215static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003216isert_wait4cmds(struct iscsi_conn *conn)
3217{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003218 isert_info("iscsi_conn %p\n", conn);
3219
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003220 if (conn->sess) {
3221 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3222 target_wait_for_sess_cmds(conn->sess->se_sess);
3223 }
3224}
3225
3226static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003227isert_beacon_done(struct ib_cq *cq, struct ib_wc *wc)
3228{
3229 struct isert_conn *isert_conn = wc->qp->qp_context;
3230
3231 isert_print_wc(wc, "beacon");
3232
3233 isert_info("conn %p completing wait_comp_err\n", isert_conn);
3234 complete(&isert_conn->wait_comp_err);
3235}
3236
3237static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003238isert_wait4flush(struct isert_conn *isert_conn)
3239{
3240 struct ib_recv_wr *bad_wr;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003241 static struct ib_cqe cqe = { .done = isert_beacon_done };
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003242
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003243 isert_info("conn %p\n", isert_conn);
3244
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003245 init_completion(&isert_conn->wait_comp_err);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003246 isert_conn->beacon.wr_cqe = &cqe;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003247 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003248 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003249 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003250 return;
3251 }
3252
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003253 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003254}
3255
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003256/**
3257 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3258 * unsolicitate dataout
3259 * @conn: iscsi connection
3260 *
3261 * We might still have commands that are waiting for unsolicited
3262 * dataouts messages. We must put the extra reference on those
3263 * before blocking on the target_wait_for_session_cmds
3264 */
3265static void
3266isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3267{
3268 struct iscsi_cmd *cmd, *tmp;
3269 static LIST_HEAD(drop_cmd_list);
3270
3271 spin_lock_bh(&conn->cmd_lock);
3272 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3273 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3274 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3275 (cmd->write_data_done < cmd->se_cmd.data_length))
3276 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3277 }
3278 spin_unlock_bh(&conn->cmd_lock);
3279
3280 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3281 list_del_init(&cmd->i_conn_node);
3282 if (cmd->i_state != ISTATE_REMOVE) {
3283 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3284
3285 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3286 isert_put_cmd(isert_cmd, true);
3287 }
3288 }
3289}
3290
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003291static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003292{
3293 struct isert_conn *isert_conn = conn->context;
3294
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003295 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003296
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003297 mutex_lock(&isert_conn->mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003298 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003299 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003300
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003301 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003302 isert_put_unsol_pending_cmds(conn);
3303 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003304 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003305
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003306 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003307}
3308
3309static void isert_free_conn(struct iscsi_conn *conn)
3310{
3311 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003312
Sagi Grimberg524630d2015-06-04 19:49:21 +03003313 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003314 isert_put_conn(isert_conn);
3315}
3316
3317static struct iscsit_transport iser_target_transport = {
3318 .name = "IB/iSER",
3319 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003320 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003321 .owner = THIS_MODULE,
3322 .iscsit_setup_np = isert_setup_np,
3323 .iscsit_accept_np = isert_accept_np,
3324 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003325 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003326 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003327 .iscsit_get_login_rx = isert_get_login_rx,
3328 .iscsit_put_login_tx = isert_put_login_tx,
3329 .iscsit_immediate_queue = isert_immediate_queue,
3330 .iscsit_response_queue = isert_response_queue,
3331 .iscsit_get_dataout = isert_get_dataout,
3332 .iscsit_queue_data_in = isert_put_datain,
3333 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003334 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003335 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003336};
3337
3338static int __init isert_init(void)
3339{
3340 int ret;
3341
Sagi Grimberg631af552015-01-25 19:09:50 +02003342 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3343 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003344 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003345 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003346 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003347 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003348 }
3349
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003350 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3351 WQ_UNBOUND_MAX_ACTIVE);
3352 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003353 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003354 ret = -ENOMEM;
3355 goto destroy_comp_wq;
3356 }
3357
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003358 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003359 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003360
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003361 return 0;
3362
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003363destroy_comp_wq:
3364 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003365
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003366 return ret;
3367}
3368
3369static void __exit isert_exit(void)
3370{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003371 flush_scheduled_work();
Sagi Grimbergb02efbfc2014-12-02 16:57:29 +02003372 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003373 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003374 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003375 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003376}
3377
3378MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003379MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003380MODULE_AUTHOR("nab@Linux-iSCSI.org");
3381MODULE_LICENSE("GPL");
3382
3383module_init(isert_init);
3384module_exit(isert_exit);