blob: 992e452c957056400511dd8e8e34fe57e74f97ca [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)
38
39static DEFINE_MUTEX(device_list_mutex);
40static LIST_HEAD(device_list);
41static struct workqueue_struct *isert_rx_wq;
42static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020043static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044
45static void
Vu Phamd40945d2013-08-28 23:23:34 +030046isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47static int
48isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
49 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030050static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020051isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030052static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020053isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020055static int
56isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020057static int
58isert_rdma_post_recvl(struct isert_conn *isert_conn);
59static int
60isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020061struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030062
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020063static inline bool
64isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
65{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020066 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020067 cmd->prot_op != TARGET_PROT_NORMAL);
68}
69
70
Vu Phamd40945d2013-08-28 23:23:34 +030071static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080072isert_qp_event_callback(struct ib_event *e, void *context)
73{
74 struct isert_conn *isert_conn = (struct isert_conn *)context;
75
76 pr_err("isert_qp_event_callback event: %d\n", e->event);
77 switch (e->event) {
78 case IB_EVENT_COMM_EST:
79 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
80 break;
81 case IB_EVENT_QP_LAST_WQE_REACHED:
82 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
83 break;
84 default:
85 break;
86 }
87}
88
89static int
90isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
91{
92 int ret;
93
94 ret = ib_query_device(ib_dev, devattr);
95 if (ret) {
96 pr_err("ib_query_device() failed: %d\n", ret);
97 return ret;
98 }
99 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
100 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
101
102 return 0;
103}
104
105static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200106isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800107{
108 struct isert_device *device = isert_conn->conn_device;
109 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800110 int ret, index, min_index = 0;
111
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800112 mutex_lock(&device_list_mutex);
113 for (index = 0; index < device->cqs_used; index++)
114 if (device->cq_active_qps[index] <
115 device->cq_active_qps[min_index])
116 min_index = index;
117 device->cq_active_qps[min_index]++;
118 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
119 mutex_unlock(&device_list_mutex);
120
121 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
122 attr.event_handler = isert_qp_event_callback;
123 attr.qp_context = isert_conn;
124 attr.send_cq = device->dev_tx_cq[min_index];
125 attr.recv_cq = device->dev_rx_cq[min_index];
126 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
127 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
128 /*
129 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700130 * work-around for RDMA_READs with ConnectX-2.
131 *
132 * Also, still make sure to have at least two SGEs for
133 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800134 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700135 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800136 isert_conn->max_sge = attr.cap.max_send_sge;
137
138 attr.cap.max_recv_sge = 1;
139 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
140 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200141 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200142 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800143
144 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
145 cma_id->device);
146 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
147 isert_conn->conn_pd->device);
148
149 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
150 if (ret) {
151 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200152 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800153 }
154 isert_conn->conn_qp = cma_id->qp;
155 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
156
157 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200158err:
159 mutex_lock(&device_list_mutex);
160 device->cq_active_qps[min_index]--;
161 mutex_unlock(&device_list_mutex);
162
163 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800164}
165
166static void
167isert_cq_event_callback(struct ib_event *e, void *context)
168{
169 pr_debug("isert_cq_event_callback event: %d\n", e->event);
170}
171
172static int
173isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
174{
175 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
176 struct iser_rx_desc *rx_desc;
177 struct ib_sge *rx_sg;
178 u64 dma_addr;
179 int i, j;
180
181 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
182 sizeof(struct iser_rx_desc), GFP_KERNEL);
183 if (!isert_conn->conn_rx_descs)
184 goto fail;
185
186 rx_desc = isert_conn->conn_rx_descs;
187
188 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
189 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
190 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
191 if (ib_dma_mapping_error(ib_dev, dma_addr))
192 goto dma_map_fail;
193
194 rx_desc->dma_addr = dma_addr;
195
196 rx_sg = &rx_desc->rx_sg;
197 rx_sg->addr = rx_desc->dma_addr;
198 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
199 rx_sg->lkey = isert_conn->conn_mr->lkey;
200 }
201
202 isert_conn->conn_rx_desc_head = 0;
203 return 0;
204
205dma_map_fail:
206 rx_desc = isert_conn->conn_rx_descs;
207 for (j = 0; j < i; j++, rx_desc++) {
208 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
209 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
210 }
211 kfree(isert_conn->conn_rx_descs);
212 isert_conn->conn_rx_descs = NULL;
213fail:
214 return -ENOMEM;
215}
216
217static void
218isert_free_rx_descriptors(struct isert_conn *isert_conn)
219{
220 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
221 struct iser_rx_desc *rx_desc;
222 int i;
223
224 if (!isert_conn->conn_rx_descs)
225 return;
226
227 rx_desc = isert_conn->conn_rx_descs;
228 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
229 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
230 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
231 }
232
233 kfree(isert_conn->conn_rx_descs);
234 isert_conn->conn_rx_descs = NULL;
235}
236
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800237static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800238static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800239static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800240static void isert_cq_rx_callback(struct ib_cq *, void *);
241
242static int
243isert_create_device_ib_res(struct isert_device *device)
244{
245 struct ib_device *ib_dev = device->ib_device;
246 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300247 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800248 int ret = 0, i, j;
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000249 int max_rx_cqe, max_tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800250
Vu Pham59464ef2013-08-28 23:23:35 +0300251 dev_attr = &device->dev_attr;
252 ret = isert_query_device(ib_dev, dev_attr);
253 if (ret)
254 return ret;
255
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000256 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
257 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
258
Vu Phamd40945d2013-08-28 23:23:34 +0300259 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200260 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
261 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200262 device->use_fastreg = 1;
263 device->reg_rdma_mem = isert_reg_rdma;
264 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300265 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200266 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300267 device->reg_rdma_mem = isert_map_rdma;
268 device->unreg_rdma_mem = isert_unmap_cmd;
269 }
Vu Phamd40945d2013-08-28 23:23:34 +0300270
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200271 /* Check signature cap */
272 device->pi_capable = dev_attr->device_cap_flags &
273 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
274
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800275 device->cqs_used = min_t(int, num_online_cpus(),
276 device->ib_device->num_comp_vectors);
277 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200278 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200279 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800280 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200281 device->ib_device->num_comp_vectors, device->use_fastreg,
282 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800283 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
284 device->cqs_used, GFP_KERNEL);
285 if (!device->cq_desc) {
286 pr_err("Unable to allocate device->cq_desc\n");
287 return -ENOMEM;
288 }
289 cq_desc = device->cq_desc;
290
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800291 for (i = 0; i < device->cqs_used; i++) {
292 cq_desc[i].device = device;
293 cq_desc[i].cq_index = i;
294
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800295 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800296 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
297 isert_cq_rx_callback,
298 isert_cq_event_callback,
299 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000300 max_rx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800301 if (IS_ERR(device->dev_rx_cq[i])) {
302 ret = PTR_ERR(device->dev_rx_cq[i]);
303 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800304 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800305 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800306
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800307 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800308 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
309 isert_cq_tx_callback,
310 isert_cq_event_callback,
311 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000312 max_tx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800313 if (IS_ERR(device->dev_tx_cq[i])) {
314 ret = PTR_ERR(device->dev_tx_cq[i]);
315 device->dev_tx_cq[i] = NULL;
316 goto out_cq;
317 }
318
319 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
320 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800321 goto out_cq;
322
Wei Yongjun94a71112013-10-29 09:56:34 +0800323 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
324 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800325 goto out_cq;
326 }
327
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800328 return 0;
329
330out_cq:
331 for (j = 0; j < i; j++) {
332 cq_desc = &device->cq_desc[j];
333
334 if (device->dev_rx_cq[j]) {
335 cancel_work_sync(&cq_desc->cq_rx_work);
336 ib_destroy_cq(device->dev_rx_cq[j]);
337 }
338 if (device->dev_tx_cq[j]) {
339 cancel_work_sync(&cq_desc->cq_tx_work);
340 ib_destroy_cq(device->dev_tx_cq[j]);
341 }
342 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800343 kfree(device->cq_desc);
344
345 return ret;
346}
347
348static void
349isert_free_device_ib_res(struct isert_device *device)
350{
351 struct isert_cq_desc *cq_desc;
352 int i;
353
354 for (i = 0; i < device->cqs_used; i++) {
355 cq_desc = &device->cq_desc[i];
356
357 cancel_work_sync(&cq_desc->cq_rx_work);
358 cancel_work_sync(&cq_desc->cq_tx_work);
359 ib_destroy_cq(device->dev_rx_cq[i]);
360 ib_destroy_cq(device->dev_tx_cq[i]);
361 device->dev_rx_cq[i] = NULL;
362 device->dev_tx_cq[i] = NULL;
363 }
364
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800365 kfree(device->cq_desc);
366}
367
368static void
369isert_device_try_release(struct isert_device *device)
370{
371 mutex_lock(&device_list_mutex);
372 device->refcount--;
373 if (!device->refcount) {
374 isert_free_device_ib_res(device);
375 list_del(&device->dev_node);
376 kfree(device);
377 }
378 mutex_unlock(&device_list_mutex);
379}
380
381static struct isert_device *
382isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
383{
384 struct isert_device *device;
385 int ret;
386
387 mutex_lock(&device_list_mutex);
388 list_for_each_entry(device, &device_list, dev_node) {
389 if (device->ib_device->node_guid == cma_id->device->node_guid) {
390 device->refcount++;
391 mutex_unlock(&device_list_mutex);
392 return device;
393 }
394 }
395
396 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
397 if (!device) {
398 mutex_unlock(&device_list_mutex);
399 return ERR_PTR(-ENOMEM);
400 }
401
402 INIT_LIST_HEAD(&device->dev_node);
403
404 device->ib_device = cma_id->device;
405 ret = isert_create_device_ib_res(device);
406 if (ret) {
407 kfree(device);
408 mutex_unlock(&device_list_mutex);
409 return ERR_PTR(ret);
410 }
411
412 device->refcount++;
413 list_add_tail(&device->dev_node, &device_list);
414 mutex_unlock(&device_list_mutex);
415
416 return device;
417}
418
Vu Pham59464ef2013-08-28 23:23:35 +0300419static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200420isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300421{
422 struct fast_reg_descriptor *fr_desc, *tmp;
423 int i = 0;
424
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200425 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300426 return;
427
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200428 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300429
430 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200431 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300432 list_del(&fr_desc->list);
433 ib_free_fast_reg_page_list(fr_desc->data_frpl);
434 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200435 if (fr_desc->pi_ctx) {
436 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
437 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
438 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
439 kfree(fr_desc->pi_ctx);
440 }
Vu Pham59464ef2013-08-28 23:23:35 +0300441 kfree(fr_desc);
442 ++i;
443 }
444
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200445 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300446 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200447 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300448}
449
450static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200451isert_create_pi_ctx(struct fast_reg_descriptor *desc,
452 struct ib_device *device,
453 struct ib_pd *pd)
454{
455 struct ib_mr_init_attr mr_init_attr;
456 struct pi_context *pi_ctx;
457 int ret;
458
459 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
460 if (!pi_ctx) {
461 pr_err("Failed to allocate pi context\n");
462 return -ENOMEM;
463 }
464
465 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
466 ISCSI_ISER_SG_TABLESIZE);
467 if (IS_ERR(pi_ctx->prot_frpl)) {
468 pr_err("Failed to allocate prot frpl err=%ld\n",
469 PTR_ERR(pi_ctx->prot_frpl));
470 ret = PTR_ERR(pi_ctx->prot_frpl);
471 goto err_pi_ctx;
472 }
473
474 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
475 if (IS_ERR(pi_ctx->prot_mr)) {
476 pr_err("Failed to allocate prot frmr err=%ld\n",
477 PTR_ERR(pi_ctx->prot_mr));
478 ret = PTR_ERR(pi_ctx->prot_mr);
479 goto err_prot_frpl;
480 }
481 desc->ind |= ISERT_PROT_KEY_VALID;
482
483 memset(&mr_init_attr, 0, sizeof(mr_init_attr));
484 mr_init_attr.max_reg_descriptors = 2;
485 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
486 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
487 if (IS_ERR(pi_ctx->sig_mr)) {
488 pr_err("Failed to allocate signature enabled mr err=%ld\n",
489 PTR_ERR(pi_ctx->sig_mr));
490 ret = PTR_ERR(pi_ctx->sig_mr);
491 goto err_prot_mr;
492 }
493
494 desc->pi_ctx = pi_ctx;
495 desc->ind |= ISERT_SIG_KEY_VALID;
496 desc->ind &= ~ISERT_PROTECTED;
497
498 return 0;
499
500err_prot_mr:
501 ib_dereg_mr(desc->pi_ctx->prot_mr);
502err_prot_frpl:
503 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
504err_pi_ctx:
505 kfree(desc->pi_ctx);
506
507 return ret;
508}
509
510static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200511isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200512 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200513{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200514 int ret;
515
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200516 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
517 ISCSI_ISER_SG_TABLESIZE);
518 if (IS_ERR(fr_desc->data_frpl)) {
519 pr_err("Failed to allocate data frpl err=%ld\n",
520 PTR_ERR(fr_desc->data_frpl));
521 return PTR_ERR(fr_desc->data_frpl);
522 }
523
524 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
525 if (IS_ERR(fr_desc->data_mr)) {
526 pr_err("Failed to allocate data frmr err=%ld\n",
527 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200528 ret = PTR_ERR(fr_desc->data_mr);
529 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200530 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200531 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200532
Sagi Grimberg570db172014-12-02 16:57:31 +0200533 pr_debug("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200534
535 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200536
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200537err_data_frpl:
538 ib_free_fast_reg_page_list(fr_desc->data_frpl);
539
540 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200541}
542
543static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200544isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300545{
546 struct fast_reg_descriptor *fr_desc;
547 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700548 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
549 struct se_node_acl *se_nacl = se_sess->se_node_acl;
550 int i, ret, tag_num;
551 /*
552 * Setup the number of FRMRs based upon the number of tags
553 * available to session in iscsi_target_locate_portal().
554 */
555 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
556 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300557
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200558 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700559 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300560 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
561 if (!fr_desc) {
562 pr_err("Failed to allocate fast_reg descriptor\n");
563 ret = -ENOMEM;
564 goto err;
565 }
566
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200567 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg570db172014-12-02 16:57:31 +0200568 isert_conn->conn_pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200569 if (ret) {
570 pr_err("Failed to create fastreg descriptor err=%d\n",
571 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800572 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300573 goto err;
574 }
575
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200576 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
577 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300578 }
579
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200580 pr_debug("Creating conn %p fastreg pool size=%d",
581 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300582
583 return 0;
584
585err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200586 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300587 return ret;
588}
589
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800590static int
591isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
592{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200593 struct isert_np *isert_np = cma_id->context;
594 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800595 struct isert_conn *isert_conn;
596 struct isert_device *device;
597 struct ib_device *ib_dev = cma_id->device;
598 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300599
600 spin_lock_bh(&np->np_thread_lock);
601 if (!np->enabled) {
602 spin_unlock_bh(&np->np_thread_lock);
603 pr_debug("iscsi_np is not enabled, reject connect request\n");
604 return rdma_reject(cma_id, NULL, 0);
605 }
606 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800607
608 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
609 cma_id, cma_id->context);
610
611 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
612 if (!isert_conn) {
613 pr_err("Unable to allocate isert_conn\n");
614 return -ENOMEM;
615 }
616 isert_conn->state = ISER_CONN_INIT;
617 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
618 init_completion(&isert_conn->conn_login_comp);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200619 init_completion(&isert_conn->login_req_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800620 init_completion(&isert_conn->conn_wait);
621 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800622 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700623 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300624 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700625 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800626
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800627 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800628
629 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
630 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
631 if (!isert_conn->login_buf) {
632 pr_err("Unable to allocate isert_conn->login_buf\n");
633 ret = -ENOMEM;
634 goto out;
635 }
636
637 isert_conn->login_req_buf = isert_conn->login_buf;
638 isert_conn->login_rsp_buf = isert_conn->login_buf +
639 ISCSI_DEF_MAX_RECV_SEG_LEN;
640 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
641 isert_conn->login_buf, isert_conn->login_req_buf,
642 isert_conn->login_rsp_buf);
643
644 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
645 (void *)isert_conn->login_req_buf,
646 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
647
648 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
649 if (ret) {
650 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
651 ret);
652 isert_conn->login_req_dma = 0;
653 goto out_login_buf;
654 }
655
656 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
657 (void *)isert_conn->login_rsp_buf,
658 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
659
660 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
661 if (ret) {
662 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
663 ret);
664 isert_conn->login_rsp_dma = 0;
665 goto out_req_dma_map;
666 }
667
668 device = isert_device_find_by_ib_dev(cma_id);
669 if (IS_ERR(device)) {
670 ret = PTR_ERR(device);
671 goto out_rsp_dma_map;
672 }
673
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300674 /* Set max inflight RDMA READ requests */
675 isert_conn->initiator_depth = min_t(u8,
676 event->param.conn.initiator_depth,
677 device->dev_attr.max_qp_init_rd_atom);
678 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
679
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800680 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200681 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
682 if (IS_ERR(isert_conn->conn_pd)) {
683 ret = PTR_ERR(isert_conn->conn_pd);
684 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
685 isert_conn, ret);
686 goto out_pd;
687 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800688
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200689 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
690 IB_ACCESS_LOCAL_WRITE);
691 if (IS_ERR(isert_conn->conn_mr)) {
692 ret = PTR_ERR(isert_conn->conn_mr);
693 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
694 isert_conn, ret);
695 goto out_mr;
696 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800697
Sagi Grimberg570db172014-12-02 16:57:31 +0200698 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800699 if (ret)
700 goto out_conn_dev;
701
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200702 ret = isert_rdma_post_recvl(isert_conn);
703 if (ret)
704 goto out_conn_dev;
705
706 ret = isert_rdma_accept(isert_conn);
707 if (ret)
708 goto out_conn_dev;
709
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800710 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300711 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800712 mutex_unlock(&isert_np->np_accept_mutex);
713
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300714 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
715 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800716 return 0;
717
718out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200719 ib_dereg_mr(isert_conn->conn_mr);
720out_mr:
721 ib_dealloc_pd(isert_conn->conn_pd);
722out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800723 isert_device_try_release(device);
724out_rsp_dma_map:
725 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
726 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
727out_req_dma_map:
728 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
729 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
730out_login_buf:
731 kfree(isert_conn->login_buf);
732out:
733 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200734 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800735 return ret;
736}
737
738static void
739isert_connect_release(struct isert_conn *isert_conn)
740{
741 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
742 struct isert_device *device = isert_conn->conn_device;
743 int cq_index;
744
745 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
746
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200747 if (device && device->use_fastreg)
748 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300749
Sagi Grimberg19e20902014-12-02 16:57:26 +0200750 isert_free_rx_descriptors(isert_conn);
751 rdma_destroy_id(isert_conn->conn_cm_id);
752
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800753 if (isert_conn->conn_qp) {
754 cq_index = ((struct isert_cq_desc *)
755 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
756 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200757 mutex_lock(&device_list_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800758 isert_conn->conn_device->cq_active_qps[cq_index]--;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200759 mutex_unlock(&device_list_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800760
Sagi Grimberg19e20902014-12-02 16:57:26 +0200761 ib_destroy_qp(isert_conn->conn_qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800762 }
763
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200764 ib_dereg_mr(isert_conn->conn_mr);
765 ib_dealloc_pd(isert_conn->conn_pd);
766
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800767 if (isert_conn->login_buf) {
768 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
769 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
770 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
771 ISCSI_DEF_MAX_RECV_SEG_LEN,
772 DMA_FROM_DEVICE);
773 kfree(isert_conn->login_buf);
774 }
775 kfree(isert_conn);
776
777 if (device)
778 isert_device_try_release(device);
779
780 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
781}
782
783static void
784isert_connected_handler(struct rdma_cm_id *cma_id)
785{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200786 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300787
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200788 pr_info("conn %p\n", isert_conn);
789
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200790 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
791 pr_warn("conn %p connect_release is running\n", isert_conn);
792 return;
793 }
794
795 mutex_lock(&isert_conn->conn_mutex);
796 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
797 isert_conn->state = ISER_CONN_UP;
798 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800799}
800
801static void
802isert_release_conn_kref(struct kref *kref)
803{
804 struct isert_conn *isert_conn = container_of(kref,
805 struct isert_conn, conn_kref);
806
807 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
808 current->comm, current->pid);
809
810 isert_connect_release(isert_conn);
811}
812
813static void
814isert_put_conn(struct isert_conn *isert_conn)
815{
816 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
817}
818
Sagi Grimberg954f2372014-12-02 16:57:17 +0200819/**
820 * isert_conn_terminate() - Initiate connection termination
821 * @isert_conn: isert connection struct
822 *
823 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200824 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200825 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200826 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200827 *
828 * This routine must be called with conn_mutex held. Thus it is
829 * safe to call multiple times.
830 */
831static void
832isert_conn_terminate(struct isert_conn *isert_conn)
833{
834 int err;
835
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200836 switch (isert_conn->state) {
837 case ISER_CONN_TERMINATING:
838 break;
839 case ISER_CONN_UP:
840 /*
841 * No flush completions will occur as we didn't
842 * get to ISER_CONN_FULL_FEATURE yet, complete
843 * to allow teardown progress.
844 */
845 complete(&isert_conn->conn_wait_comp_err);
846 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200847 pr_info("Terminating conn %p state %d\n",
848 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200849 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200850 err = rdma_disconnect(isert_conn->conn_cm_id);
851 if (err)
852 pr_warn("Failed rdma_disconnect isert_conn %p\n",
853 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200854 break;
855 default:
856 pr_warn("conn %p teminating in state %d\n",
857 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200858 }
859}
860
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700861static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200862isert_np_cma_handler(struct isert_np *isert_np,
863 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800864{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200865 pr_debug("isert np %p, handling event %d\n", isert_np, event);
866
867 switch (event) {
868 case RDMA_CM_EVENT_DEVICE_REMOVAL:
869 isert_np->np_cm_id = NULL;
870 break;
871 case RDMA_CM_EVENT_ADDR_CHANGE:
872 isert_np->np_cm_id = isert_setup_id(isert_np);
873 if (IS_ERR(isert_np->np_cm_id)) {
874 pr_err("isert np %p setup id failed: %ld\n",
875 isert_np, PTR_ERR(isert_np->np_cm_id));
876 isert_np->np_cm_id = NULL;
877 }
878 break;
879 default:
880 pr_err("isert np %p Unexpected event %d\n",
881 isert_np, event);
882 }
883
884 return -1;
885}
886
887static int
888isert_disconnected_handler(struct rdma_cm_id *cma_id,
889 enum rdma_cm_event_type event)
890{
891 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700892 struct isert_conn *isert_conn;
893
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200894 if (isert_np->np_cm_id == cma_id)
895 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700896
Sagi Grimberg19e20902014-12-02 16:57:26 +0200897 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800898
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200899 mutex_lock(&isert_conn->conn_mutex);
900 isert_conn_terminate(isert_conn);
901 mutex_unlock(&isert_conn->conn_mutex);
902
903 pr_info("conn %p completing conn_wait\n", isert_conn);
904 complete(&isert_conn->conn_wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700905
906 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800907}
908
Sagi Grimberg954f2372014-12-02 16:57:17 +0200909static void
910isert_connect_error(struct rdma_cm_id *cma_id)
911{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200912 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200913
914 isert_put_conn(isert_conn);
915}
916
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800917static int
918isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
919{
920 int ret = 0;
921
922 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
923 event->event, event->status, cma_id->context, cma_id);
924
925 switch (event->event) {
926 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800927 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700928 if (ret)
929 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
930 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800931 break;
932 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 isert_connected_handler(cma_id);
934 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300935 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
936 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
937 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300938 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200939 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800940 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200941 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
942 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800943 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg954f2372014-12-02 16:57:17 +0200944 isert_connect_error(cma_id);
945 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800946 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300947 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800948 break;
949 }
950
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800951 return ret;
952}
953
954static int
955isert_post_recv(struct isert_conn *isert_conn, u32 count)
956{
957 struct ib_recv_wr *rx_wr, *rx_wr_failed;
958 int i, ret;
959 unsigned int rx_head = isert_conn->conn_rx_desc_head;
960 struct iser_rx_desc *rx_desc;
961
962 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
963 rx_desc = &isert_conn->conn_rx_descs[rx_head];
964 rx_wr->wr_id = (unsigned long)rx_desc;
965 rx_wr->sg_list = &rx_desc->rx_sg;
966 rx_wr->num_sge = 1;
967 rx_wr->next = rx_wr + 1;
968 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
969 }
970
971 rx_wr--;
972 rx_wr->next = NULL; /* mark end of work requests list */
973
974 isert_conn->post_recv_buf_count += count;
975 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
976 &rx_wr_failed);
977 if (ret) {
978 pr_err("ib_post_recv() failed with ret: %d\n", ret);
979 isert_conn->post_recv_buf_count -= count;
980 } else {
981 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
982 isert_conn->conn_rx_desc_head = rx_head;
983 }
984 return ret;
985}
986
987static int
988isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
989{
990 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
991 struct ib_send_wr send_wr, *send_wr_failed;
992 int ret;
993
994 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
995 ISER_HEADERS_LEN, DMA_TO_DEVICE);
996
997 send_wr.next = NULL;
998 send_wr.wr_id = (unsigned long)tx_desc;
999 send_wr.sg_list = tx_desc->tx_sg;
1000 send_wr.num_sge = tx_desc->num_sge;
1001 send_wr.opcode = IB_WR_SEND;
1002 send_wr.send_flags = IB_SEND_SIGNALED;
1003
1004 atomic_inc(&isert_conn->post_send_buf_count);
1005
1006 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1007 if (ret) {
1008 pr_err("ib_post_send() failed, ret: %d\n", ret);
1009 atomic_dec(&isert_conn->post_send_buf_count);
1010 }
1011
1012 return ret;
1013}
1014
1015static void
1016isert_create_send_desc(struct isert_conn *isert_conn,
1017 struct isert_cmd *isert_cmd,
1018 struct iser_tx_desc *tx_desc)
1019{
1020 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1021
1022 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1023 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1024
1025 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1026 tx_desc->iser_header.flags = ISER_VER;
1027
1028 tx_desc->num_sge = 1;
1029 tx_desc->isert_cmd = isert_cmd;
1030
1031 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1032 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1033 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1034 }
1035}
1036
1037static int
1038isert_init_tx_hdrs(struct isert_conn *isert_conn,
1039 struct iser_tx_desc *tx_desc)
1040{
1041 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1042 u64 dma_addr;
1043
1044 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1045 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1046 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1047 pr_err("ib_dma_mapping_error() failed\n");
1048 return -ENOMEM;
1049 }
1050
1051 tx_desc->dma_addr = dma_addr;
1052 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1053 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1054 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1055
1056 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1057 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1058 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1059
1060 return 0;
1061}
1062
1063static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001064isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001065 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001066{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001067 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1068
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001069 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1070 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1071 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001072 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001073 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001074 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001075}
1076
1077static int
1078isert_rdma_post_recvl(struct isert_conn *isert_conn)
1079{
1080 struct ib_recv_wr rx_wr, *rx_wr_fail;
1081 struct ib_sge sge;
1082 int ret;
1083
1084 memset(&sge, 0, sizeof(struct ib_sge));
1085 sge.addr = isert_conn->login_req_dma;
1086 sge.length = ISER_RX_LOGIN_SIZE;
1087 sge.lkey = isert_conn->conn_mr->lkey;
1088
1089 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1090 sge.addr, sge.length, sge.lkey);
1091
1092 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1093 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1094 rx_wr.sg_list = &sge;
1095 rx_wr.num_sge = 1;
1096
1097 isert_conn->post_recv_buf_count++;
1098 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1099 if (ret) {
1100 pr_err("ib_post_recv() failed: %d\n", ret);
1101 isert_conn->post_recv_buf_count--;
1102 }
1103
1104 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1105 return ret;
1106}
1107
1108static int
1109isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1110 u32 length)
1111{
1112 struct isert_conn *isert_conn = conn->context;
1113 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1114 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1115 int ret;
1116
1117 isert_create_send_desc(isert_conn, NULL, tx_desc);
1118
1119 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1120 sizeof(struct iscsi_hdr));
1121
1122 isert_init_tx_hdrs(isert_conn, tx_desc);
1123
1124 if (length > 0) {
1125 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1126
1127 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1128 length, DMA_TO_DEVICE);
1129
1130 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1131
1132 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1133 length, DMA_TO_DEVICE);
1134
1135 tx_dsg->addr = isert_conn->login_rsp_dma;
1136 tx_dsg->length = length;
1137 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1138 tx_desc->num_sge = 2;
1139 }
1140 if (!login->login_failed) {
1141 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001142 if (!conn->sess->sess_ops->SessionType &&
1143 isert_conn->conn_device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001144 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001145 if (ret) {
1146 pr_err("Conn: %p failed to create"
1147 " fastreg pool\n", isert_conn);
1148 return ret;
1149 }
1150 }
1151
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001152 ret = isert_alloc_rx_descriptors(isert_conn);
1153 if (ret)
1154 return ret;
1155
1156 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1157 if (ret)
1158 return ret;
1159
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001160 /* Now we are in FULL_FEATURE phase */
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001161 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001162 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001163 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001164 goto post_send;
1165 }
1166
1167 ret = isert_rdma_post_recvl(isert_conn);
1168 if (ret)
1169 return ret;
1170 }
1171post_send:
1172 ret = isert_post_send(isert_conn, tx_desc);
1173 if (ret)
1174 return ret;
1175
1176 return 0;
1177}
1178
1179static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001180isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001181{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001182 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1183 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001184 struct iscsi_conn *conn = isert_conn->conn;
1185 struct iscsi_login *login = conn->conn_login;
1186 int size;
1187
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001188 pr_info("conn %p\n", isert_conn);
1189
1190 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191
1192 if (login->first_request) {
1193 struct iscsi_login_req *login_req =
1194 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1195 /*
1196 * Setup the initial iscsi_login values from the leading
1197 * login request PDU.
1198 */
1199 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1200 login->current_stage =
1201 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1202 >> 2;
1203 login->version_min = login_req->min_version;
1204 login->version_max = login_req->max_version;
1205 memcpy(login->isid, login_req->isid, 6);
1206 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1207 login->init_task_tag = login_req->itt;
1208 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1209 login->cid = be16_to_cpu(login_req->cid);
1210 login->tsih = be16_to_cpu(login_req->tsih);
1211 }
1212
1213 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1214
1215 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1216 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1217 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1218 memcpy(login->req_buf, &rx_desc->data[0], size);
1219
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001220 if (login->first_request) {
1221 complete(&isert_conn->conn_login_comp);
1222 return;
1223 }
1224 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001225}
1226
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001228*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001229{
1230 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1231 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001232 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001233
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001234 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001235 if (!cmd) {
1236 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001237 return NULL;
1238 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001239 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001240 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001241 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001242
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001243 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001244}
1245
1246static int
1247isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001248 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1249 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001250{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251 struct iscsi_conn *conn = isert_conn->conn;
1252 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1253 struct scatterlist *sg;
1254 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1255 bool dump_payload = false;
1256
1257 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1258 if (rc < 0)
1259 return rc;
1260
1261 imm_data = cmd->immediate_data;
1262 imm_data_len = cmd->first_burst_len;
1263 unsol_data = cmd->unsolicited_data;
1264
1265 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1266 if (rc < 0) {
1267 return 0;
1268 } else if (rc > 0) {
1269 dump_payload = true;
1270 goto sequence_cmd;
1271 }
1272
1273 if (!imm_data)
1274 return 0;
1275
1276 sg = &cmd->se_cmd.t_data_sg[0];
1277 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1278
1279 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1280 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1281
1282 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1283
1284 cmd->write_data_done += imm_data_len;
1285
1286 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1287 spin_lock_bh(&cmd->istate_lock);
1288 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1289 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1290 spin_unlock_bh(&cmd->istate_lock);
1291 }
1292
1293sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001294 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001295
1296 if (!rc && dump_payload == false && unsol_data)
1297 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001298 else if (dump_payload && imm_data)
1299 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001300
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001301 return 0;
1302}
1303
1304static int
1305isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1306 struct iser_rx_desc *rx_desc, unsigned char *buf)
1307{
1308 struct scatterlist *sg_start;
1309 struct iscsi_conn *conn = isert_conn->conn;
1310 struct iscsi_cmd *cmd = NULL;
1311 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1312 u32 unsol_data_len = ntoh24(hdr->dlength);
1313 int rc, sg_nents, sg_off, page_off;
1314
1315 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1316 if (rc < 0)
1317 return rc;
1318 else if (!cmd)
1319 return 0;
1320 /*
1321 * FIXME: Unexpected unsolicited_data out
1322 */
1323 if (!cmd->unsolicited_data) {
1324 pr_err("Received unexpected solicited data payload\n");
1325 dump_stack();
1326 return -1;
1327 }
1328
1329 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1330 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1331
1332 sg_off = cmd->write_data_done / PAGE_SIZE;
1333 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1334 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1335 page_off = cmd->write_data_done % PAGE_SIZE;
1336 /*
1337 * FIXME: Non page-aligned unsolicited_data out
1338 */
1339 if (page_off) {
1340 pr_err("Received unexpected non-page aligned data payload\n");
1341 dump_stack();
1342 return -1;
1343 }
1344 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1345 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1346
1347 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1348 unsol_data_len);
1349
1350 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1351 if (rc < 0)
1352 return rc;
1353
1354 return 0;
1355}
1356
1357static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001358isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001359 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1360 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001361{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001362 struct iscsi_conn *conn = isert_conn->conn;
1363 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1364 int rc;
1365
1366 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1367 if (rc < 0)
1368 return rc;
1369 /*
1370 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1371 */
1372
1373 return iscsit_process_nop_out(conn, cmd, hdr);
1374}
1375
1376static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001377isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001378 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1379 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001380{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001381 struct iscsi_conn *conn = isert_conn->conn;
1382 u32 payload_length = ntoh24(hdr->dlength);
1383 int rc;
1384 unsigned char *text_in;
1385
1386 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1387 if (rc < 0)
1388 return rc;
1389
1390 text_in = kzalloc(payload_length, GFP_KERNEL);
1391 if (!text_in) {
1392 pr_err("Unable to allocate text_in of payload_length: %u\n",
1393 payload_length);
1394 return -ENOMEM;
1395 }
1396 cmd->text_in_ptr = text_in;
1397
1398 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1399
1400 return iscsit_process_text_cmd(conn, cmd, hdr);
1401}
1402
1403static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001404isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1405 uint32_t read_stag, uint64_t read_va,
1406 uint32_t write_stag, uint64_t write_va)
1407{
1408 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1409 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001410 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001411 struct iscsi_cmd *cmd;
1412 struct isert_cmd *isert_cmd;
1413 int ret = -EINVAL;
1414 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1415
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001416 if (sess->sess_ops->SessionType &&
1417 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1418 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1419 " ignoring\n", opcode);
1420 return 0;
1421 }
1422
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001423 switch (opcode) {
1424 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001425 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001426 if (!cmd)
1427 break;
1428
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001429 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001430 isert_cmd->read_stag = read_stag;
1431 isert_cmd->read_va = read_va;
1432 isert_cmd->write_stag = write_stag;
1433 isert_cmd->write_va = write_va;
1434
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001435 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001436 rx_desc, (unsigned char *)hdr);
1437 break;
1438 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001439 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001440 if (!cmd)
1441 break;
1442
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001443 isert_cmd = iscsit_priv_cmd(cmd);
1444 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001445 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001446 break;
1447 case ISCSI_OP_SCSI_DATA_OUT:
1448 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1449 (unsigned char *)hdr);
1450 break;
1451 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001452 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001453 if (!cmd)
1454 break;
1455
1456 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1457 (unsigned char *)hdr);
1458 break;
1459 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001460 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001461 if (!cmd)
1462 break;
1463
1464 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1465 if (ret > 0)
1466 wait_for_completion_timeout(&conn->conn_logout_comp,
1467 SECONDS_FOR_LOGOUT_COMP *
1468 HZ);
1469 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001470 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001471 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001472 if (!cmd)
1473 break;
1474
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001475 isert_cmd = iscsit_priv_cmd(cmd);
1476 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001477 rx_desc, (struct iscsi_text *)hdr);
1478 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001479 default:
1480 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1481 dump_stack();
1482 break;
1483 }
1484
1485 return ret;
1486}
1487
1488static void
1489isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1490{
1491 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1492 uint64_t read_va = 0, write_va = 0;
1493 uint32_t read_stag = 0, write_stag = 0;
1494 int rc;
1495
1496 switch (iser_hdr->flags & 0xF0) {
1497 case ISCSI_CTRL:
1498 if (iser_hdr->flags & ISER_RSV) {
1499 read_stag = be32_to_cpu(iser_hdr->read_stag);
1500 read_va = be64_to_cpu(iser_hdr->read_va);
1501 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1502 read_stag, (unsigned long long)read_va);
1503 }
1504 if (iser_hdr->flags & ISER_WSV) {
1505 write_stag = be32_to_cpu(iser_hdr->write_stag);
1506 write_va = be64_to_cpu(iser_hdr->write_va);
1507 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1508 write_stag, (unsigned long long)write_va);
1509 }
1510
1511 pr_debug("ISER ISCSI_CTRL PDU\n");
1512 break;
1513 case ISER_HELLO:
1514 pr_err("iSER Hello message\n");
1515 break;
1516 default:
1517 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1518 break;
1519 }
1520
1521 rc = isert_rx_opcode(isert_conn, rx_desc,
1522 read_stag, read_va, write_stag, write_va);
1523}
1524
1525static void
1526isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1527 unsigned long xfer_len)
1528{
1529 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1530 struct iscsi_hdr *hdr;
1531 u64 rx_dma;
1532 int rx_buflen, outstanding;
1533
1534 if ((char *)desc == isert_conn->login_req_buf) {
1535 rx_dma = isert_conn->login_req_dma;
1536 rx_buflen = ISER_RX_LOGIN_SIZE;
1537 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1538 rx_dma, rx_buflen);
1539 } else {
1540 rx_dma = desc->dma_addr;
1541 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1542 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1543 rx_dma, rx_buflen);
1544 }
1545
1546 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1547
1548 hdr = &desc->iscsi_header;
1549 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1550 hdr->opcode, hdr->itt, hdr->flags,
1551 (int)(xfer_len - ISER_HEADERS_LEN));
1552
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001553 if ((char *)desc == isert_conn->login_req_buf) {
1554 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1555 if (isert_conn->conn) {
1556 struct iscsi_login *login = isert_conn->conn->conn_login;
1557
1558 if (login && !login->first_request)
1559 isert_rx_login_req(isert_conn);
1560 }
1561 mutex_lock(&isert_conn->conn_mutex);
1562 complete(&isert_conn->login_req_comp);
1563 mutex_unlock(&isert_conn->conn_mutex);
1564 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001565 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001566 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001567
1568 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1569 DMA_FROM_DEVICE);
1570
1571 isert_conn->post_recv_buf_count--;
1572 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1573 isert_conn->post_recv_buf_count);
1574
1575 if ((char *)desc == isert_conn->login_req_buf)
1576 return;
1577
1578 outstanding = isert_conn->post_recv_buf_count;
1579 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1580 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1581 ISERT_MIN_POSTED_RX);
1582 err = isert_post_recv(isert_conn, count);
1583 if (err) {
1584 pr_err("isert_post_recv() count: %d failed, %d\n",
1585 count, err);
1586 }
1587 }
1588}
1589
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001590static int
1591isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1592 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1593 enum iser_ib_op_code op, struct isert_data_buf *data)
1594{
1595 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1596
1597 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1598 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1599
1600 data->len = length - offset;
1601 data->offset = offset;
1602 data->sg_off = data->offset / PAGE_SIZE;
1603
1604 data->sg = &sg[data->sg_off];
1605 data->nents = min_t(unsigned int, nents - data->sg_off,
1606 ISCSI_ISER_SG_TABLESIZE);
1607 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1608 PAGE_SIZE);
1609
1610 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1611 data->dma_dir);
1612 if (unlikely(!data->dma_nents)) {
1613 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1614 return -EINVAL;
1615 }
1616
1617 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1618 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1619
1620 return 0;
1621}
1622
1623static void
1624isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1625{
1626 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1627
1628 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1629 memset(data, 0, sizeof(*data));
1630}
1631
1632
1633
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001634static void
1635isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1636{
1637 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638
Vu Pham90ecc6e2013-08-28 23:23:33 +03001639 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001640
1641 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001642 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001643 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001644 }
1645
Vu Pham90ecc6e2013-08-28 23:23:33 +03001646 if (wr->send_wr) {
1647 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1648 kfree(wr->send_wr);
1649 wr->send_wr = NULL;
1650 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651
Vu Pham90ecc6e2013-08-28 23:23:33 +03001652 if (wr->ib_sge) {
1653 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1654 kfree(wr->ib_sge);
1655 wr->ib_sge = NULL;
1656 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001657}
1658
1659static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001660isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001661{
1662 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001663 LIST_HEAD(unmap_list);
1664
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001665 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001666
1667 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001668 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001669 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001670 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1671 isert_unmap_data_buf(isert_conn, &wr->prot);
1672 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1673 }
Vu Pham59464ef2013-08-28 23:23:35 +03001674 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001675 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001676 spin_unlock_bh(&isert_conn->conn_lock);
1677 wr->fr_desc = NULL;
1678 }
1679
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001680 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001681 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001682 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001683 }
1684
1685 wr->ib_sge = NULL;
1686 wr->send_wr = NULL;
1687}
1688
1689static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001690isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001691{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001692 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001693 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001694 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001695 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696
1697 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1698
1699 switch (cmd->iscsi_opcode) {
1700 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701 spin_lock_bh(&conn->cmd_lock);
1702 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001703 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704 spin_unlock_bh(&conn->cmd_lock);
1705
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001706 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001708 /*
1709 * Check for special case during comp_err where
1710 * WRITE_PENDING has been handed off from core,
1711 * but requires an extra target_put_sess_cmd()
1712 * before transport_generic_free_cmd() below.
1713 */
1714 if (comp_err &&
1715 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1716 struct se_cmd *se_cmd = &cmd->se_cmd;
1717
1718 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1719 }
1720 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001721
Vu Phamd40945d2013-08-28 23:23:34 +03001722 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001723 transport_generic_free_cmd(&cmd->se_cmd, 0);
1724 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001725 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001726 spin_lock_bh(&conn->cmd_lock);
1727 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001728 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001729 spin_unlock_bh(&conn->cmd_lock);
1730
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001731 transport_generic_free_cmd(&cmd->se_cmd, 0);
1732 break;
1733 case ISCSI_OP_REJECT:
1734 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001735 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001736 spin_lock_bh(&conn->cmd_lock);
1737 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001738 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739 spin_unlock_bh(&conn->cmd_lock);
1740
1741 /*
1742 * Handle special case for REJECT when iscsi_add_reject*() has
1743 * overwritten the original iscsi_opcode assignment, and the
1744 * associated cmd->se_cmd needs to be released.
1745 */
1746 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001747 pr_debug("Calling transport_generic_free_cmd from"
1748 " isert_put_cmd for 0x%02x\n",
1749 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 transport_generic_free_cmd(&cmd->se_cmd, 0);
1751 break;
1752 }
1753 /*
1754 * Fall-through
1755 */
1756 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001757 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001758 break;
1759 }
1760}
1761
1762static void
1763isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1764{
1765 if (tx_desc->dma_addr != 0) {
1766 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1767 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1768 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1769 tx_desc->dma_addr = 0;
1770 }
1771}
1772
1773static void
1774isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001775 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001776{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001777 if (isert_cmd->pdu_buf_dma != 0) {
1778 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1779 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1780 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1781 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001782 }
1783
1784 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001785 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786}
1787
Sagi Grimberg96b79732014-03-17 12:52:18 +02001788static int
1789isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1790{
1791 struct ib_mr_status mr_status;
1792 int ret;
1793
1794 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1795 if (ret) {
1796 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1797 goto fail_mr_status;
1798 }
1799
1800 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1801 u64 sec_offset_err;
1802 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1803
1804 switch (mr_status.sig_err.err_type) {
1805 case IB_SIG_BAD_GUARD:
1806 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1807 break;
1808 case IB_SIG_BAD_REFTAG:
1809 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1810 break;
1811 case IB_SIG_BAD_APPTAG:
1812 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1813 break;
1814 }
1815 sec_offset_err = mr_status.sig_err.sig_err_offset;
1816 do_div(sec_offset_err, block_size);
1817 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1818
1819 pr_err("isert: PI error found type %d at sector 0x%llx "
1820 "expected 0x%x vs actual 0x%x\n",
1821 mr_status.sig_err.err_type,
1822 (unsigned long long)se_cmd->bad_sector,
1823 mr_status.sig_err.expected,
1824 mr_status.sig_err.actual);
1825 ret = 1;
1826 }
1827
1828fail_mr_status:
1829 return ret;
1830}
1831
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001832static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001833isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1834 struct isert_cmd *isert_cmd)
1835{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001836 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001837 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001838 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001839 struct isert_conn *isert_conn = isert_cmd->conn;
1840 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001841 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001842
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001843 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001844 ret = isert_check_pi_status(se_cmd,
1845 wr->fr_desc->pi_ctx->sig_mr);
1846 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001847 }
1848
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001849 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001850 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001851 if (ret)
1852 transport_send_check_condition_and_sense(se_cmd,
1853 se_cmd->pi_err, 0);
1854 else
1855 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001856}
1857
1858static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001859isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1860 struct isert_cmd *isert_cmd)
1861{
1862 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001863 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001864 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001865 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001866 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001867 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001868
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001869 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001870 ret = isert_check_pi_status(se_cmd,
1871 wr->fr_desc->pi_ctx->sig_mr);
1872 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001873 }
1874
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001875 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001876 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001877 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001878 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001879
Vu Pham90ecc6e2013-08-28 23:23:33 +03001880 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001881 spin_lock_bh(&cmd->istate_lock);
1882 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1883 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1884 spin_unlock_bh(&cmd->istate_lock);
1885
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001886 if (ret)
1887 transport_send_check_condition_and_sense(se_cmd,
1888 se_cmd->pi_err, 0);
1889 else
1890 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001891}
1892
1893static void
1894isert_do_control_comp(struct work_struct *work)
1895{
1896 struct isert_cmd *isert_cmd = container_of(work,
1897 struct isert_cmd, comp_work);
1898 struct isert_conn *isert_conn = isert_cmd->conn;
1899 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001900 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001901
1902 switch (cmd->i_state) {
1903 case ISTATE_SEND_TASKMGTRSP:
1904 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1905
1906 atomic_dec(&isert_conn->post_send_buf_count);
1907 iscsit_tmr_post_handler(cmd, cmd->conn);
1908
1909 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001910 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001911 break;
1912 case ISTATE_SEND_REJECT:
1913 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1914 atomic_dec(&isert_conn->post_send_buf_count);
1915
1916 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001917 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001918 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001919 case ISTATE_SEND_LOGOUTRSP:
1920 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001921
1922 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001923 iscsit_logout_post_handler(cmd, cmd->conn);
1924 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001925 case ISTATE_SEND_TEXTRSP:
1926 atomic_dec(&isert_conn->post_send_buf_count);
1927 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001928 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001929 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001930 default:
1931 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1932 dump_stack();
1933 break;
1934 }
1935}
1936
1937static void
1938isert_response_completion(struct iser_tx_desc *tx_desc,
1939 struct isert_cmd *isert_cmd,
1940 struct isert_conn *isert_conn,
1941 struct ib_device *ib_dev)
1942{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001943 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001944 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001945
1946 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001947 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001948 cmd->i_state == ISTATE_SEND_REJECT ||
1949 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001950 isert_unmap_tx_desc(tx_desc, ib_dev);
1951
1952 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1953 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1954 return;
1955 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001956
1957 /**
1958 * If send_wr_num is 0 this means that we got
1959 * RDMA completion and we cleared it and we should
1960 * simply decrement the response post. else the
1961 * response is incorporated in send_wr_num, just
1962 * sub it.
1963 **/
1964 if (wr->send_wr_num)
1965 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1966 else
1967 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001968
1969 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001970 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001971}
1972
1973static void
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001974isert_send_completion(struct iser_tx_desc *tx_desc,
1975 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001976{
1977 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1978 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1979 struct isert_rdma_wr *wr;
1980
1981 if (!isert_cmd) {
1982 atomic_dec(&isert_conn->post_send_buf_count);
1983 isert_unmap_tx_desc(tx_desc, ib_dev);
1984 return;
1985 }
1986 wr = &isert_cmd->rdma_wr;
1987
1988 switch (wr->iser_ib_op) {
1989 case ISER_IB_RECV:
1990 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1991 dump_stack();
1992 break;
1993 case ISER_IB_SEND:
1994 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1995 isert_response_completion(tx_desc, isert_cmd,
1996 isert_conn, ib_dev);
1997 break;
1998 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001999 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002000 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002001 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002002 break;
2003 case ISER_IB_RDMA_READ:
2004 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
2005
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002006 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002007 isert_completion_rdma_read(tx_desc, isert_cmd);
2008 break;
2009 default:
2010 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
2011 dump_stack();
2012 break;
2013 }
2014}
2015
2016static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002017isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002018{
2019 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002020 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002021
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002022 if (!isert_cmd)
2023 isert_unmap_tx_desc(tx_desc, ib_dev);
2024 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002025 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002026}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002027
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002028static void
2029isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2030{
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002031 struct iscsi_conn *conn = isert_conn->conn;
2032
2033 if (isert_conn->post_recv_buf_count)
2034 return;
2035
2036 if (conn->sess) {
2037 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2038 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039 }
2040
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002041 while (atomic_read(&isert_conn->post_send_buf_count))
2042 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002044 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002045 isert_conn_terminate(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002046 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002047
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002048 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2049
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002050 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002051}
2052
2053static void
2054isert_cq_tx_work(struct work_struct *work)
2055{
2056 struct isert_cq_desc *cq_desc = container_of(work,
2057 struct isert_cq_desc, cq_tx_work);
2058 struct isert_device *device = cq_desc->device;
2059 int cq_index = cq_desc->cq_index;
2060 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2061 struct isert_conn *isert_conn;
2062 struct iser_tx_desc *tx_desc;
2063 struct ib_wc wc;
2064
2065 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2066 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2067 isert_conn = wc.qp->qp_context;
2068
2069 if (wc.status == IB_WC_SUCCESS) {
2070 isert_send_completion(tx_desc, isert_conn);
2071 } else {
2072 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2073 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002074 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002075
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002076 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2077 atomic_dec(&isert_conn->post_send_buf_count);
2078 isert_cq_tx_comp_err(tx_desc, isert_conn);
2079 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002080 }
2081 }
2082
2083 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2084}
2085
2086static void
2087isert_cq_tx_callback(struct ib_cq *cq, void *context)
2088{
2089 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2090
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2092}
2093
2094static void
2095isert_cq_rx_work(struct work_struct *work)
2096{
2097 struct isert_cq_desc *cq_desc = container_of(work,
2098 struct isert_cq_desc, cq_rx_work);
2099 struct isert_device *device = cq_desc->device;
2100 int cq_index = cq_desc->cq_index;
2101 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2102 struct isert_conn *isert_conn;
2103 struct iser_rx_desc *rx_desc;
2104 struct ib_wc wc;
2105 unsigned long xfer_len;
2106
2107 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2108 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2109 isert_conn = wc.qp->qp_context;
2110
2111 if (wc.status == IB_WC_SUCCESS) {
2112 xfer_len = (unsigned long)wc.byte_len;
2113 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2114 } else {
2115 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002116 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002117 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002118 pr_debug("RX wc.vendor_err: 0x%08x\n",
2119 wc.vendor_err);
2120 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002121 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002122 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002123 }
2124 }
2125
2126 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2127}
2128
2129static void
2130isert_cq_rx_callback(struct ib_cq *cq, void *context)
2131{
2132 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2133
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002134 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2135}
2136
2137static int
2138isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2139{
2140 struct ib_send_wr *wr_failed;
2141 int ret;
2142
2143 atomic_inc(&isert_conn->post_send_buf_count);
2144
2145 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2146 &wr_failed);
2147 if (ret) {
2148 pr_err("ib_post_send failed with %d\n", ret);
2149 atomic_dec(&isert_conn->post_send_buf_count);
2150 return ret;
2151 }
2152 return ret;
2153}
2154
2155static int
2156isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2157{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002158 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002159 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2160 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2161 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2162 &isert_cmd->tx_desc.iscsi_header;
2163
2164 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2165 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2166 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2167 /*
2168 * Attach SENSE DATA payload to iSCSI Response PDU
2169 */
2170 if (cmd->se_cmd.sense_buffer &&
2171 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2172 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2173 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2174 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002175 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002176
2177 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2178 cmd->sense_buffer);
2179 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2180
2181 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2182 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002183 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002184
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002185 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2186 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187 DMA_TO_DEVICE);
2188
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002189 isert_cmd->pdu_buf_len = pdu_len;
2190 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2191 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002192 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2193 isert_cmd->tx_desc.num_sge = 2;
2194 }
2195
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002196 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002197
2198 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2199
2200 return isert_post_response(isert_conn, isert_cmd);
2201}
2202
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002203static void
2204isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2205{
2206 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2207 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2208 struct isert_device *device = isert_conn->conn_device;
2209
2210 spin_lock_bh(&conn->cmd_lock);
2211 if (!list_empty(&cmd->i_conn_node))
2212 list_del_init(&cmd->i_conn_node);
2213 spin_unlock_bh(&conn->cmd_lock);
2214
2215 if (cmd->data_direction == DMA_TO_DEVICE)
2216 iscsit_stop_dataout_timer(cmd);
2217
2218 device->unreg_rdma_mem(isert_cmd, isert_conn);
2219}
2220
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002221static enum target_prot_op
2222isert_get_sup_prot_ops(struct iscsi_conn *conn)
2223{
2224 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2225 struct isert_device *device = isert_conn->conn_device;
2226
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002227 if (conn->tpg->tpg_attrib.t10_pi) {
2228 if (device->pi_capable) {
2229 pr_info("conn %p PI offload enabled\n", isert_conn);
2230 isert_conn->pi_support = true;
2231 return TARGET_PROT_ALL;
2232 }
2233 }
2234
2235 pr_info("conn %p PI offload disabled\n", isert_conn);
2236 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002237
2238 return TARGET_PROT_NORMAL;
2239}
2240
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002241static int
2242isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2243 bool nopout_response)
2244{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002245 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002246 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2247 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2248
2249 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2250 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2251 &isert_cmd->tx_desc.iscsi_header,
2252 nopout_response);
2253 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002254 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002255
Masanari Iida8b513d02013-05-21 23:13:12 +09002256 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002257
2258 return isert_post_response(isert_conn, isert_cmd);
2259}
2260
2261static int
2262isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2263{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002264 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002265 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2266 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2267
2268 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2269 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2270 &isert_cmd->tx_desc.iscsi_header);
2271 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002272 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002273
2274 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2275
2276 return isert_post_response(isert_conn, isert_cmd);
2277}
2278
2279static int
2280isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2281{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002282 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002283 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2284 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2285
2286 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2287 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2288 &isert_cmd->tx_desc.iscsi_header);
2289 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002290 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002291
2292 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2293
2294 return isert_post_response(isert_conn, isert_cmd);
2295}
2296
2297static int
2298isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2299{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002300 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002301 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2302 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002303 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2304 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2305 struct iscsi_reject *hdr =
2306 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002307
2308 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002309 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002310 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002311
2312 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002313 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002314 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2315 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002316 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2317 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002318 tx_dsg->length = ISCSI_HDR_LEN;
2319 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2320 isert_cmd->tx_desc.num_sge = 2;
2321
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002322 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002323
2324 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2325
2326 return isert_post_response(isert_conn, isert_cmd);
2327}
2328
2329static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002330isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2331{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002332 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002333 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2334 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2335 struct iscsi_text_rsp *hdr =
2336 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2337 u32 txt_rsp_len;
2338 int rc;
2339
2340 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002341 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002342 if (rc < 0)
2343 return rc;
2344
2345 txt_rsp_len = rc;
2346 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2347
2348 if (txt_rsp_len) {
2349 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2350 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2351 void *txt_rsp_buf = cmd->buf_ptr;
2352
2353 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2354 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2355
2356 isert_cmd->pdu_buf_len = txt_rsp_len;
2357 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2358 tx_dsg->length = txt_rsp_len;
2359 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2360 isert_cmd->tx_desc.num_sge = 2;
2361 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002362 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002363
2364 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2365
2366 return isert_post_response(isert_conn, isert_cmd);
2367}
2368
2369static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002370isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2371 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2372 u32 data_left, u32 offset)
2373{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002374 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002375 struct scatterlist *sg_start, *tmp_sg;
2376 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2377 u32 sg_off, page_off;
2378 int i = 0, sg_nents;
2379
2380 sg_off = offset / PAGE_SIZE;
2381 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2382 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2383 page_off = offset % PAGE_SIZE;
2384
2385 send_wr->sg_list = ib_sge;
2386 send_wr->num_sge = sg_nents;
2387 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2388 /*
2389 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2390 */
2391 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2392 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2393 (unsigned long long)tmp_sg->dma_address,
2394 tmp_sg->length, page_off);
2395
2396 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2397 ib_sge->length = min_t(u32, data_left,
2398 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2399 ib_sge->lkey = isert_conn->conn_mr->lkey;
2400
Vu Pham90ecc6e2013-08-28 23:23:33 +03002401 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2402 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 page_off = 0;
2404 data_left -= ib_sge->length;
2405 ib_sge++;
2406 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2407 }
2408
2409 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2410 send_wr->sg_list, send_wr->num_sge);
2411
2412 return sg_nents;
2413}
2414
2415static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002416isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2417 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418{
2419 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002420 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002421 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002422 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002423 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002424 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002425 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2426 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002427
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002428 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002430 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2431 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2432 se_cmd->t_data_nents, se_cmd->data_length,
2433 offset, wr->iser_ib_op, &wr->data);
2434 if (ret)
2435 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002437 data_left = data->len;
2438 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002439
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002440 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002442 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002443 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002444 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002445 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002446 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002447
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002448 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002449 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2450 GFP_KERNEL);
2451 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002452 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002453 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002454 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002455 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002456
2457 wr->isert_cmd = isert_cmd;
2458 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459
2460 for (i = 0; i < wr->send_wr_num; i++) {
2461 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2462 data_len = min(data_left, rdma_write_max);
2463
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002464 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002465 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2466 send_wr->opcode = IB_WR_RDMA_WRITE;
2467 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2468 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2469 if (i + 1 == wr->send_wr_num)
2470 send_wr->next = &isert_cmd->tx_desc.send_wr;
2471 else
2472 send_wr->next = &wr->send_wr[i + 1];
2473 } else {
2474 send_wr->opcode = IB_WR_RDMA_READ;
2475 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2476 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2477 if (i + 1 == wr->send_wr_num)
2478 send_wr->send_flags = IB_SEND_SIGNALED;
2479 else
2480 send_wr->next = &wr->send_wr[i + 1];
2481 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002482
2483 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2484 send_wr, data_len, offset);
2485 ib_sge += ib_sge_cnt;
2486
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002487 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002488 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002489 data_left -= data_len;
2490 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002491
2492 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002493unmap_cmd:
2494 isert_unmap_data_buf(isert_conn, data);
2495
Vu Pham90ecc6e2013-08-28 23:23:33 +03002496 return ret;
2497}
2498
2499static int
Vu Pham59464ef2013-08-28 23:23:35 +03002500isert_map_fr_pagelist(struct ib_device *ib_dev,
2501 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2502{
2503 u64 start_addr, end_addr, page, chunk_start = 0;
2504 struct scatterlist *tmp_sg;
2505 int i = 0, new_chunk, last_ent, n_pages;
2506
2507 n_pages = 0;
2508 new_chunk = 1;
2509 last_ent = sg_nents - 1;
2510 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2511 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2512 if (new_chunk)
2513 chunk_start = start_addr;
2514 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2515
2516 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2517 i, (unsigned long long)tmp_sg->dma_address,
2518 tmp_sg->length);
2519
2520 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2521 new_chunk = 0;
2522 continue;
2523 }
2524 new_chunk = 1;
2525
2526 page = chunk_start & PAGE_MASK;
2527 do {
2528 fr_pl[n_pages++] = page;
2529 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2530 n_pages - 1, page);
2531 page += PAGE_SIZE;
2532 } while (page < end_addr);
2533 }
2534
2535 return n_pages;
2536}
2537
2538static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002539isert_fast_reg_mr(struct isert_conn *isert_conn,
2540 struct fast_reg_descriptor *fr_desc,
2541 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002542 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002543 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002544{
Vu Pham59464ef2013-08-28 23:23:35 +03002545 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002546 struct ib_mr *mr;
2547 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002548 struct ib_send_wr fr_wr, inv_wr;
2549 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002550 int ret, pagelist_len;
2551 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002552 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002553
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002554 if (mem->dma_nents == 1) {
2555 sge->lkey = isert_conn->conn_mr->lkey;
2556 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2557 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002558 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2559 __func__, __LINE__, sge->addr, sge->length,
2560 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002561 return 0;
2562 }
2563
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002564 if (ind == ISERT_DATA_KEY_VALID) {
2565 /* Registering data buffer */
2566 mr = fr_desc->data_mr;
2567 frpl = fr_desc->data_frpl;
2568 } else {
2569 /* Registering protection buffer */
2570 mr = fr_desc->pi_ctx->prot_mr;
2571 frpl = fr_desc->pi_ctx->prot_frpl;
2572 }
2573
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002574 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002575
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002576 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002577 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002578
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002579 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002580 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002581
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002582 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002583 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002584 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002585 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002586 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002587 wr = &inv_wr;
2588 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002589 key = (u8)(mr->rkey & 0x000000FF);
2590 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002591 }
2592
2593 /* Prepare FASTREG WR */
2594 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002595 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002596 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002597 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2598 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002599 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2600 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002601 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002602 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002603 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2604
2605 if (!wr)
2606 wr = &fr_wr;
2607 else
2608 wr->next = &fr_wr;
2609
2610 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2611 if (ret) {
2612 pr_err("fast registration failed, ret:%d\n", ret);
2613 return ret;
2614 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002615 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002616
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002617 sge->lkey = mr->lkey;
2618 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002619 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002620
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002621 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2622 __func__, __LINE__, sge->addr, sge->length,
2623 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002624
2625 return ret;
2626}
2627
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002628static inline void
2629isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2630 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002631{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002632 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002633 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2634 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2635 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002636 /*
2637 * At the moment we hard code those, but if in the future
2638 * the target core would like to use it, we will take it
2639 * from se_cmd.
2640 */
2641 domain->sig.dif.apptag_check_mask = 0xffff;
2642 domain->sig.dif.app_escape = true;
2643 domain->sig.dif.ref_escape = true;
2644 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2645 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2646 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002647};
2648
2649static int
2650isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2651{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002652 switch (se_cmd->prot_op) {
2653 case TARGET_PROT_DIN_INSERT:
2654 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002655 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002656 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002657 break;
2658 case TARGET_PROT_DOUT_INSERT:
2659 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002660 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002661 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002662 break;
2663 case TARGET_PROT_DIN_PASS:
2664 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002665 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2666 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002667 break;
2668 default:
2669 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2670 return -EINVAL;
2671 }
2672
2673 return 0;
2674}
2675
2676static inline u8
2677isert_set_prot_checks(u8 prot_checks)
2678{
2679 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2680 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2681 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2682}
2683
2684static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002685isert_reg_sig_mr(struct isert_conn *isert_conn,
2686 struct se_cmd *se_cmd,
2687 struct isert_rdma_wr *rdma_wr,
2688 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002689{
2690 struct ib_send_wr sig_wr, inv_wr;
2691 struct ib_send_wr *bad_wr, *wr = NULL;
2692 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2693 struct ib_sig_attrs sig_attrs;
2694 int ret;
2695 u32 key;
2696
2697 memset(&sig_attrs, 0, sizeof(sig_attrs));
2698 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2699 if (ret)
2700 goto err;
2701
2702 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2703
2704 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2705 memset(&inv_wr, 0, sizeof(inv_wr));
2706 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002707 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002708 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2709 wr = &inv_wr;
2710 /* Bump the key */
2711 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2712 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2713 }
2714
2715 memset(&sig_wr, 0, sizeof(sig_wr));
2716 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002717 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002718 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002719 sig_wr.num_sge = 1;
2720 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2721 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2722 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2723 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002724 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002725
2726 if (!wr)
2727 wr = &sig_wr;
2728 else
2729 wr->next = &sig_wr;
2730
2731 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2732 if (ret) {
2733 pr_err("fast registration failed, ret:%d\n", ret);
2734 goto err;
2735 }
2736 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2737
Sagi Grimberg570db172014-12-02 16:57:31 +02002738 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2739 rdma_wr->ib_sg[SIG].addr = 0;
2740 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002741 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2742 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2743 /*
2744 * We have protection guards on the wire
2745 * so we need to set a larget transfer
2746 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002747 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002748
2749 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002750 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2751 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002752err:
2753 return ret;
2754}
2755
Vu Pham59464ef2013-08-28 23:23:35 +03002756static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002757isert_handle_prot_cmd(struct isert_conn *isert_conn,
2758 struct isert_cmd *isert_cmd,
2759 struct isert_rdma_wr *wr)
2760{
2761 struct isert_device *device = isert_conn->conn_device;
2762 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2763 int ret;
2764
2765 if (!wr->fr_desc->pi_ctx) {
2766 ret = isert_create_pi_ctx(wr->fr_desc,
2767 device->ib_device,
2768 isert_conn->conn_pd);
2769 if (ret) {
2770 pr_err("conn %p failed to allocate pi_ctx\n",
2771 isert_conn);
2772 return ret;
2773 }
2774 }
2775
2776 if (se_cmd->t_prot_sg) {
2777 ret = isert_map_data_buf(isert_conn, isert_cmd,
2778 se_cmd->t_prot_sg,
2779 se_cmd->t_prot_nents,
2780 se_cmd->prot_length,
2781 0, wr->iser_ib_op, &wr->prot);
2782 if (ret) {
2783 pr_err("conn %p failed to map protection buffer\n",
2784 isert_conn);
2785 return ret;
2786 }
2787
2788 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2789 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2790 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2791 if (ret) {
2792 pr_err("conn %p failed to fast reg mr\n",
2793 isert_conn);
2794 goto unmap_prot_cmd;
2795 }
2796 }
2797
2798 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2799 if (ret) {
2800 pr_err("conn %p failed to fast reg mr\n",
2801 isert_conn);
2802 goto unmap_prot_cmd;
2803 }
2804 wr->fr_desc->ind |= ISERT_PROTECTED;
2805
2806 return 0;
2807
2808unmap_prot_cmd:
2809 if (se_cmd->t_prot_sg)
2810 isert_unmap_data_buf(isert_conn, &wr->prot);
2811
2812 return ret;
2813}
2814
2815static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002816isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2817 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002818{
2819 struct se_cmd *se_cmd = &cmd->se_cmd;
2820 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002821 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002822 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002823 struct ib_send_wr *send_wr;
2824 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002825 u32 offset;
2826 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002827 unsigned long flags;
2828
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002829 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2830
2831 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2832 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2833 se_cmd->t_data_nents, se_cmd->data_length,
2834 offset, wr->iser_ib_op, &wr->data);
2835 if (ret)
2836 return ret;
2837
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002838 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002839 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2840 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2841 struct fast_reg_descriptor, list);
2842 list_del(&fr_desc->list);
2843 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2844 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002845 }
2846
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002847 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002848 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002849 if (ret)
2850 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002851
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002852 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002853 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002854 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002855 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002856
Sagi Grimberg570db172014-12-02 16:57:31 +02002857 ib_sg = &wr->ib_sg[SIG];
2858 } else {
2859 ib_sg = &wr->ib_sg[DATA];
2860 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002861
Sagi Grimberg570db172014-12-02 16:57:31 +02002862 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002863 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002864 wr->send_wr_num = 1;
2865 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2866 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002867 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002868
2869 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002870 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002871 send_wr->num_sge = 1;
2872 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2873 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2874 send_wr->opcode = IB_WR_RDMA_WRITE;
2875 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2876 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002877 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002878 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002879 } else {
2880 send_wr->opcode = IB_WR_RDMA_READ;
2881 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2882 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2883 send_wr->send_flags = IB_SEND_SIGNALED;
2884 }
2885
Vu Pham59464ef2013-08-28 23:23:35 +03002886 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002887
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002888unmap_cmd:
2889 if (fr_desc) {
2890 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2891 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2892 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2893 }
2894 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002895
Vu Pham59464ef2013-08-28 23:23:35 +03002896 return ret;
2897}
2898
2899static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002900isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2901{
2902 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002903 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002904 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2905 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002906 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002907 struct ib_send_wr *wr_failed;
2908 int rc;
2909
2910 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2911 isert_cmd, se_cmd->data_length);
2912 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002913 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002914 if (rc) {
2915 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2916 return rc;
2917 }
2918
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002919 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002920 /*
2921 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2922 */
2923 isert_create_send_desc(isert_conn, isert_cmd,
2924 &isert_cmd->tx_desc);
2925 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2926 &isert_cmd->tx_desc.iscsi_header);
2927 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2928 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002929 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002930 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002931 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002932 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002933
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002934 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002935
2936 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2937 if (rc) {
2938 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002939 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002940 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002941
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002942 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002943 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2944 "READ\n", isert_cmd);
2945 else
2946 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2947 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002948
Vu Pham90ecc6e2013-08-28 23:23:33 +03002949 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002950}
2951
2952static int
2953isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2954{
2955 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002956 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002957 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2958 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002959 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002960 struct ib_send_wr *wr_failed;
2961 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002962
Vu Pham90ecc6e2013-08-28 23:23:33 +03002963 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2964 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002965 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002966 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002967 if (rc) {
2968 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2969 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002970 }
2971
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002972 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002973
2974 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2975 if (rc) {
2976 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002977 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002978 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002979 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2980 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002981
Vu Pham90ecc6e2013-08-28 23:23:33 +03002982 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002983}
2984
2985static int
2986isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2987{
2988 int ret;
2989
2990 switch (state) {
2991 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2992 ret = isert_put_nopin(cmd, conn, false);
2993 break;
2994 default:
2995 pr_err("Unknown immediate state: 0x%02x\n", state);
2996 ret = -EINVAL;
2997 break;
2998 }
2999
3000 return ret;
3001}
3002
3003static int
3004isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3005{
3006 int ret;
3007
3008 switch (state) {
3009 case ISTATE_SEND_LOGOUTRSP:
3010 ret = isert_put_logout_rsp(cmd, conn);
3011 if (!ret) {
3012 pr_debug("Returning iSER Logout -EAGAIN\n");
3013 ret = -EAGAIN;
3014 }
3015 break;
3016 case ISTATE_SEND_NOPIN:
3017 ret = isert_put_nopin(cmd, conn, true);
3018 break;
3019 case ISTATE_SEND_TASKMGTRSP:
3020 ret = isert_put_tm_rsp(cmd, conn);
3021 break;
3022 case ISTATE_SEND_REJECT:
3023 ret = isert_put_reject(cmd, conn);
3024 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003025 case ISTATE_SEND_TEXTRSP:
3026 ret = isert_put_text_rsp(cmd, conn);
3027 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003028 case ISTATE_SEND_STATUS:
3029 /*
3030 * Special case for sending non GOOD SCSI status from TX thread
3031 * context during pre se_cmd excecution failure.
3032 */
3033 ret = isert_put_response(conn, cmd);
3034 break;
3035 default:
3036 pr_err("Unknown response state: 0x%02x\n", state);
3037 ret = -EINVAL;
3038 break;
3039 }
3040
3041 return ret;
3042}
3043
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003044struct rdma_cm_id *
3045isert_setup_id(struct isert_np *isert_np)
3046{
3047 struct iscsi_np *np = isert_np->np;
3048 struct rdma_cm_id *id;
3049 struct sockaddr *sa;
3050 int ret;
3051
3052 sa = (struct sockaddr *)&np->np_sockaddr;
3053 pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3054
3055 id = rdma_create_id(isert_cma_handler, isert_np,
3056 RDMA_PS_TCP, IB_QPT_RC);
3057 if (IS_ERR(id)) {
3058 pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3059 ret = PTR_ERR(id);
3060 goto out;
3061 }
3062 pr_debug("id %p context %p\n", id, id->context);
3063
3064 ret = rdma_bind_addr(id, sa);
3065 if (ret) {
3066 pr_err("rdma_bind_addr() failed: %d\n", ret);
3067 goto out_id;
3068 }
3069
3070 ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3071 if (ret) {
3072 pr_err("rdma_listen() failed: %d\n", ret);
3073 goto out_id;
3074 }
3075
3076 return id;
3077out_id:
3078 rdma_destroy_id(id);
3079out:
3080 return ERR_PTR(ret);
3081}
3082
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003083static int
3084isert_setup_np(struct iscsi_np *np,
3085 struct __kernel_sockaddr_storage *ksockaddr)
3086{
3087 struct isert_np *isert_np;
3088 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003089 int ret;
3090
3091 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3092 if (!isert_np) {
3093 pr_err("Unable to allocate struct isert_np\n");
3094 return -ENOMEM;
3095 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003096 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003097 mutex_init(&isert_np->np_accept_mutex);
3098 INIT_LIST_HEAD(&isert_np->np_accept_list);
3099 init_completion(&isert_np->np_login_comp);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003100 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003101
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003102 /*
3103 * Setup the np->np_sockaddr from the passed sockaddr setup
3104 * in iscsi_target_configfs.c code..
3105 */
3106 memcpy(&np->np_sockaddr, ksockaddr,
3107 sizeof(struct __kernel_sockaddr_storage));
3108
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003109 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003110 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003111 ret = PTR_ERR(isert_lid);
3112 goto out;
3113 }
3114
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003115 isert_np->np_cm_id = isert_lid;
3116 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003117
3118 return 0;
3119
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003120out:
3121 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003122
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003123 return ret;
3124}
3125
3126static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003127isert_rdma_accept(struct isert_conn *isert_conn)
3128{
3129 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3130 struct rdma_conn_param cp;
3131 int ret;
3132
3133 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003134 cp.initiator_depth = isert_conn->initiator_depth;
3135 cp.retry_count = 7;
3136 cp.rnr_retry_count = 7;
3137
3138 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3139
3140 ret = rdma_accept(cm_id, &cp);
3141 if (ret) {
3142 pr_err("rdma_accept() failed with: %d\n", ret);
3143 return ret;
3144 }
3145
3146 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3147
3148 return 0;
3149}
3150
3151static int
3152isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3153{
3154 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3155 int ret;
3156
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003157 pr_info("before login_req comp conn: %p\n", isert_conn);
3158 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3159 if (ret) {
3160 pr_err("isert_conn %p interrupted before got login req\n",
3161 isert_conn);
3162 return ret;
3163 }
3164 reinit_completion(&isert_conn->login_req_comp);
3165
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003166 /*
3167 * For login requests after the first PDU, isert_rx_login_req() will
3168 * kick schedule_delayed_work(&conn->login_work) as the packet is
3169 * received, which turns this callback from iscsi_target_do_login_rx()
3170 * into a NOP.
3171 */
3172 if (!login->first_request)
3173 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003174
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003175 isert_rx_login_req(isert_conn);
3176
3177 pr_info("before conn_login_comp conn: %p\n", conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003178 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3179 if (ret)
3180 return ret;
3181
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003182 pr_info("processing login->req: %p\n", login->req);
3183
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003184 return 0;
3185}
3186
3187static void
3188isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3189 struct isert_conn *isert_conn)
3190{
3191 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3192 struct rdma_route *cm_route = &cm_id->route;
3193 struct sockaddr_in *sock_in;
3194 struct sockaddr_in6 *sock_in6;
3195
3196 conn->login_family = np->np_sockaddr.ss_family;
3197
3198 if (np->np_sockaddr.ss_family == AF_INET6) {
3199 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3200 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3201 &sock_in6->sin6_addr.in6_u);
3202 conn->login_port = ntohs(sock_in6->sin6_port);
3203
3204 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3205 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3206 &sock_in6->sin6_addr.in6_u);
3207 conn->local_port = ntohs(sock_in6->sin6_port);
3208 } else {
3209 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3210 sprintf(conn->login_ip, "%pI4",
3211 &sock_in->sin_addr.s_addr);
3212 conn->login_port = ntohs(sock_in->sin_port);
3213
3214 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3215 sprintf(conn->local_ip, "%pI4",
3216 &sock_in->sin_addr.s_addr);
3217 conn->local_port = ntohs(sock_in->sin_port);
3218 }
3219}
3220
3221static int
3222isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3223{
3224 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3225 struct isert_conn *isert_conn;
3226 int max_accept = 0, ret;
3227
3228accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003229 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003230 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003231 return -ENODEV;
3232
3233 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003234 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003235 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003236 pr_debug("np_thread_state %d for isert_accept_np\n",
3237 np->np_thread_state);
3238 /**
3239 * No point in stalling here when np_thread
3240 * is in state RESET/SHUTDOWN/EXIT - bail
3241 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003242 return -ENODEV;
3243 }
3244 spin_unlock_bh(&np->np_thread_lock);
3245
3246 mutex_lock(&isert_np->np_accept_mutex);
3247 if (list_empty(&isert_np->np_accept_list)) {
3248 mutex_unlock(&isert_np->np_accept_mutex);
3249 max_accept++;
3250 goto accept_wait;
3251 }
3252 isert_conn = list_first_entry(&isert_np->np_accept_list,
3253 struct isert_conn, conn_accept_node);
3254 list_del_init(&isert_conn->conn_accept_node);
3255 mutex_unlock(&isert_np->np_accept_mutex);
3256
3257 conn->context = isert_conn;
3258 isert_conn->conn = conn;
3259 max_accept = 0;
3260
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003261 isert_set_conn_info(np, conn, isert_conn);
3262
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003263 pr_debug("Processing isert_conn: %p\n", isert_conn);
3264
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265 return 0;
3266}
3267
3268static void
3269isert_free_np(struct iscsi_np *np)
3270{
3271 struct isert_np *isert_np = (struct isert_np *)np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003272 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003273
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003274 if (isert_np->np_cm_id)
3275 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003276
Sagi Grimberg268e6812014-12-02 16:57:36 +02003277 /*
3278 * FIXME: At this point we don't have a good way to insure
3279 * that at this point we don't have hanging connections that
3280 * completed RDMA establishment but didn't start iscsi login
3281 * process. So work-around this by cleaning up what ever piled
3282 * up in np_accept_list.
3283 */
3284 mutex_lock(&isert_np->np_accept_mutex);
3285 if (!list_empty(&isert_np->np_accept_list)) {
3286 pr_info("Still have isert connections, cleaning up...\n");
3287 list_for_each_entry_safe(isert_conn, n,
3288 &isert_np->np_accept_list,
3289 conn_accept_node) {
3290 pr_info("cleaning isert_conn %p state (%d)\n",
3291 isert_conn, isert_conn->state);
3292 isert_connect_release(isert_conn);
3293 }
3294 }
3295 mutex_unlock(&isert_np->np_accept_mutex);
3296
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
3307 pr_info("Starting release conn %p\n", isert_conn);
3308
3309 wait_for_completion(&isert_conn->conn_wait);
3310
3311 mutex_lock(&isert_conn->conn_mutex);
3312 isert_conn->state = ISER_CONN_DOWN;
3313 mutex_unlock(&isert_conn->conn_mutex);
3314
3315 pr_info("Destroying conn %p\n", isert_conn);
3316 isert_put_conn(isert_conn);
3317}
3318
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003319static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003320{
3321 struct isert_conn *isert_conn = conn->context;
3322
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003323 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003324
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003325 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003326 /*
3327 * Only wait for conn_wait_comp_err if the isert_conn made it
3328 * into full feature phase..
3329 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003330 if (isert_conn->state == ISER_CONN_INIT) {
3331 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003332 return;
3333 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003334 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003335 mutex_unlock(&isert_conn->conn_mutex);
3336
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003337 wait_for_completion(&isert_conn->conn_wait_comp_err);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003338
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003339 INIT_WORK(&isert_conn->release_work, isert_release_work);
3340 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003341}
3342
3343static void isert_free_conn(struct iscsi_conn *conn)
3344{
3345 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003346
3347 isert_put_conn(isert_conn);
3348}
3349
3350static struct iscsit_transport iser_target_transport = {
3351 .name = "IB/iSER",
3352 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003353 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003354 .owner = THIS_MODULE,
3355 .iscsit_setup_np = isert_setup_np,
3356 .iscsit_accept_np = isert_accept_np,
3357 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003358 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003359 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003360 .iscsit_get_login_rx = isert_get_login_rx,
3361 .iscsit_put_login_tx = isert_put_login_tx,
3362 .iscsit_immediate_queue = isert_immediate_queue,
3363 .iscsit_response_queue = isert_response_queue,
3364 .iscsit_get_dataout = isert_get_dataout,
3365 .iscsit_queue_data_in = isert_put_datain,
3366 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003367 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003368 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003369};
3370
3371static int __init isert_init(void)
3372{
3373 int ret;
3374
3375 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3376 if (!isert_rx_wq) {
3377 pr_err("Unable to allocate isert_rx_wq\n");
3378 return -ENOMEM;
3379 }
3380
3381 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3382 if (!isert_comp_wq) {
3383 pr_err("Unable to allocate isert_comp_wq\n");
3384 ret = -ENOMEM;
3385 goto destroy_rx_wq;
3386 }
3387
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003388 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3389 WQ_UNBOUND_MAX_ACTIVE);
3390 if (!isert_release_wq) {
3391 pr_err("Unable to allocate isert_release_wq\n");
3392 ret = -ENOMEM;
3393 goto destroy_comp_wq;
3394 }
3395
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003396 iscsit_register_transport(&iser_target_transport);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003397 pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3398
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003399 return 0;
3400
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003401destroy_comp_wq:
3402 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003403destroy_rx_wq:
3404 destroy_workqueue(isert_rx_wq);
3405 return ret;
3406}
3407
3408static void __exit isert_exit(void)
3409{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003410 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003411 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003412 destroy_workqueue(isert_comp_wq);
3413 destroy_workqueue(isert_rx_wq);
3414 iscsit_unregister_transport(&iser_target_transport);
3415 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3416}
3417
3418MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3419MODULE_VERSION("0.1");
3420MODULE_AUTHOR("nab@Linux-iSCSI.org");
3421MODULE_LICENSE("GPL");
3422
3423module_init(isert_init);
3424module_exit(isert_exit);