blob: f3f498f6ecc5e2c76c51fbdd9365300805e372fc [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
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020038#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080040
Sagi Grimberg45678b62015-01-25 19:11:19 +020041static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020042module_param_named(debug_level, isert_debug_level, int, 0644);
43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
44
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080045static DEFINE_MUTEX(device_list_mutex);
46static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080047static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020048static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080049
50static void
Vu Phamd40945d2013-08-28 23:23:34 +030051isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52static int
53isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030055static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020056isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030057static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020058isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
59 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020060static int
61isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020062static int
63isert_rdma_post_recvl(struct isert_conn *isert_conn);
64static int
65isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020066struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030067
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030068static void isert_release_work(struct work_struct *work);
69
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020070static inline bool
71isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
72{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020073 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020074 cmd->prot_op != TARGET_PROT_NORMAL);
75}
76
77
Vu Phamd40945d2013-08-28 23:23:34 +030078static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080079isert_qp_event_callback(struct ib_event *e, void *context)
80{
Sagi Grimberg67004252015-03-29 15:52:09 +030081 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080082
Sagi Grimbergea8a1612015-05-18 13:40:31 +030083 isert_err("%s (%d): conn %p\n",
84 ib_event_msg(e->event), e->event, isert_conn);
85
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080086 switch (e->event) {
87 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030088 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080089 break;
90 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020091 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080092 break;
93 default:
94 break;
95 }
96}
97
98static int
99isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
100{
101 int ret;
102
103 ret = ib_query_device(ib_dev, devattr);
104 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200105 isert_err("ib_query_device() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106 return ret;
107 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200108 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
109 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800110
111 return 0;
112}
113
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300114static struct isert_comp *
115isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800116{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300117 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200118 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300119 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800120
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800121 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200122 for (i = 0; i < device->comps_used; i++)
123 if (device->comps[i].active_qps <
124 device->comps[min].active_qps)
125 min = i;
126 comp = &device->comps[min];
127 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300128 mutex_unlock(&device_list_mutex);
129
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200130 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200131 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300132
133 return comp;
134}
135
136static void
137isert_comp_put(struct isert_comp *comp)
138{
139 mutex_lock(&device_list_mutex);
140 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800141 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300142}
143
144static struct ib_qp *
145isert_create_qp(struct isert_conn *isert_conn,
146 struct isert_comp *comp,
147 struct rdma_cm_id *cma_id)
148{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300149 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300150 struct ib_qp_init_attr attr;
151 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800152
153 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
154 attr.event_handler = isert_qp_event_callback;
155 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200156 attr.send_cq = comp->cq;
157 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200159 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800160 /*
161 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700162 * work-around for RDMA_READs with ConnectX-2.
163 *
164 * Also, still make sure to have at least two SGEs for
165 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800166 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700167 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800168 isert_conn->max_sge = attr.cap.max_send_sge;
169
170 attr.cap.max_recv_sge = 1;
171 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
172 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200173 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200174 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300176 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800177 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200178 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300179 return ERR_PTR(ret);
180 }
181
182 return cma_id->qp;
183}
184
185static int
186isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
187{
188 struct isert_comp *comp;
189 int ret;
190
191 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300192 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
193 if (IS_ERR(isert_conn->qp)) {
194 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200195 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800196 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800197
198 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200199err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300200 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200201 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800202}
203
204static void
205isert_cq_event_callback(struct ib_event *e, void *context)
206{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200207 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800208}
209
210static int
211isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
212{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300213 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300214 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800215 struct iser_rx_desc *rx_desc;
216 struct ib_sge *rx_sg;
217 u64 dma_addr;
218 int i, j;
219
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300220 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300222 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800223 goto fail;
224
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300225 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800226
227 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
228 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
229 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
230 if (ib_dma_mapping_error(ib_dev, dma_addr))
231 goto dma_map_fail;
232
233 rx_desc->dma_addr = dma_addr;
234
235 rx_sg = &rx_desc->rx_sg;
236 rx_sg->addr = rx_desc->dma_addr;
237 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600238 rx_sg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 }
240
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300241 isert_conn->rx_desc_head = 0;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200242
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800243 return 0;
244
245dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300246 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800247 for (j = 0; j < i; j++, rx_desc++) {
248 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
249 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
250 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300251 kfree(isert_conn->rx_descs);
252 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800253fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200254 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
255
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800256 return -ENOMEM;
257}
258
259static void
260isert_free_rx_descriptors(struct isert_conn *isert_conn)
261{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300262 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800263 struct iser_rx_desc *rx_desc;
264 int i;
265
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300266 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800267 return;
268
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300269 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800270 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
271 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
272 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
273 }
274
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300275 kfree(isert_conn->rx_descs);
276 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277}
278
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200279static void isert_cq_work(struct work_struct *);
280static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800281
Sagi Grimberg172369c2015-03-29 15:52:11 +0300282static void
283isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800284{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300285 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800286
Sagi Grimberg172369c2015-03-29 15:52:11 +0300287 for (i = 0; i < device->comps_used; i++) {
288 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300289
Sagi Grimberg172369c2015-03-29 15:52:11 +0300290 if (comp->cq) {
291 cancel_work_sync(&comp->work);
292 ib_destroy_cq(comp->cq);
293 }
Vu Pham59464ef2013-08-28 23:23:35 +0300294 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300295 kfree(device->comps);
296}
Vu Phamd40945d2013-08-28 23:23:34 +0300297
Sagi Grimberg172369c2015-03-29 15:52:11 +0300298static int
299isert_alloc_comps(struct isert_device *device,
300 struct ib_device_attr *attr)
301{
302 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200303
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200304 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300305 device->ib_device->num_comp_vectors));
306
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200307 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200308 "Fast registration %d pi_capable %d\n",
309 device->comps_used, device->ib_device->name,
310 device->ib_device->num_comp_vectors, device->use_fastreg,
311 device->pi_capable);
312
313 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
314 GFP_KERNEL);
315 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200316 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800317 return -ENOMEM;
318 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800319
Sagi Grimberg172369c2015-03-29 15:52:11 +0300320 max_cqe = min(ISER_MAX_CQ_LEN, attr->max_cqe);
321
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200322 for (i = 0; i < device->comps_used; i++) {
Matan Barak8e372102015-06-11 16:35:21 +0300323 struct ib_cq_init_attr cq_attr = {};
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200324 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800325
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200326 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200327 INIT_WORK(&comp->work, isert_cq_work);
Matan Barak8e372102015-06-11 16:35:21 +0300328 cq_attr.cqe = max_cqe;
329 cq_attr.comp_vector = i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200330 comp->cq = ib_create_cq(device->ib_device,
331 isert_cq_callback,
332 isert_cq_event_callback,
333 (void *)comp,
Matan Barak8e372102015-06-11 16:35:21 +0300334 &cq_attr);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200335 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300336 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200337 ret = PTR_ERR(comp->cq);
338 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800339 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800340 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800341
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200342 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800343 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800344 goto out_cq;
345 }
346
Sagi Grimberg172369c2015-03-29 15:52:11 +0300347 return 0;
348out_cq:
349 isert_free_comps(device);
350 return ret;
351}
352
353static int
354isert_create_device_ib_res(struct isert_device *device)
355{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300356 struct ib_device_attr *dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300357 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300358
359 dev_attr = &device->dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300360 ret = isert_query_device(device->ib_device, dev_attr);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300361 if (ret)
362 return ret;
363
364 /* asign function handlers */
365 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
366 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
367 device->use_fastreg = 1;
368 device->reg_rdma_mem = isert_reg_rdma;
369 device->unreg_rdma_mem = isert_unreg_rdma;
370 } else {
371 device->use_fastreg = 0;
372 device->reg_rdma_mem = isert_map_rdma;
373 device->unreg_rdma_mem = isert_unmap_cmd;
374 }
375
376 ret = isert_alloc_comps(device, dev_attr);
377 if (ret)
378 return ret;
379
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300380 device->pd = ib_alloc_pd(device->ib_device);
381 if (IS_ERR(device->pd)) {
382 ret = PTR_ERR(device->pd);
383 isert_err("failed to allocate pd, device %p, ret=%d\n",
384 device, ret);
385 goto out_cq;
386 }
387
Sagi Grimberg172369c2015-03-29 15:52:11 +0300388 /* Check signature cap */
389 device->pi_capable = dev_attr->device_cap_flags &
390 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300391
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800392 return 0;
393
394out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300395 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800396 return ret;
397}
398
399static void
400isert_free_device_ib_res(struct isert_device *device)
401{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200402 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800403
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300404 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300405 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800406}
407
408static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300409isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800410{
411 mutex_lock(&device_list_mutex);
412 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200413 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800414 if (!device->refcount) {
415 isert_free_device_ib_res(device);
416 list_del(&device->dev_node);
417 kfree(device);
418 }
419 mutex_unlock(&device_list_mutex);
420}
421
422static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300423isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800424{
425 struct isert_device *device;
426 int ret;
427
428 mutex_lock(&device_list_mutex);
429 list_for_each_entry(device, &device_list, dev_node) {
430 if (device->ib_device->node_guid == cma_id->device->node_guid) {
431 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200432 isert_info("Found iser device %p refcount %d\n",
433 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800434 mutex_unlock(&device_list_mutex);
435 return device;
436 }
437 }
438
439 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
440 if (!device) {
441 mutex_unlock(&device_list_mutex);
442 return ERR_PTR(-ENOMEM);
443 }
444
445 INIT_LIST_HEAD(&device->dev_node);
446
447 device->ib_device = cma_id->device;
448 ret = isert_create_device_ib_res(device);
449 if (ret) {
450 kfree(device);
451 mutex_unlock(&device_list_mutex);
452 return ERR_PTR(ret);
453 }
454
455 device->refcount++;
456 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200457 isert_info("Created a new iser device %p refcount %d\n",
458 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800459 mutex_unlock(&device_list_mutex);
460
461 return device;
462}
463
Vu Pham59464ef2013-08-28 23:23:35 +0300464static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200465isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300466{
467 struct fast_reg_descriptor *fr_desc, *tmp;
468 int i = 0;
469
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300470 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300471 return;
472
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200473 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300474
475 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300476 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300477 list_del(&fr_desc->list);
478 ib_free_fast_reg_page_list(fr_desc->data_frpl);
479 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200480 if (fr_desc->pi_ctx) {
481 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
482 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300483 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200484 kfree(fr_desc->pi_ctx);
485 }
Vu Pham59464ef2013-08-28 23:23:35 +0300486 kfree(fr_desc);
487 ++i;
488 }
489
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300490 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200491 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300492 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300493}
494
495static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200496isert_create_pi_ctx(struct fast_reg_descriptor *desc,
497 struct ib_device *device,
498 struct ib_pd *pd)
499{
Sagi Grimberg570db172014-12-02 16:57:31 +0200500 struct pi_context *pi_ctx;
501 int ret;
502
503 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
504 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200505 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200506 return -ENOMEM;
507 }
508
509 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
510 ISCSI_ISER_SG_TABLESIZE);
511 if (IS_ERR(pi_ctx->prot_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200512 isert_err("Failed to allocate prot frpl err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200513 PTR_ERR(pi_ctx->prot_frpl));
514 ret = PTR_ERR(pi_ctx->prot_frpl);
515 goto err_pi_ctx;
516 }
517
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300518 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
519 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200520 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200521 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200522 PTR_ERR(pi_ctx->prot_mr));
523 ret = PTR_ERR(pi_ctx->prot_mr);
524 goto err_prot_frpl;
525 }
526 desc->ind |= ISERT_PROT_KEY_VALID;
527
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300528 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200529 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200530 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200531 PTR_ERR(pi_ctx->sig_mr));
532 ret = PTR_ERR(pi_ctx->sig_mr);
533 goto err_prot_mr;
534 }
535
536 desc->pi_ctx = pi_ctx;
537 desc->ind |= ISERT_SIG_KEY_VALID;
538 desc->ind &= ~ISERT_PROTECTED;
539
540 return 0;
541
542err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700543 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200544err_prot_frpl:
Roland Dreierb2feda42015-05-29 23:12:10 -0700545 ib_free_fast_reg_page_list(pi_ctx->prot_frpl);
Sagi Grimberg570db172014-12-02 16:57:31 +0200546err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700547 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200548
549 return ret;
550}
551
552static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200553isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200554 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200555{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200556 int ret;
557
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200558 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
559 ISCSI_ISER_SG_TABLESIZE);
560 if (IS_ERR(fr_desc->data_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200561 isert_err("Failed to allocate data frpl err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200562 PTR_ERR(fr_desc->data_frpl));
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200563 return PTR_ERR(fr_desc->data_frpl);
564 }
565
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300566 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
567 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200568 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200569 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200570 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200571 ret = PTR_ERR(fr_desc->data_mr);
572 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200573 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200574 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200575
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200576 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200577
578 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200579
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200580err_data_frpl:
581 ib_free_fast_reg_page_list(fr_desc->data_frpl);
582
583 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200584}
585
586static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200587isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300588{
589 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300590 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700591 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
592 struct se_node_acl *se_nacl = se_sess->se_node_acl;
593 int i, ret, tag_num;
594 /*
595 * Setup the number of FRMRs based upon the number of tags
596 * available to session in iscsi_target_locate_portal().
597 */
598 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
599 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300600
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300601 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700602 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300603 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
604 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200605 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300606 ret = -ENOMEM;
607 goto err;
608 }
609
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200610 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300611 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200612 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200613 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200614 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800615 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300616 goto err;
617 }
618
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300619 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
620 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300621 }
622
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200623 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300624 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300625
626 return 0;
627
628err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200629 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300630 return ret;
631}
632
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300633static void
634isert_init_conn(struct isert_conn *isert_conn)
635{
636 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300637 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300638 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300639 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300640 init_completion(&isert_conn->wait);
641 kref_init(&isert_conn->kref);
642 mutex_init(&isert_conn->mutex);
643 spin_lock_init(&isert_conn->pool_lock);
644 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300645 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300646}
647
648static void
649isert_free_login_buf(struct isert_conn *isert_conn)
650{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300651 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300652
653 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
654 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
655 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
656 ISCSI_DEF_MAX_RECV_SEG_LEN,
657 DMA_FROM_DEVICE);
658 kfree(isert_conn->login_buf);
659}
660
661static int
662isert_alloc_login_buf(struct isert_conn *isert_conn,
663 struct ib_device *ib_dev)
664{
665 int ret;
666
667 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
668 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
669 if (!isert_conn->login_buf) {
670 isert_err("Unable to allocate isert_conn->login_buf\n");
671 return -ENOMEM;
672 }
673
674 isert_conn->login_req_buf = isert_conn->login_buf;
675 isert_conn->login_rsp_buf = isert_conn->login_buf +
676 ISCSI_DEF_MAX_RECV_SEG_LEN;
677
678 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
679 isert_conn->login_buf, isert_conn->login_req_buf,
680 isert_conn->login_rsp_buf);
681
682 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
683 (void *)isert_conn->login_req_buf,
684 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
685
686 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
687 if (ret) {
688 isert_err("login_req_dma mapping error: %d\n", ret);
689 isert_conn->login_req_dma = 0;
690 goto out_login_buf;
691 }
692
693 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
694 (void *)isert_conn->login_rsp_buf,
695 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
696
697 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
698 if (ret) {
699 isert_err("login_rsp_dma mapping error: %d\n", ret);
700 isert_conn->login_rsp_dma = 0;
701 goto out_req_dma_map;
702 }
703
704 return 0;
705
706out_req_dma_map:
707 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
708 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
709out_login_buf:
710 kfree(isert_conn->login_buf);
711 return ret;
712}
713
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800714static int
715isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
716{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200717 struct isert_np *isert_np = cma_id->context;
718 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800719 struct isert_conn *isert_conn;
720 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800721 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300722
723 spin_lock_bh(&np->np_thread_lock);
724 if (!np->enabled) {
725 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200726 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300727 return rdma_reject(cma_id, NULL, 0);
728 }
729 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800730
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200731 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800732 cma_id, cma_id->context);
733
734 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300735 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800736 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300738 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300739 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800740
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300741 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
742 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800743 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800744
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300745 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800746 if (IS_ERR(device)) {
747 ret = PTR_ERR(device);
748 goto out_rsp_dma_map;
749 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300750 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800751
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300752 /* Set max inflight RDMA READ requests */
753 isert_conn->initiator_depth = min_t(u8,
754 event->param.conn.initiator_depth,
755 device->dev_attr.max_qp_init_rd_atom);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200756 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300757
Sagi Grimberg570db172014-12-02 16:57:31 +0200758 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800759 if (ret)
760 goto out_conn_dev;
761
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200762 ret = isert_rdma_post_recvl(isert_conn);
763 if (ret)
764 goto out_conn_dev;
765
766 ret = isert_rdma_accept(isert_conn);
767 if (ret)
768 goto out_conn_dev;
769
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300770 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300771 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300772 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800773
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800774 return 0;
775
776out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300777 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800778out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300779 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800780out:
781 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200782 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800783 return ret;
784}
785
786static void
787isert_connect_release(struct isert_conn *isert_conn)
788{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300789 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800790
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200791 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800792
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300793 BUG_ON(!device);
794
795 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200796 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300797
Sagi Grimberg19e20902014-12-02 16:57:26 +0200798 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300799 if (isert_conn->cm_id)
800 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200801
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300802 if (isert_conn->qp) {
803 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200804
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300805 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300806 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800807 }
808
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300809 if (isert_conn->login_buf)
810 isert_free_login_buf(isert_conn);
811
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300812 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800813
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300814 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800815}
816
817static void
818isert_connected_handler(struct rdma_cm_id *cma_id)
819{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200820 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300821 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300822
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200823 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200824
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300825 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300826 isert_conn->state = ISER_CONN_UP;
827 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300828 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300829
830 mutex_lock(&isert_np->mutex);
831 list_move_tail(&isert_conn->node, &isert_np->pending);
832 mutex_unlock(&isert_np->mutex);
833
834 isert_info("np %p: Allow accept_np to continue\n", isert_np);
835 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800836}
837
838static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300839isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800840{
841 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300842 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800843
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200844 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
845 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800846
847 isert_connect_release(isert_conn);
848}
849
850static void
851isert_put_conn(struct isert_conn *isert_conn)
852{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300853 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800854}
855
Sagi Grimberg954f2372014-12-02 16:57:17 +0200856/**
857 * isert_conn_terminate() - Initiate connection termination
858 * @isert_conn: isert connection struct
859 *
860 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200861 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200862 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200863 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200864 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300865 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200866 * safe to call multiple times.
867 */
868static void
869isert_conn_terminate(struct isert_conn *isert_conn)
870{
871 int err;
872
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200873 switch (isert_conn->state) {
874 case ISER_CONN_TERMINATING:
875 break;
876 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200877 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200878 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200879 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200880 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300881 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200882 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200883 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200884 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200885 break;
886 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200887 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200888 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200889 }
890}
891
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700892static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200893isert_np_cma_handler(struct isert_np *isert_np,
894 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300896 isert_dbg("%s (%d): isert np %p\n",
897 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200898
899 switch (event) {
900 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300901 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200902 break;
903 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300904 isert_np->cm_id = isert_setup_id(isert_np);
905 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200906 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300907 isert_np, PTR_ERR(isert_np->cm_id));
908 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200909 }
910 break;
911 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200912 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200913 isert_np, event);
914 }
915
916 return -1;
917}
918
919static int
920isert_disconnected_handler(struct rdma_cm_id *cma_id,
921 enum rdma_cm_event_type event)
922{
923 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700924 struct isert_conn *isert_conn;
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300925 bool terminating = false;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700926
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300927 if (isert_np->cm_id == cma_id)
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200928 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700929
Sagi Grimberg19e20902014-12-02 16:57:26 +0200930 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800931
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300932 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300933 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200934 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300935 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200936
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300937 isert_info("conn %p completing wait\n", isert_conn);
938 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700939
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300940 if (terminating)
941 goto out;
942
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300943 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300944 if (!list_empty(&isert_conn->node)) {
945 list_del_init(&isert_conn->node);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300946 isert_put_conn(isert_conn);
947 queue_work(isert_release_wq, &isert_conn->release_work);
948 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300949 mutex_unlock(&isert_np->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300950
951out:
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700952 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800953}
954
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300955static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200956isert_connect_error(struct rdma_cm_id *cma_id)
957{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200958 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200959
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300960 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300961 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200962 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300963
964 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200965}
966
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800967static int
968isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
969{
970 int ret = 0;
971
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300972 isert_info("%s (%d): status %d id %p np %p\n",
973 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200974 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800975
976 switch (event->event) {
977 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800978 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700979 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200980 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981 break;
982 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800983 isert_connected_handler(cma_id);
984 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300985 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
986 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
987 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300988 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200989 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800990 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200991 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
992 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800993 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300994 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200995 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800996 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200997 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800998 break;
999 }
1000
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001001 return ret;
1002}
1003
1004static int
1005isert_post_recv(struct isert_conn *isert_conn, u32 count)
1006{
1007 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1008 int i, ret;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001009 unsigned int rx_head = isert_conn->rx_desc_head;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001010 struct iser_rx_desc *rx_desc;
1011
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001012 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
1013 rx_desc = &isert_conn->rx_descs[rx_head];
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001014 rx_wr->wr_id = (uintptr_t)rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001015 rx_wr->sg_list = &rx_desc->rx_sg;
1016 rx_wr->num_sge = 1;
1017 rx_wr->next = rx_wr + 1;
1018 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
1019 }
1020
1021 rx_wr--;
1022 rx_wr->next = NULL; /* mark end of work requests list */
1023
1024 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001025 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001026 &rx_wr_failed);
1027 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001028 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001029 isert_conn->post_recv_buf_count -= count;
1030 } else {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001031 isert_dbg("Posted %d RX buffers\n", count);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001032 isert_conn->rx_desc_head = rx_head;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033 }
1034 return ret;
1035}
1036
1037static int
1038isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1039{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001040 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001041 struct ib_send_wr send_wr, *send_wr_failed;
1042 int ret;
1043
1044 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1045 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1046
1047 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001048 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001049 send_wr.sg_list = tx_desc->tx_sg;
1050 send_wr.num_sge = tx_desc->num_sge;
1051 send_wr.opcode = IB_WR_SEND;
1052 send_wr.send_flags = IB_SEND_SIGNALED;
1053
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001054 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001055 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001056 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001057
1058 return ret;
1059}
1060
1061static void
1062isert_create_send_desc(struct isert_conn *isert_conn,
1063 struct isert_cmd *isert_cmd,
1064 struct iser_tx_desc *tx_desc)
1065{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001066 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001067 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001068
1069 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1070 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1071
1072 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1073 tx_desc->iser_header.flags = ISER_VER;
1074
1075 tx_desc->num_sge = 1;
1076 tx_desc->isert_cmd = isert_cmd;
1077
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001078 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1079 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001080 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001081 }
1082}
1083
1084static int
1085isert_init_tx_hdrs(struct isert_conn *isert_conn,
1086 struct iser_tx_desc *tx_desc)
1087{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001088 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001089 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001090 u64 dma_addr;
1091
1092 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1093 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1094 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001095 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001096 return -ENOMEM;
1097 }
1098
1099 tx_desc->dma_addr = dma_addr;
1100 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1101 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001102 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001103
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001104 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1105 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1106 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001107
1108 return 0;
1109}
1110
1111static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001112isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001113 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001114{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001115 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1116
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001117 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001118 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001119 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001120 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001121 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001122 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001123}
1124
1125static int
1126isert_rdma_post_recvl(struct isert_conn *isert_conn)
1127{
1128 struct ib_recv_wr rx_wr, *rx_wr_fail;
1129 struct ib_sge sge;
1130 int ret;
1131
1132 memset(&sge, 0, sizeof(struct ib_sge));
1133 sge.addr = isert_conn->login_req_dma;
1134 sge.length = ISER_RX_LOGIN_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001135 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001136
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001137 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001138 sge.addr, sge.length, sge.lkey);
1139
1140 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001141 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001142 rx_wr.sg_list = &sge;
1143 rx_wr.num_sge = 1;
1144
1145 isert_conn->post_recv_buf_count++;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001146 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001147 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001148 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001149 isert_conn->post_recv_buf_count--;
1150 }
1151
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001152 return ret;
1153}
1154
1155static int
1156isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1157 u32 length)
1158{
1159 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001160 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001161 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001162 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001163 int ret;
1164
1165 isert_create_send_desc(isert_conn, NULL, tx_desc);
1166
1167 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1168 sizeof(struct iscsi_hdr));
1169
1170 isert_init_tx_hdrs(isert_conn, tx_desc);
1171
1172 if (length > 0) {
1173 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1174
1175 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1176 length, DMA_TO_DEVICE);
1177
1178 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1179
1180 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1181 length, DMA_TO_DEVICE);
1182
1183 tx_dsg->addr = isert_conn->login_rsp_dma;
1184 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001185 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001186 tx_desc->num_sge = 2;
1187 }
1188 if (!login->login_failed) {
1189 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001190 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001191 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001192 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001193 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001194 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001195 " fastreg pool\n", isert_conn);
1196 return ret;
1197 }
1198 }
1199
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001200 ret = isert_alloc_rx_descriptors(isert_conn);
1201 if (ret)
1202 return ret;
1203
1204 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1205 if (ret)
1206 return ret;
1207
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001208 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001209 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001210 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001211 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001212 goto post_send;
1213 }
1214
1215 ret = isert_rdma_post_recvl(isert_conn);
1216 if (ret)
1217 return ret;
1218 }
1219post_send:
1220 ret = isert_post_send(isert_conn, tx_desc);
1221 if (ret)
1222 return ret;
1223
1224 return 0;
1225}
1226
1227static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001228isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001229{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001230 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1231 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001232 struct iscsi_conn *conn = isert_conn->conn;
1233 struct iscsi_login *login = conn->conn_login;
1234 int size;
1235
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001236 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001237
1238 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001239
1240 if (login->first_request) {
1241 struct iscsi_login_req *login_req =
1242 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1243 /*
1244 * Setup the initial iscsi_login values from the leading
1245 * login request PDU.
1246 */
1247 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1248 login->current_stage =
1249 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1250 >> 2;
1251 login->version_min = login_req->min_version;
1252 login->version_max = login_req->max_version;
1253 memcpy(login->isid, login_req->isid, 6);
1254 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1255 login->init_task_tag = login_req->itt;
1256 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1257 login->cid = be16_to_cpu(login_req->cid);
1258 login->tsih = be16_to_cpu(login_req->tsih);
1259 }
1260
1261 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1262
1263 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001264 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1265 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1266 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001267 memcpy(login->req_buf, &rx_desc->data[0], size);
1268
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001269 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001270 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001271 return;
1272 }
1273 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001274}
1275
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001276static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001277*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001278{
Sagi Grimberg67004252015-03-29 15:52:09 +03001279 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001280 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001281 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001283 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001284 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001285 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001286 return NULL;
1287 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001288 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001289 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001290 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001291
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001292 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293}
1294
1295static int
1296isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001297 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1298 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001299{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001300 struct iscsi_conn *conn = isert_conn->conn;
1301 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1302 struct scatterlist *sg;
1303 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1304 bool dump_payload = false;
1305
1306 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1307 if (rc < 0)
1308 return rc;
1309
1310 imm_data = cmd->immediate_data;
1311 imm_data_len = cmd->first_burst_len;
1312 unsol_data = cmd->unsolicited_data;
1313
1314 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1315 if (rc < 0) {
1316 return 0;
1317 } else if (rc > 0) {
1318 dump_payload = true;
1319 goto sequence_cmd;
1320 }
1321
1322 if (!imm_data)
1323 return 0;
1324
1325 sg = &cmd->se_cmd.t_data_sg[0];
1326 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1327
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001328 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001329 sg, sg_nents, &rx_desc->data[0], imm_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001330
1331 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1332
1333 cmd->write_data_done += imm_data_len;
1334
1335 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1336 spin_lock_bh(&cmd->istate_lock);
1337 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1338 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1339 spin_unlock_bh(&cmd->istate_lock);
1340 }
1341
1342sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001343 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001344
1345 if (!rc && dump_payload == false && unsol_data)
1346 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001347 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001348 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001349
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001350 return 0;
1351}
1352
1353static int
1354isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1355 struct iser_rx_desc *rx_desc, unsigned char *buf)
1356{
1357 struct scatterlist *sg_start;
1358 struct iscsi_conn *conn = isert_conn->conn;
1359 struct iscsi_cmd *cmd = NULL;
1360 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1361 u32 unsol_data_len = ntoh24(hdr->dlength);
1362 int rc, sg_nents, sg_off, page_off;
1363
1364 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1365 if (rc < 0)
1366 return rc;
1367 else if (!cmd)
1368 return 0;
1369 /*
1370 * FIXME: Unexpected unsolicited_data out
1371 */
1372 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001373 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001374 dump_stack();
1375 return -1;
1376 }
1377
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001378 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1379 "write_data_done: %u, data_length: %u\n",
1380 unsol_data_len, cmd->write_data_done,
1381 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001382
1383 sg_off = cmd->write_data_done / PAGE_SIZE;
1384 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1385 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1386 page_off = cmd->write_data_done % PAGE_SIZE;
1387 /*
1388 * FIXME: Non page-aligned unsolicited_data out
1389 */
1390 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001391 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001392 dump_stack();
1393 return -1;
1394 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001395 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1396 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1397 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001398
1399 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1400 unsol_data_len);
1401
1402 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1403 if (rc < 0)
1404 return rc;
1405
1406 return 0;
1407}
1408
1409static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001410isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001411 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1412 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001413{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001414 struct iscsi_conn *conn = isert_conn->conn;
1415 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1416 int rc;
1417
1418 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1419 if (rc < 0)
1420 return rc;
1421 /*
1422 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1423 */
1424
1425 return iscsit_process_nop_out(conn, cmd, hdr);
1426}
1427
1428static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001429isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001430 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1431 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001432{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001433 struct iscsi_conn *conn = isert_conn->conn;
1434 u32 payload_length = ntoh24(hdr->dlength);
1435 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001436 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001437
1438 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1439 if (rc < 0)
1440 return rc;
1441
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001442 if (payload_length) {
1443 text_in = kzalloc(payload_length, GFP_KERNEL);
1444 if (!text_in) {
1445 isert_err("Unable to allocate text_in of payload_length: %u\n",
1446 payload_length);
1447 return -ENOMEM;
1448 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001449 }
1450 cmd->text_in_ptr = text_in;
1451
1452 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1453
1454 return iscsit_process_text_cmd(conn, cmd, hdr);
1455}
1456
1457static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001458isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1459 uint32_t read_stag, uint64_t read_va,
1460 uint32_t write_stag, uint64_t write_va)
1461{
1462 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1463 struct iscsi_conn *conn = isert_conn->conn;
1464 struct iscsi_cmd *cmd;
1465 struct isert_cmd *isert_cmd;
1466 int ret = -EINVAL;
1467 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1468
Sagi Grimbergfb140272015-03-29 15:52:08 +03001469 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001470 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001471 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001472 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001473 return 0;
1474 }
1475
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001476 switch (opcode) {
1477 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001478 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001479 if (!cmd)
1480 break;
1481
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001482 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001483 isert_cmd->read_stag = read_stag;
1484 isert_cmd->read_va = read_va;
1485 isert_cmd->write_stag = write_stag;
1486 isert_cmd->write_va = write_va;
1487
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001488 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001489 rx_desc, (unsigned char *)hdr);
1490 break;
1491 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001492 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001493 if (!cmd)
1494 break;
1495
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001496 isert_cmd = iscsit_priv_cmd(cmd);
1497 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001498 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001499 break;
1500 case ISCSI_OP_SCSI_DATA_OUT:
1501 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1502 (unsigned char *)hdr);
1503 break;
1504 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001505 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001506 if (!cmd)
1507 break;
1508
1509 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1510 (unsigned char *)hdr);
1511 break;
1512 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001513 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001514 if (!cmd)
1515 break;
1516
1517 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001518 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001519 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001520 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1521 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1522 if (!cmd)
1523 break;
1524 } else {
1525 cmd = isert_allocate_cmd(conn);
1526 if (!cmd)
1527 break;
1528 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001529
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001530 isert_cmd = iscsit_priv_cmd(cmd);
1531 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001532 rx_desc, (struct iscsi_text *)hdr);
1533 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001534 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001535 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001536 dump_stack();
1537 break;
1538 }
1539
1540 return ret;
1541}
1542
1543static void
1544isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1545{
1546 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1547 uint64_t read_va = 0, write_va = 0;
1548 uint32_t read_stag = 0, write_stag = 0;
1549 int rc;
1550
1551 switch (iser_hdr->flags & 0xF0) {
1552 case ISCSI_CTRL:
1553 if (iser_hdr->flags & ISER_RSV) {
1554 read_stag = be32_to_cpu(iser_hdr->read_stag);
1555 read_va = be64_to_cpu(iser_hdr->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001556 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1557 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558 }
1559 if (iser_hdr->flags & ISER_WSV) {
1560 write_stag = be32_to_cpu(iser_hdr->write_stag);
1561 write_va = be64_to_cpu(iser_hdr->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001562 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1563 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001564 }
1565
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001566 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001567 break;
1568 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001569 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001570 break;
1571 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001572 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001573 break;
1574 }
1575
1576 rc = isert_rx_opcode(isert_conn, rx_desc,
1577 read_stag, read_va, write_stag, write_va);
1578}
1579
1580static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001581isert_rcv_completion(struct iser_rx_desc *desc,
1582 struct isert_conn *isert_conn,
1583 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001584{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001585 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001586 struct iscsi_hdr *hdr;
1587 u64 rx_dma;
1588 int rx_buflen, outstanding;
1589
1590 if ((char *)desc == isert_conn->login_req_buf) {
1591 rx_dma = isert_conn->login_req_dma;
1592 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001593 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001594 rx_dma, rx_buflen);
1595 } else {
1596 rx_dma = desc->dma_addr;
1597 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001598 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599 rx_dma, rx_buflen);
1600 }
1601
1602 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1603
1604 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001605 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001606 hdr->opcode, hdr->itt, hdr->flags,
1607 (int)(xfer_len - ISER_HEADERS_LEN));
1608
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001609 if ((char *)desc == isert_conn->login_req_buf) {
1610 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1611 if (isert_conn->conn) {
1612 struct iscsi_login *login = isert_conn->conn->conn_login;
1613
1614 if (login && !login->first_request)
1615 isert_rx_login_req(isert_conn);
1616 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001617 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001618 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001619 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001620 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001622 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001623
1624 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1625 DMA_FROM_DEVICE);
1626
1627 isert_conn->post_recv_buf_count--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001628 isert_dbg("Decremented post_recv_buf_count: %d\n",
1629 isert_conn->post_recv_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001630
1631 if ((char *)desc == isert_conn->login_req_buf)
1632 return;
1633
1634 outstanding = isert_conn->post_recv_buf_count;
1635 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1636 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1637 ISERT_MIN_POSTED_RX);
1638 err = isert_post_recv(isert_conn, count);
1639 if (err) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001640 isert_err("isert_post_recv() count: %d failed, %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001641 count, err);
1642 }
1643 }
1644}
1645
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001646static int
1647isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1648 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1649 enum iser_ib_op_code op, struct isert_data_buf *data)
1650{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001651 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001652
1653 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1654 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1655
1656 data->len = length - offset;
1657 data->offset = offset;
1658 data->sg_off = data->offset / PAGE_SIZE;
1659
1660 data->sg = &sg[data->sg_off];
1661 data->nents = min_t(unsigned int, nents - data->sg_off,
1662 ISCSI_ISER_SG_TABLESIZE);
1663 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1664 PAGE_SIZE);
1665
1666 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1667 data->dma_dir);
1668 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001669 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001670 return -EINVAL;
1671 }
1672
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001673 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001674 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001675
1676 return 0;
1677}
1678
1679static void
1680isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1681{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001682 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001683
1684 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1685 memset(data, 0, sizeof(*data));
1686}
1687
1688
1689
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001690static void
1691isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1692{
1693 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001694
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001695 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001696
1697 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001698 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001699 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001700 }
1701
Vu Pham90ecc6e2013-08-28 23:23:33 +03001702 if (wr->send_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001703 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001704 kfree(wr->send_wr);
1705 wr->send_wr = NULL;
1706 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707
Vu Pham90ecc6e2013-08-28 23:23:33 +03001708 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001709 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001710 kfree(wr->ib_sge);
1711 wr->ib_sge = NULL;
1712 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001713}
1714
1715static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001716isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001717{
1718 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001719
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001720 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001721
1722 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001723 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001724 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1725 isert_unmap_data_buf(isert_conn, &wr->prot);
1726 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1727 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001728 spin_lock_bh(&isert_conn->pool_lock);
1729 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1730 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001731 wr->fr_desc = NULL;
1732 }
1733
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001734 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001735 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001736 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001737 }
1738
1739 wr->ib_sge = NULL;
1740 wr->send_wr = NULL;
1741}
1742
1743static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001744isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001746 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001748 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001749 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001750 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001751
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001752 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753
1754 switch (cmd->iscsi_opcode) {
1755 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001756 spin_lock_bh(&conn->cmd_lock);
1757 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001758 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001759 spin_unlock_bh(&conn->cmd_lock);
1760
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001761 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001762 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001763 /*
1764 * Check for special case during comp_err where
1765 * WRITE_PENDING has been handed off from core,
1766 * but requires an extra target_put_sess_cmd()
1767 * before transport_generic_free_cmd() below.
1768 */
1769 if (comp_err &&
1770 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1771 struct se_cmd *se_cmd = &cmd->se_cmd;
1772
Bart Van Asscheafc16602015-04-27 13:52:36 +02001773 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001774 }
1775 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001776
Vu Phamd40945d2013-08-28 23:23:34 +03001777 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001778 transport_generic_free_cmd(&cmd->se_cmd, 0);
1779 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001780 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001781 spin_lock_bh(&conn->cmd_lock);
1782 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001783 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001784 spin_unlock_bh(&conn->cmd_lock);
1785
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786 transport_generic_free_cmd(&cmd->se_cmd, 0);
1787 break;
1788 case ISCSI_OP_REJECT:
1789 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001790 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001791 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1792 /* If the continue bit is on, keep the command alive */
1793 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1794 break;
1795
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796 spin_lock_bh(&conn->cmd_lock);
1797 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001798 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001799 spin_unlock_bh(&conn->cmd_lock);
1800
1801 /*
1802 * Handle special case for REJECT when iscsi_add_reject*() has
1803 * overwritten the original iscsi_opcode assignment, and the
1804 * associated cmd->se_cmd needs to be released.
1805 */
1806 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001807 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001808 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001809 transport_generic_free_cmd(&cmd->se_cmd, 0);
1810 break;
1811 }
1812 /*
1813 * Fall-through
1814 */
1815 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001816 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001817 break;
1818 }
1819}
1820
1821static void
1822isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1823{
1824 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001825 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001826 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1827 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1828 tx_desc->dma_addr = 0;
1829 }
1830}
1831
1832static void
1833isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001834 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001836 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001837 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001838 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1839 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1840 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001841 }
1842
1843 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001844 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001845}
1846
Sagi Grimberg96b79732014-03-17 12:52:18 +02001847static int
1848isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1849{
1850 struct ib_mr_status mr_status;
1851 int ret;
1852
1853 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1854 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001855 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001856 goto fail_mr_status;
1857 }
1858
1859 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1860 u64 sec_offset_err;
1861 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1862
1863 switch (mr_status.sig_err.err_type) {
1864 case IB_SIG_BAD_GUARD:
1865 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1866 break;
1867 case IB_SIG_BAD_REFTAG:
1868 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1869 break;
1870 case IB_SIG_BAD_APPTAG:
1871 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1872 break;
1873 }
1874 sec_offset_err = mr_status.sig_err.sig_err_offset;
1875 do_div(sec_offset_err, block_size);
1876 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1877
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001878 isert_err("PI error found type %d at sector 0x%llx "
1879 "expected 0x%x vs actual 0x%x\n",
1880 mr_status.sig_err.err_type,
1881 (unsigned long long)se_cmd->bad_sector,
1882 mr_status.sig_err.expected,
1883 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001884 ret = 1;
1885 }
1886
1887fail_mr_status:
1888 return ret;
1889}
1890
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001891static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001892isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1893 struct isert_cmd *isert_cmd)
1894{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001895 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001896 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001897 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001898 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001899 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001900 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001901
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001902 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001903 ret = isert_check_pi_status(se_cmd,
1904 wr->fr_desc->pi_ctx->sig_mr);
1905 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001906 }
1907
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001908 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001909 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001910 if (ret)
1911 transport_send_check_condition_and_sense(se_cmd,
1912 se_cmd->pi_err, 0);
1913 else
1914 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001915}
1916
1917static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001918isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1919 struct isert_cmd *isert_cmd)
1920{
1921 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001922 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001923 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001924 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001925 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001926 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001927
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001928 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001929 ret = isert_check_pi_status(se_cmd,
1930 wr->fr_desc->pi_ctx->sig_mr);
1931 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001932 }
1933
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001934 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001935 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001936 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001937 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001938
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001939 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001940 spin_lock_bh(&cmd->istate_lock);
1941 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1942 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1943 spin_unlock_bh(&cmd->istate_lock);
1944
Sagi Grimberg364189f2015-03-29 15:52:03 +03001945 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001946 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001947 transport_send_check_condition_and_sense(se_cmd,
1948 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001949 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001950 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001951 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001952}
1953
1954static void
1955isert_do_control_comp(struct work_struct *work)
1956{
1957 struct isert_cmd *isert_cmd = container_of(work,
1958 struct isert_cmd, comp_work);
1959 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001960 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001961 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001962
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001963 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1964
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001965 switch (cmd->i_state) {
1966 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001968 case ISTATE_SEND_REJECT: /* FALLTHRU */
1969 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001970 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001971 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1972 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001973 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001974 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001975 iscsit_logout_post_handler(cmd, cmd->conn);
1976 break;
1977 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001978 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001979 dump_stack();
1980 break;
1981 }
1982}
1983
1984static void
1985isert_response_completion(struct iser_tx_desc *tx_desc,
1986 struct isert_cmd *isert_cmd,
1987 struct isert_conn *isert_conn,
1988 struct ib_device *ib_dev)
1989{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001990 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001991
1992 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001993 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001994 cmd->i_state == ISTATE_SEND_REJECT ||
1995 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001996 isert_unmap_tx_desc(tx_desc, ib_dev);
1997
1998 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1999 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2000 return;
2001 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002002
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002004 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002005}
2006
2007static void
Sagi Grimberg77486812015-03-29 15:52:13 +03002008isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002009 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002010{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002011 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002012 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2013 struct isert_rdma_wr *wr;
2014
2015 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002016 isert_unmap_tx_desc(tx_desc, ib_dev);
2017 return;
2018 }
2019 wr = &isert_cmd->rdma_wr;
2020
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002021 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2022
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002023 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002024 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002025 isert_response_completion(tx_desc, isert_cmd,
2026 isert_conn, ib_dev);
2027 break;
2028 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002029 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030 break;
2031 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002032 isert_completion_rdma_read(tx_desc, isert_cmd);
2033 break;
2034 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002035 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002036 dump_stack();
2037 break;
2038 }
2039}
2040
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002041/**
2042 * is_isert_tx_desc() - Indicate if the completion wr_id
2043 * is a TX descriptor or not.
2044 * @isert_conn: iser connection
2045 * @wr_id: completion WR identifier
2046 *
2047 * Since we cannot rely on wc opcode in FLUSH errors
2048 * we must work around it by checking if the wr_id address
2049 * falls in the iser connection rx_descs buffer. If so
2050 * it is an RX descriptor, otherwize it is a TX.
2051 */
2052static inline bool
2053is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002054{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002055 void *start = isert_conn->rx_descs;
2056 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002057
2058 if (wr_id >= start && wr_id < start + len)
2059 return false;
2060
2061 return true;
2062}
2063
2064static void
2065isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2066{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002067 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002068 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002069 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002070 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002071 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002072 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002073 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002074 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002075
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002076 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2077 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002078 if (!isert_cmd)
2079 isert_unmap_tx_desc(desc, ib_dev);
2080 else
2081 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002082 } else {
2083 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002084 if (!isert_conn->post_recv_buf_count)
2085 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002086 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002087}
2088
2089static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002090isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002092 struct isert_conn *isert_conn;
2093 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002094 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002095
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002096 isert_conn = wc->qp->qp_context;
2097 if (likely(wc->status == IB_WC_SUCCESS)) {
2098 if (wc->opcode == IB_WC_RECV) {
2099 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002100 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002101 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002102 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002103 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002105 } else {
2106 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002107 isert_err("%s (%d): wr id %llx vend_err %x\n",
2108 ib_wc_status_msg(wc->status), wc->status,
2109 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002110 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002111 isert_dbg("%s (%d): wr id %llx\n",
2112 ib_wc_status_msg(wc->status), wc->status,
2113 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002114
2115 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2116 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002117 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118}
2119
2120static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002121isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002122{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002123 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002124 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002125 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002126 struct ib_wc *const wcs = comp->wcs;
2127 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002128
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002129 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2130 for (i = 0; i < n; i++)
2131 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002132
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002133 completed += n;
2134 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002135 break;
2136 }
2137
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002138 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002139}
2140
2141static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002142isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002143{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002144 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002145
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002146 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002147}
2148
2149static int
2150isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2151{
2152 struct ib_send_wr *wr_failed;
2153 int ret;
2154
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002155 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002156 &wr_failed);
2157 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002158 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002159 return ret;
2160 }
2161 return ret;
2162}
2163
2164static int
2165isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2166{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002167 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002168 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002169 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2170 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2171 &isert_cmd->tx_desc.iscsi_header;
2172
2173 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2174 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2175 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2176 /*
2177 * Attach SENSE DATA payload to iSCSI Response PDU
2178 */
2179 if (cmd->se_cmd.sense_buffer &&
2180 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2181 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002182 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002183 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002184 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002185 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002186
2187 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2188 cmd->sense_buffer);
2189 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2190
2191 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2192 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002193 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002194
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002195 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2196 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002197 DMA_TO_DEVICE);
2198
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002199 isert_cmd->pdu_buf_len = pdu_len;
2200 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2201 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002202 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002203 isert_cmd->tx_desc.num_sge = 2;
2204 }
2205
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002206 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002207
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002208 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002209
2210 return isert_post_response(isert_conn, isert_cmd);
2211}
2212
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002213static void
2214isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2215{
2216 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002217 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002218 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002219
2220 spin_lock_bh(&conn->cmd_lock);
2221 if (!list_empty(&cmd->i_conn_node))
2222 list_del_init(&cmd->i_conn_node);
2223 spin_unlock_bh(&conn->cmd_lock);
2224
2225 if (cmd->data_direction == DMA_TO_DEVICE)
2226 iscsit_stop_dataout_timer(cmd);
2227
2228 device->unreg_rdma_mem(isert_cmd, isert_conn);
2229}
2230
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002231static enum target_prot_op
2232isert_get_sup_prot_ops(struct iscsi_conn *conn)
2233{
Sagi Grimberg67004252015-03-29 15:52:09 +03002234 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002235 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002236
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002237 if (conn->tpg->tpg_attrib.t10_pi) {
2238 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002239 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002240 isert_conn->pi_support = true;
2241 return TARGET_PROT_ALL;
2242 }
2243 }
2244
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002245 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002246 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002247
2248 return TARGET_PROT_NORMAL;
2249}
2250
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002251static int
2252isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2253 bool nopout_response)
2254{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002255 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002256 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002257 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2258
2259 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2260 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2261 &isert_cmd->tx_desc.iscsi_header,
2262 nopout_response);
2263 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002264 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002265
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002266 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002267
2268 return isert_post_response(isert_conn, isert_cmd);
2269}
2270
2271static int
2272isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2273{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002274 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002275 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002276 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2277
2278 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2279 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2280 &isert_cmd->tx_desc.iscsi_header);
2281 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002282 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002283
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002284 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002285
2286 return isert_post_response(isert_conn, isert_cmd);
2287}
2288
2289static int
2290isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2291{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002292 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002293 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002294 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2295
2296 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2297 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2298 &isert_cmd->tx_desc.iscsi_header);
2299 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002300 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002301
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002302 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002303
2304 return isert_post_response(isert_conn, isert_cmd);
2305}
2306
2307static int
2308isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2309{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002310 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002311 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002312 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002313 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002314 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002315 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2316 struct iscsi_reject *hdr =
2317 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002318
2319 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002320 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002321 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002322
2323 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002324 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002325 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2326 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002327 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2328 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002329 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002330 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002331 isert_cmd->tx_desc.num_sge = 2;
2332
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002333 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002334
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002335 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002336
2337 return isert_post_response(isert_conn, isert_cmd);
2338}
2339
2340static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002341isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2342{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002343 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002344 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002345 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2346 struct iscsi_text_rsp *hdr =
2347 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2348 u32 txt_rsp_len;
2349 int rc;
2350
2351 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002352 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002353 if (rc < 0)
2354 return rc;
2355
2356 txt_rsp_len = rc;
2357 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2358
2359 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002360 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002361 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002362 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2363 void *txt_rsp_buf = cmd->buf_ptr;
2364
2365 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2366 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2367
2368 isert_cmd->pdu_buf_len = txt_rsp_len;
2369 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2370 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002371 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002372 isert_cmd->tx_desc.num_sge = 2;
2373 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002374 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002375
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002376 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002377
2378 return isert_post_response(isert_conn, isert_cmd);
2379}
2380
2381static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002382isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2383 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2384 u32 data_left, u32 offset)
2385{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002386 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002387 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002388 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002389 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002390 u32 sg_off, page_off;
2391 int i = 0, sg_nents;
2392
2393 sg_off = offset / PAGE_SIZE;
2394 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2395 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2396 page_off = offset % PAGE_SIZE;
2397
2398 send_wr->sg_list = ib_sge;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002399 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400 /*
2401 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2402 */
2403 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002404 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2405 "page_off: %u\n",
2406 (unsigned long long)tmp_sg->dma_address,
2407 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002408
2409 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2410 ib_sge->length = min_t(u32, data_left,
2411 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002412 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002413
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002414 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2415 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 page_off = 0;
2417 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002418 if (!data_left)
2419 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002420 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002421 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422 }
2423
Sagi Grimberg9253e662015-06-04 19:49:19 +03002424 send_wr->num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002425 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002426 send_wr->sg_list, send_wr->num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002427
Sagi Grimberg9253e662015-06-04 19:49:19 +03002428 return send_wr->num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429}
2430
2431static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002432isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2433 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002434{
2435 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002436 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002437 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002438 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002439 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002440 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002441 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2442 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002443
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002444 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002445
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002446 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2447 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2448 se_cmd->t_data_nents, se_cmd->data_length,
2449 offset, wr->iser_ib_op, &wr->data);
2450 if (ret)
2451 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002452
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002453 data_left = data->len;
2454 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002455
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002456 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002457 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002458 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002460 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002461 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002462 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002463
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002464 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2466 GFP_KERNEL);
2467 if (!wr->send_wr) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002468 isert_dbg("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002469 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002470 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002471 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002472
2473 wr->isert_cmd = isert_cmd;
2474 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002475
2476 for (i = 0; i < wr->send_wr_num; i++) {
2477 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2478 data_len = min(data_left, rdma_write_max);
2479
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002480 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002481 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2482 send_wr->opcode = IB_WR_RDMA_WRITE;
2483 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2484 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2485 if (i + 1 == wr->send_wr_num)
2486 send_wr->next = &isert_cmd->tx_desc.send_wr;
2487 else
2488 send_wr->next = &wr->send_wr[i + 1];
2489 } else {
2490 send_wr->opcode = IB_WR_RDMA_READ;
2491 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2492 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2493 if (i + 1 == wr->send_wr_num)
2494 send_wr->send_flags = IB_SEND_SIGNALED;
2495 else
2496 send_wr->next = &wr->send_wr[i + 1];
2497 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002498
2499 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2500 send_wr, data_len, offset);
2501 ib_sge += ib_sge_cnt;
2502
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002503 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002504 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002505 data_left -= data_len;
2506 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002507
2508 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002509unmap_cmd:
2510 isert_unmap_data_buf(isert_conn, data);
2511
Vu Pham90ecc6e2013-08-28 23:23:33 +03002512 return ret;
2513}
2514
2515static int
Vu Pham59464ef2013-08-28 23:23:35 +03002516isert_map_fr_pagelist(struct ib_device *ib_dev,
2517 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2518{
2519 u64 start_addr, end_addr, page, chunk_start = 0;
2520 struct scatterlist *tmp_sg;
2521 int i = 0, new_chunk, last_ent, n_pages;
2522
2523 n_pages = 0;
2524 new_chunk = 1;
2525 last_ent = sg_nents - 1;
2526 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2527 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2528 if (new_chunk)
2529 chunk_start = start_addr;
2530 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2531
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002532 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2533 i, (unsigned long long)tmp_sg->dma_address,
2534 tmp_sg->length);
Vu Pham59464ef2013-08-28 23:23:35 +03002535
2536 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2537 new_chunk = 0;
2538 continue;
2539 }
2540 new_chunk = 1;
2541
2542 page = chunk_start & PAGE_MASK;
2543 do {
2544 fr_pl[n_pages++] = page;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002545 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2546 n_pages - 1, page);
Vu Pham59464ef2013-08-28 23:23:35 +03002547 page += PAGE_SIZE;
2548 } while (page < end_addr);
2549 }
2550
2551 return n_pages;
2552}
2553
Sagi Grimberg10633c32014-12-07 13:12:04 +02002554static inline void
2555isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2556{
2557 u32 rkey;
2558
2559 memset(inv_wr, 0, sizeof(*inv_wr));
2560 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2561 inv_wr->opcode = IB_WR_LOCAL_INV;
2562 inv_wr->ex.invalidate_rkey = mr->rkey;
2563
2564 /* Bump the key */
2565 rkey = ib_inc_rkey(mr->rkey);
2566 ib_update_fast_reg_key(mr, rkey);
2567}
2568
Vu Pham59464ef2013-08-28 23:23:35 +03002569static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002570isert_fast_reg_mr(struct isert_conn *isert_conn,
2571 struct fast_reg_descriptor *fr_desc,
2572 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002573 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002574 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002575{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002576 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002577 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002578 struct ib_mr *mr;
2579 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002580 struct ib_send_wr fr_wr, inv_wr;
2581 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002582 int ret, pagelist_len;
2583 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002584
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002585 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002586 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002587 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2588 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002589 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2590 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002591 return 0;
2592 }
2593
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002594 if (ind == ISERT_DATA_KEY_VALID) {
2595 /* Registering data buffer */
2596 mr = fr_desc->data_mr;
2597 frpl = fr_desc->data_frpl;
2598 } else {
2599 /* Registering protection buffer */
2600 mr = fr_desc->pi_ctx->prot_mr;
2601 frpl = fr_desc->pi_ctx->prot_frpl;
2602 }
2603
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002604 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002605
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002606 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002607 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002608
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002609 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002610 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002611
Sagi Grimberg10633c32014-12-07 13:12:04 +02002612 if (!(fr_desc->ind & ind)) {
2613 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002614 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002615 }
2616
2617 /* Prepare FASTREG WR */
2618 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002619 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002620 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002621 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2622 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002623 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2624 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002625 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002626 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002627 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2628
2629 if (!wr)
2630 wr = &fr_wr;
2631 else
2632 wr->next = &fr_wr;
2633
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002634 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002635 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002636 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002637 return ret;
2638 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002639 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002640
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002641 sge->lkey = mr->lkey;
2642 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002643 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002644
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002645 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2646 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002647
2648 return ret;
2649}
2650
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002651static inline void
2652isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2653 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002654{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002655 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002656 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2657 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2658 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002659 /*
2660 * At the moment we hard code those, but if in the future
2661 * the target core would like to use it, we will take it
2662 * from se_cmd.
2663 */
2664 domain->sig.dif.apptag_check_mask = 0xffff;
2665 domain->sig.dif.app_escape = true;
2666 domain->sig.dif.ref_escape = true;
2667 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2668 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2669 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002670};
2671
2672static int
2673isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2674{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002675 switch (se_cmd->prot_op) {
2676 case TARGET_PROT_DIN_INSERT:
2677 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002678 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002679 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002680 break;
2681 case TARGET_PROT_DOUT_INSERT:
2682 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002683 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002684 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002685 break;
2686 case TARGET_PROT_DIN_PASS:
2687 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002688 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2689 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002690 break;
2691 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002692 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002693 return -EINVAL;
2694 }
2695
2696 return 0;
2697}
2698
2699static inline u8
2700isert_set_prot_checks(u8 prot_checks)
2701{
2702 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2703 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2704 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2705}
2706
2707static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002708isert_reg_sig_mr(struct isert_conn *isert_conn,
2709 struct se_cmd *se_cmd,
2710 struct isert_rdma_wr *rdma_wr,
2711 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002712{
2713 struct ib_send_wr sig_wr, inv_wr;
2714 struct ib_send_wr *bad_wr, *wr = NULL;
2715 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2716 struct ib_sig_attrs sig_attrs;
2717 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002718
2719 memset(&sig_attrs, 0, sizeof(sig_attrs));
2720 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2721 if (ret)
2722 goto err;
2723
2724 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2725
2726 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002727 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002728 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002729 }
2730
2731 memset(&sig_wr, 0, sizeof(sig_wr));
2732 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002733 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002734 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002735 sig_wr.num_sge = 1;
2736 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2737 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2738 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2739 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002740 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002741
2742 if (!wr)
2743 wr = &sig_wr;
2744 else
2745 wr->next = &sig_wr;
2746
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002747 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002748 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002749 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002750 goto err;
2751 }
2752 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2753
Sagi Grimberg570db172014-12-02 16:57:31 +02002754 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2755 rdma_wr->ib_sg[SIG].addr = 0;
2756 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002757 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2758 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2759 /*
2760 * We have protection guards on the wire
2761 * so we need to set a larget transfer
2762 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002763 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002764
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002765 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002766 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2767 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002768err:
2769 return ret;
2770}
2771
Vu Pham59464ef2013-08-28 23:23:35 +03002772static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002773isert_handle_prot_cmd(struct isert_conn *isert_conn,
2774 struct isert_cmd *isert_cmd,
2775 struct isert_rdma_wr *wr)
2776{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002777 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002778 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2779 int ret;
2780
2781 if (!wr->fr_desc->pi_ctx) {
2782 ret = isert_create_pi_ctx(wr->fr_desc,
2783 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002784 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002785 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002786 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002787 isert_conn);
2788 return ret;
2789 }
2790 }
2791
2792 if (se_cmd->t_prot_sg) {
2793 ret = isert_map_data_buf(isert_conn, isert_cmd,
2794 se_cmd->t_prot_sg,
2795 se_cmd->t_prot_nents,
2796 se_cmd->prot_length,
2797 0, wr->iser_ib_op, &wr->prot);
2798 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002799 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002800 isert_conn);
2801 return ret;
2802 }
2803
2804 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2805 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2806 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2807 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002808 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002809 isert_conn);
2810 goto unmap_prot_cmd;
2811 }
2812 }
2813
2814 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2815 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002816 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002817 isert_conn);
2818 goto unmap_prot_cmd;
2819 }
2820 wr->fr_desc->ind |= ISERT_PROTECTED;
2821
2822 return 0;
2823
2824unmap_prot_cmd:
2825 if (se_cmd->t_prot_sg)
2826 isert_unmap_data_buf(isert_conn, &wr->prot);
2827
2828 return ret;
2829}
2830
2831static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002832isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2833 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002834{
2835 struct se_cmd *se_cmd = &cmd->se_cmd;
2836 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002837 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002838 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002839 struct ib_send_wr *send_wr;
2840 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002841 u32 offset;
2842 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002843 unsigned long flags;
2844
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002845 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2846
2847 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2848 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2849 se_cmd->t_data_nents, se_cmd->data_length,
2850 offset, wr->iser_ib_op, &wr->data);
2851 if (ret)
2852 return ret;
2853
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002854 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002855 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2856 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002857 struct fast_reg_descriptor, list);
2858 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002859 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002860 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002861 }
2862
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002863 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002864 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002865 if (ret)
2866 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002867
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002868 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002869 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002870 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002871 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002872
Sagi Grimberg570db172014-12-02 16:57:31 +02002873 ib_sg = &wr->ib_sg[SIG];
2874 } else {
2875 ib_sg = &wr->ib_sg[DATA];
2876 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002877
Sagi Grimberg570db172014-12-02 16:57:31 +02002878 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002879 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002880 wr->send_wr_num = 1;
2881 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2882 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002883 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002884
2885 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002886 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002887 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002888 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002889 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2890 send_wr->opcode = IB_WR_RDMA_WRITE;
2891 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2892 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002893 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002894 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002895 } else {
2896 send_wr->opcode = IB_WR_RDMA_READ;
2897 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2898 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2899 send_wr->send_flags = IB_SEND_SIGNALED;
2900 }
2901
Vu Pham59464ef2013-08-28 23:23:35 +03002902 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002903
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002904unmap_cmd:
2905 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002906 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2907 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2908 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002909 }
2910 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002911
Vu Pham59464ef2013-08-28 23:23:35 +03002912 return ret;
2913}
2914
2915static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002916isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2917{
2918 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002919 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002920 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002921 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002922 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002923 struct ib_send_wr *wr_failed;
2924 int rc;
2925
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002926 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002927 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002928
Vu Pham90ecc6e2013-08-28 23:23:33 +03002929 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002930 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002931 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002932 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002933 return rc;
2934 }
2935
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002936 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002937 /*
2938 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2939 */
2940 isert_create_send_desc(isert_conn, isert_cmd,
2941 &isert_cmd->tx_desc);
2942 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2943 &isert_cmd->tx_desc.iscsi_header);
2944 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2945 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002946 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002947 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002948 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002949 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002950
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002951 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002952 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002953 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002954
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002955 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002956 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002957 "READ\n", isert_cmd);
2958 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002959 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002960 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002961
Vu Pham90ecc6e2013-08-28 23:23:33 +03002962 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002963}
2964
2965static int
2966isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2967{
2968 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002969 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002970 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002971 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002972 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002973 struct ib_send_wr *wr_failed;
2974 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002975
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002976 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002977 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002978 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002979 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002980 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002981 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002982 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002983 }
2984
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002985 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002986 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002987 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002988
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002989 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002990 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002991
Vu Pham90ecc6e2013-08-28 23:23:33 +03002992 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002993}
2994
2995static int
2996isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2997{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002998 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2999 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003000
3001 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03003002 case ISTATE_REMOVE:
3003 spin_lock_bh(&conn->cmd_lock);
3004 list_del_init(&cmd->i_conn_node);
3005 spin_unlock_bh(&conn->cmd_lock);
3006 isert_put_cmd(isert_cmd, true);
3007 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003008 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3009 ret = isert_put_nopin(cmd, conn, false);
3010 break;
3011 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003012 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003013 ret = -EINVAL;
3014 break;
3015 }
3016
3017 return ret;
3018}
3019
3020static int
3021isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3022{
Sagi Grimberg991bb762014-12-07 13:12:01 +02003023 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003024 int ret;
3025
3026 switch (state) {
3027 case ISTATE_SEND_LOGOUTRSP:
3028 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003029 if (!ret)
3030 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003031 break;
3032 case ISTATE_SEND_NOPIN:
3033 ret = isert_put_nopin(cmd, conn, true);
3034 break;
3035 case ISTATE_SEND_TASKMGTRSP:
3036 ret = isert_put_tm_rsp(cmd, conn);
3037 break;
3038 case ISTATE_SEND_REJECT:
3039 ret = isert_put_reject(cmd, conn);
3040 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003041 case ISTATE_SEND_TEXTRSP:
3042 ret = isert_put_text_rsp(cmd, conn);
3043 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003044 case ISTATE_SEND_STATUS:
3045 /*
3046 * Special case for sending non GOOD SCSI status from TX thread
3047 * context during pre se_cmd excecution failure.
3048 */
3049 ret = isert_put_response(conn, cmd);
3050 break;
3051 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003052 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003053 ret = -EINVAL;
3054 break;
3055 }
3056
3057 return ret;
3058}
3059
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003060struct rdma_cm_id *
3061isert_setup_id(struct isert_np *isert_np)
3062{
3063 struct iscsi_np *np = isert_np->np;
3064 struct rdma_cm_id *id;
3065 struct sockaddr *sa;
3066 int ret;
3067
3068 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003069 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003070
3071 id = rdma_create_id(isert_cma_handler, isert_np,
3072 RDMA_PS_TCP, IB_QPT_RC);
3073 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003074 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003075 ret = PTR_ERR(id);
3076 goto out;
3077 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003078 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003079
3080 ret = rdma_bind_addr(id, sa);
3081 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003082 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003083 goto out_id;
3084 }
3085
Sagi Grimberg992607e2015-03-29 15:52:18 +03003086 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003087 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003088 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003089 goto out_id;
3090 }
3091
3092 return id;
3093out_id:
3094 rdma_destroy_id(id);
3095out:
3096 return ERR_PTR(ret);
3097}
3098
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099static int
3100isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003101 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003102{
3103 struct isert_np *isert_np;
3104 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003105 int ret;
3106
3107 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3108 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003109 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003110 return -ENOMEM;
3111 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003112 sema_init(&isert_np->sem, 0);
3113 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003114 INIT_LIST_HEAD(&isert_np->accepted);
3115 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003116 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003117
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003118 /*
3119 * Setup the np->np_sockaddr from the passed sockaddr setup
3120 * in iscsi_target_configfs.c code..
3121 */
3122 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003123 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003125 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003126 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003127 ret = PTR_ERR(isert_lid);
3128 goto out;
3129 }
3130
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003131 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003132 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003133
3134 return 0;
3135
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003136out:
3137 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003138
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003139 return ret;
3140}
3141
3142static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003143isert_rdma_accept(struct isert_conn *isert_conn)
3144{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003145 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003146 struct rdma_conn_param cp;
3147 int ret;
3148
3149 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003150 cp.initiator_depth = isert_conn->initiator_depth;
3151 cp.retry_count = 7;
3152 cp.rnr_retry_count = 7;
3153
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003154 ret = rdma_accept(cm_id, &cp);
3155 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003156 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003157 return ret;
3158 }
3159
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003160 return 0;
3161}
3162
3163static int
3164isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3165{
Sagi Grimberg67004252015-03-29 15:52:09 +03003166 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003167 int ret;
3168
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003169 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003170 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3171 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003172 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003173 isert_conn);
3174 return ret;
3175 }
3176 reinit_completion(&isert_conn->login_req_comp);
3177
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003178 /*
3179 * For login requests after the first PDU, isert_rx_login_req() will
3180 * kick schedule_delayed_work(&conn->login_work) as the packet is
3181 * received, which turns this callback from iscsi_target_do_login_rx()
3182 * into a NOP.
3183 */
3184 if (!login->first_request)
3185 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003186
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003187 isert_rx_login_req(isert_conn);
3188
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003189 isert_info("before login_comp conn: %p\n", conn);
3190 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003191 if (ret)
3192 return ret;
3193
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003194 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003195
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003196 return 0;
3197}
3198
3199static void
3200isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3201 struct isert_conn *isert_conn)
3202{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003203 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003204 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003205
3206 conn->login_family = np->np_sockaddr.ss_family;
3207
Andy Groverdc58f762015-08-24 10:26:05 -07003208 conn->login_sockaddr = cm_route->addr.dst_addr;
3209 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003210}
3211
3212static int
3213isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3214{
Sagi Grimberg67004252015-03-29 15:52:09 +03003215 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003216 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003217 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003218
3219accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003220 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003221 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003222 return -ENODEV;
3223
3224 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003225 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003226 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003227 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003228 np->np_thread_state);
3229 /**
3230 * No point in stalling here when np_thread
3231 * is in state RESET/SHUTDOWN/EXIT - bail
3232 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003233 return -ENODEV;
3234 }
3235 spin_unlock_bh(&np->np_thread_lock);
3236
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003237 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003238 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003239 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003240 goto accept_wait;
3241 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003242 isert_conn = list_first_entry(&isert_np->pending,
3243 struct isert_conn, node);
3244 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003245 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003246
3247 conn->context = isert_conn;
3248 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003249
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003250 isert_set_conn_info(np, conn, isert_conn);
3251
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003252 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003253
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003254 return 0;
3255}
3256
3257static void
3258isert_free_np(struct iscsi_np *np)
3259{
Sagi Grimberg67004252015-03-29 15:52:09 +03003260 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003261 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003262
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003263 if (isert_np->cm_id)
3264 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265
Sagi Grimberg268e6812014-12-02 16:57:36 +02003266 /*
3267 * FIXME: At this point we don't have a good way to insure
3268 * that at this point we don't have hanging connections that
3269 * completed RDMA establishment but didn't start iscsi login
3270 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003271 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003272 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003273 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003274 if (!list_empty(&isert_np->pending)) {
3275 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003276 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003277 &isert_np->pending,
3278 node) {
3279 isert_info("cleaning isert_conn %p state (%d)\n",
3280 isert_conn, isert_conn->state);
3281 isert_connect_release(isert_conn);
3282 }
3283 }
3284
3285 if (!list_empty(&isert_np->accepted)) {
3286 isert_info("Still have isert accepted connections\n");
3287 list_for_each_entry_safe(isert_conn, n,
3288 &isert_np->accepted,
3289 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003290 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003291 isert_conn, isert_conn->state);
3292 isert_connect_release(isert_conn);
3293 }
3294 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003295 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003296
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003297 np->np_context = NULL;
3298 kfree(isert_np);
3299}
3300
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003301static void isert_release_work(struct work_struct *work)
3302{
3303 struct isert_conn *isert_conn = container_of(work,
3304 struct isert_conn,
3305 release_work);
3306
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003307 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003308
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003309 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003310
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003311 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003312 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003313 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003314
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003315 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003316 isert_put_conn(isert_conn);
3317}
3318
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003319static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003320isert_wait4logout(struct isert_conn *isert_conn)
3321{
3322 struct iscsi_conn *conn = isert_conn->conn;
3323
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003324 isert_info("conn %p\n", isert_conn);
3325
Sagi Grimberg991bb762014-12-07 13:12:01 +02003326 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003327 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003328 wait_for_completion_timeout(&conn->conn_logout_comp,
3329 SECONDS_FOR_LOGOUT_COMP * HZ);
3330 }
3331}
3332
3333static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003334isert_wait4cmds(struct iscsi_conn *conn)
3335{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003336 isert_info("iscsi_conn %p\n", conn);
3337
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003338 if (conn->sess) {
3339 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3340 target_wait_for_sess_cmds(conn->sess->se_sess);
3341 }
3342}
3343
3344static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003345isert_wait4flush(struct isert_conn *isert_conn)
3346{
3347 struct ib_recv_wr *bad_wr;
3348
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003349 isert_info("conn %p\n", isert_conn);
3350
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003351 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003352 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3353 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003354 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003355 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003356 return;
3357 }
3358
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003359 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003360}
3361
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003362/**
3363 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3364 * unsolicitate dataout
3365 * @conn: iscsi connection
3366 *
3367 * We might still have commands that are waiting for unsolicited
3368 * dataouts messages. We must put the extra reference on those
3369 * before blocking on the target_wait_for_session_cmds
3370 */
3371static void
3372isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3373{
3374 struct iscsi_cmd *cmd, *tmp;
3375 static LIST_HEAD(drop_cmd_list);
3376
3377 spin_lock_bh(&conn->cmd_lock);
3378 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3379 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3380 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3381 (cmd->write_data_done < cmd->se_cmd.data_length))
3382 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3383 }
3384 spin_unlock_bh(&conn->cmd_lock);
3385
3386 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3387 list_del_init(&cmd->i_conn_node);
3388 if (cmd->i_state != ISTATE_REMOVE) {
3389 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3390
3391 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3392 isert_put_cmd(isert_cmd, true);
3393 }
3394 }
3395}
3396
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003397static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003398{
3399 struct isert_conn *isert_conn = conn->context;
3400
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003401 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003402
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003403 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003404 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003405 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003406 * into full feature phase..
3407 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003408 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003409 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003410 return;
3411 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003412 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003413 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003414
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003415 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003416 isert_put_unsol_pending_cmds(conn);
3417 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003418 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003419
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003420 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003421}
3422
3423static void isert_free_conn(struct iscsi_conn *conn)
3424{
3425 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003426
Sagi Grimberg524630d2015-06-04 19:49:21 +03003427 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003428 isert_put_conn(isert_conn);
3429}
3430
3431static struct iscsit_transport iser_target_transport = {
3432 .name = "IB/iSER",
3433 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003434 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003435 .owner = THIS_MODULE,
3436 .iscsit_setup_np = isert_setup_np,
3437 .iscsit_accept_np = isert_accept_np,
3438 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003439 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003440 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003441 .iscsit_get_login_rx = isert_get_login_rx,
3442 .iscsit_put_login_tx = isert_put_login_tx,
3443 .iscsit_immediate_queue = isert_immediate_queue,
3444 .iscsit_response_queue = isert_response_queue,
3445 .iscsit_get_dataout = isert_get_dataout,
3446 .iscsit_queue_data_in = isert_put_datain,
3447 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003448 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003449 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003450};
3451
3452static int __init isert_init(void)
3453{
3454 int ret;
3455
Sagi Grimberg631af552015-01-25 19:09:50 +02003456 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3457 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003458 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003459 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003460 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003461 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003462 }
3463
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003464 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3465 WQ_UNBOUND_MAX_ACTIVE);
3466 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003467 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003468 ret = -ENOMEM;
3469 goto destroy_comp_wq;
3470 }
3471
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003472 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003473 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003474
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003475 return 0;
3476
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003477destroy_comp_wq:
3478 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003479
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003480 return ret;
3481}
3482
3483static void __exit isert_exit(void)
3484{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003485 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003486 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003487 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003488 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003489 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003490}
3491
3492MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003493MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003494MODULE_AUTHOR("nab@Linux-iSCSI.org");
3495MODULE_LICENSE("GPL");
3496
3497module_init(isert_init);
3498module_exit(isert_exit);