blob: 33b549e752c14d3b40fc33a4f344798dca6aced4 [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>
Nicholas Bellinger95b60f02013-11-05 13:16:12 -080025#include <linux/llist.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080026#include <rdma/ib_verbs.h>
27#include <rdma/rdma_cm.h>
28#include <target/target_core_base.h>
29#include <target/target_core_fabric.h>
30#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030031#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032
33#include "isert_proto.h"
34#include "ib_isert.h"
35
36#define ISERT_MAX_CONN 8
37#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
39
40static DEFINE_MUTEX(device_list_mutex);
41static LIST_HEAD(device_list);
42static struct workqueue_struct *isert_rx_wq;
43static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020044static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080045
46static void
Vu Phamd40945d2013-08-28 23:23:34 +030047isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
48static int
49isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
50 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030051static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020052isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030053static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020054isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
55 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020056static int
57isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020058static int
59isert_rdma_post_recvl(struct isert_conn *isert_conn);
60static int
61isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020062struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030063
64static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080065isert_qp_event_callback(struct ib_event *e, void *context)
66{
67 struct isert_conn *isert_conn = (struct isert_conn *)context;
68
69 pr_err("isert_qp_event_callback event: %d\n", e->event);
70 switch (e->event) {
71 case IB_EVENT_COMM_EST:
72 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
73 break;
74 case IB_EVENT_QP_LAST_WQE_REACHED:
75 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
76 break;
77 default:
78 break;
79 }
80}
81
82static int
83isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
84{
85 int ret;
86
87 ret = ib_query_device(ib_dev, devattr);
88 if (ret) {
89 pr_err("ib_query_device() failed: %d\n", ret);
90 return ret;
91 }
92 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
93 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
94
95 return 0;
96}
97
98static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +020099isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
100 u8 protection)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800101{
102 struct isert_device *device = isert_conn->conn_device;
103 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104 int ret, index, min_index = 0;
105
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106 mutex_lock(&device_list_mutex);
107 for (index = 0; index < device->cqs_used; index++)
108 if (device->cq_active_qps[index] <
109 device->cq_active_qps[min_index])
110 min_index = index;
111 device->cq_active_qps[min_index]++;
112 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
113 mutex_unlock(&device_list_mutex);
114
115 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
116 attr.event_handler = isert_qp_event_callback;
117 attr.qp_context = isert_conn;
118 attr.send_cq = device->dev_tx_cq[min_index];
119 attr.recv_cq = device->dev_rx_cq[min_index];
120 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
121 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
122 /*
123 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700124 * work-around for RDMA_READs with ConnectX-2.
125 *
126 * Also, still make sure to have at least two SGEs for
127 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800128 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700129 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800130 isert_conn->max_sge = attr.cap.max_send_sge;
131
132 attr.cap.max_recv_sge = 1;
133 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
134 attr.qp_type = IB_QPT_RC;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200135 if (protection)
136 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800137
138 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
139 cma_id->device);
140 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
141 isert_conn->conn_pd->device);
142
143 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
144 if (ret) {
145 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200146 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800147 }
148 isert_conn->conn_qp = cma_id->qp;
149 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
150
151 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200152err:
153 mutex_lock(&device_list_mutex);
154 device->cq_active_qps[min_index]--;
155 mutex_unlock(&device_list_mutex);
156
157 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158}
159
160static void
161isert_cq_event_callback(struct ib_event *e, void *context)
162{
163 pr_debug("isert_cq_event_callback event: %d\n", e->event);
164}
165
166static int
167isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
168{
169 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
170 struct iser_rx_desc *rx_desc;
171 struct ib_sge *rx_sg;
172 u64 dma_addr;
173 int i, j;
174
175 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
176 sizeof(struct iser_rx_desc), GFP_KERNEL);
177 if (!isert_conn->conn_rx_descs)
178 goto fail;
179
180 rx_desc = isert_conn->conn_rx_descs;
181
182 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
183 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
184 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
185 if (ib_dma_mapping_error(ib_dev, dma_addr))
186 goto dma_map_fail;
187
188 rx_desc->dma_addr = dma_addr;
189
190 rx_sg = &rx_desc->rx_sg;
191 rx_sg->addr = rx_desc->dma_addr;
192 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
193 rx_sg->lkey = isert_conn->conn_mr->lkey;
194 }
195
196 isert_conn->conn_rx_desc_head = 0;
197 return 0;
198
199dma_map_fail:
200 rx_desc = isert_conn->conn_rx_descs;
201 for (j = 0; j < i; j++, rx_desc++) {
202 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
203 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
204 }
205 kfree(isert_conn->conn_rx_descs);
206 isert_conn->conn_rx_descs = NULL;
207fail:
208 return -ENOMEM;
209}
210
211static void
212isert_free_rx_descriptors(struct isert_conn *isert_conn)
213{
214 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
215 struct iser_rx_desc *rx_desc;
216 int i;
217
218 if (!isert_conn->conn_rx_descs)
219 return;
220
221 rx_desc = isert_conn->conn_rx_descs;
222 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
223 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
224 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
225 }
226
227 kfree(isert_conn->conn_rx_descs);
228 isert_conn->conn_rx_descs = NULL;
229}
230
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800231static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800232static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800233static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800234static void isert_cq_rx_callback(struct ib_cq *, void *);
235
236static int
237isert_create_device_ib_res(struct isert_device *device)
238{
239 struct ib_device *ib_dev = device->ib_device;
240 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300241 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800242 int ret = 0, i, j;
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000243 int max_rx_cqe, max_tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244
Vu Pham59464ef2013-08-28 23:23:35 +0300245 dev_attr = &device->dev_attr;
246 ret = isert_query_device(ib_dev, dev_attr);
247 if (ret)
248 return ret;
249
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000250 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
251 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
252
Vu Phamd40945d2013-08-28 23:23:34 +0300253 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200254 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
255 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200256 device->use_fastreg = 1;
257 device->reg_rdma_mem = isert_reg_rdma;
258 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300259 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200260 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300261 device->reg_rdma_mem = isert_map_rdma;
262 device->unreg_rdma_mem = isert_unmap_cmd;
263 }
Vu Phamd40945d2013-08-28 23:23:34 +0300264
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200265 /* Check signature cap */
266 device->pi_capable = dev_attr->device_cap_flags &
267 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
268
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800269 device->cqs_used = min_t(int, num_online_cpus(),
270 device->ib_device->num_comp_vectors);
271 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200272 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200273 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800274 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200275 device->ib_device->num_comp_vectors, device->use_fastreg,
276 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
278 device->cqs_used, GFP_KERNEL);
279 if (!device->cq_desc) {
280 pr_err("Unable to allocate device->cq_desc\n");
281 return -ENOMEM;
282 }
283 cq_desc = device->cq_desc;
284
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800285 for (i = 0; i < device->cqs_used; i++) {
286 cq_desc[i].device = device;
287 cq_desc[i].cq_index = i;
288
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800289 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800290 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
291 isert_cq_rx_callback,
292 isert_cq_event_callback,
293 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000294 max_rx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800295 if (IS_ERR(device->dev_rx_cq[i])) {
296 ret = PTR_ERR(device->dev_rx_cq[i]);
297 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800298 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800299 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800300
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800301 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800302 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
303 isert_cq_tx_callback,
304 isert_cq_event_callback,
305 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000306 max_tx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800307 if (IS_ERR(device->dev_tx_cq[i])) {
308 ret = PTR_ERR(device->dev_tx_cq[i]);
309 device->dev_tx_cq[i] = NULL;
310 goto out_cq;
311 }
312
313 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
314 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800315 goto out_cq;
316
Wei Yongjun94a71112013-10-29 09:56:34 +0800317 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
318 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800319 goto out_cq;
320 }
321
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800322 return 0;
323
324out_cq:
325 for (j = 0; j < i; j++) {
326 cq_desc = &device->cq_desc[j];
327
328 if (device->dev_rx_cq[j]) {
329 cancel_work_sync(&cq_desc->cq_rx_work);
330 ib_destroy_cq(device->dev_rx_cq[j]);
331 }
332 if (device->dev_tx_cq[j]) {
333 cancel_work_sync(&cq_desc->cq_tx_work);
334 ib_destroy_cq(device->dev_tx_cq[j]);
335 }
336 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800337 kfree(device->cq_desc);
338
339 return ret;
340}
341
342static void
343isert_free_device_ib_res(struct isert_device *device)
344{
345 struct isert_cq_desc *cq_desc;
346 int i;
347
348 for (i = 0; i < device->cqs_used; i++) {
349 cq_desc = &device->cq_desc[i];
350
351 cancel_work_sync(&cq_desc->cq_rx_work);
352 cancel_work_sync(&cq_desc->cq_tx_work);
353 ib_destroy_cq(device->dev_rx_cq[i]);
354 ib_destroy_cq(device->dev_tx_cq[i]);
355 device->dev_rx_cq[i] = NULL;
356 device->dev_tx_cq[i] = NULL;
357 }
358
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800359 kfree(device->cq_desc);
360}
361
362static void
363isert_device_try_release(struct isert_device *device)
364{
365 mutex_lock(&device_list_mutex);
366 device->refcount--;
367 if (!device->refcount) {
368 isert_free_device_ib_res(device);
369 list_del(&device->dev_node);
370 kfree(device);
371 }
372 mutex_unlock(&device_list_mutex);
373}
374
375static struct isert_device *
376isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
377{
378 struct isert_device *device;
379 int ret;
380
381 mutex_lock(&device_list_mutex);
382 list_for_each_entry(device, &device_list, dev_node) {
383 if (device->ib_device->node_guid == cma_id->device->node_guid) {
384 device->refcount++;
385 mutex_unlock(&device_list_mutex);
386 return device;
387 }
388 }
389
390 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
391 if (!device) {
392 mutex_unlock(&device_list_mutex);
393 return ERR_PTR(-ENOMEM);
394 }
395
396 INIT_LIST_HEAD(&device->dev_node);
397
398 device->ib_device = cma_id->device;
399 ret = isert_create_device_ib_res(device);
400 if (ret) {
401 kfree(device);
402 mutex_unlock(&device_list_mutex);
403 return ERR_PTR(ret);
404 }
405
406 device->refcount++;
407 list_add_tail(&device->dev_node, &device_list);
408 mutex_unlock(&device_list_mutex);
409
410 return device;
411}
412
Vu Pham59464ef2013-08-28 23:23:35 +0300413static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200414isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300415{
416 struct fast_reg_descriptor *fr_desc, *tmp;
417 int i = 0;
418
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200419 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300420 return;
421
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200422 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300423
424 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200425 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300426 list_del(&fr_desc->list);
427 ib_free_fast_reg_page_list(fr_desc->data_frpl);
428 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200429 if (fr_desc->pi_ctx) {
430 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
431 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
432 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
433 kfree(fr_desc->pi_ctx);
434 }
Vu Pham59464ef2013-08-28 23:23:35 +0300435 kfree(fr_desc);
436 ++i;
437 }
438
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200439 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300440 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200441 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300442}
443
444static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200445isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200446 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200447{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200448 int ret;
449
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200450 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
451 ISCSI_ISER_SG_TABLESIZE);
452 if (IS_ERR(fr_desc->data_frpl)) {
453 pr_err("Failed to allocate data frpl err=%ld\n",
454 PTR_ERR(fr_desc->data_frpl));
455 return PTR_ERR(fr_desc->data_frpl);
456 }
457
458 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
459 if (IS_ERR(fr_desc->data_mr)) {
460 pr_err("Failed to allocate data frmr err=%ld\n",
461 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200462 ret = PTR_ERR(fr_desc->data_mr);
463 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200464 }
465 pr_debug("Create fr_desc %p page_list %p\n",
466 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200467 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200468
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200469 if (protection) {
470 struct ib_mr_init_attr mr_init_attr = {0};
471 struct pi_context *pi_ctx;
472
473 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
474 if (!fr_desc->pi_ctx) {
475 pr_err("Failed to allocate pi context\n");
476 ret = -ENOMEM;
477 goto err_data_mr;
478 }
479 pi_ctx = fr_desc->pi_ctx;
480
481 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
482 ISCSI_ISER_SG_TABLESIZE);
483 if (IS_ERR(pi_ctx->prot_frpl)) {
484 pr_err("Failed to allocate prot frpl err=%ld\n",
485 PTR_ERR(pi_ctx->prot_frpl));
486 ret = PTR_ERR(pi_ctx->prot_frpl);
487 goto err_pi_ctx;
488 }
489
490 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
491 if (IS_ERR(pi_ctx->prot_mr)) {
492 pr_err("Failed to allocate prot frmr err=%ld\n",
493 PTR_ERR(pi_ctx->prot_mr));
494 ret = PTR_ERR(pi_ctx->prot_mr);
495 goto err_prot_frpl;
496 }
497 fr_desc->ind |= ISERT_PROT_KEY_VALID;
498
499 mr_init_attr.max_reg_descriptors = 2;
500 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
501 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
502 if (IS_ERR(pi_ctx->sig_mr)) {
503 pr_err("Failed to allocate signature enabled mr err=%ld\n",
504 PTR_ERR(pi_ctx->sig_mr));
505 ret = PTR_ERR(pi_ctx->sig_mr);
506 goto err_prot_mr;
507 }
508 fr_desc->ind |= ISERT_SIG_KEY_VALID;
509 }
510 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200511
512 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200513err_prot_mr:
514 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
515err_prot_frpl:
516 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
517err_pi_ctx:
518 kfree(fr_desc->pi_ctx);
519err_data_mr:
520 ib_dereg_mr(fr_desc->data_mr);
521err_data_frpl:
522 ib_free_fast_reg_page_list(fr_desc->data_frpl);
523
524 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200525}
526
527static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200528isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
Vu Pham59464ef2013-08-28 23:23:35 +0300529{
530 struct fast_reg_descriptor *fr_desc;
531 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700532 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
533 struct se_node_acl *se_nacl = se_sess->se_node_acl;
534 int i, ret, tag_num;
535 /*
536 * Setup the number of FRMRs based upon the number of tags
537 * available to session in iscsi_target_locate_portal().
538 */
539 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
540 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300541
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200542 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700543 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300544 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
545 if (!fr_desc) {
546 pr_err("Failed to allocate fast_reg descriptor\n");
547 ret = -ENOMEM;
548 goto err;
549 }
550
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200551 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200552 isert_conn->conn_pd, fr_desc,
553 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200554 if (ret) {
555 pr_err("Failed to create fastreg descriptor err=%d\n",
556 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800557 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300558 goto err;
559 }
560
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200561 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
562 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300563 }
564
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200565 pr_debug("Creating conn %p fastreg pool size=%d",
566 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300567
568 return 0;
569
570err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200571 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300572 return ret;
573}
574
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800575static int
576isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
577{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200578 struct isert_np *isert_np = cma_id->context;
579 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800580 struct isert_conn *isert_conn;
581 struct isert_device *device;
582 struct ib_device *ib_dev = cma_id->device;
583 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300584 u8 pi_support;
585
586 spin_lock_bh(&np->np_thread_lock);
587 if (!np->enabled) {
588 spin_unlock_bh(&np->np_thread_lock);
589 pr_debug("iscsi_np is not enabled, reject connect request\n");
590 return rdma_reject(cma_id, NULL, 0);
591 }
592 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800593
594 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
595 cma_id, cma_id->context);
596
597 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
598 if (!isert_conn) {
599 pr_err("Unable to allocate isert_conn\n");
600 return -ENOMEM;
601 }
602 isert_conn->state = ISER_CONN_INIT;
603 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
604 init_completion(&isert_conn->conn_login_comp);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200605 init_completion(&isert_conn->login_req_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800606 init_completion(&isert_conn->conn_wait);
607 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800608 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700609 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300610 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700611 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800612
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800613 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800614
615 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
616 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
617 if (!isert_conn->login_buf) {
618 pr_err("Unable to allocate isert_conn->login_buf\n");
619 ret = -ENOMEM;
620 goto out;
621 }
622
623 isert_conn->login_req_buf = isert_conn->login_buf;
624 isert_conn->login_rsp_buf = isert_conn->login_buf +
625 ISCSI_DEF_MAX_RECV_SEG_LEN;
626 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
627 isert_conn->login_buf, isert_conn->login_req_buf,
628 isert_conn->login_rsp_buf);
629
630 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
631 (void *)isert_conn->login_req_buf,
632 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
633
634 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
635 if (ret) {
636 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
637 ret);
638 isert_conn->login_req_dma = 0;
639 goto out_login_buf;
640 }
641
642 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
643 (void *)isert_conn->login_rsp_buf,
644 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
645
646 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
647 if (ret) {
648 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
649 ret);
650 isert_conn->login_rsp_dma = 0;
651 goto out_req_dma_map;
652 }
653
654 device = isert_device_find_by_ib_dev(cma_id);
655 if (IS_ERR(device)) {
656 ret = PTR_ERR(device);
657 goto out_rsp_dma_map;
658 }
659
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300660 /* Set max inflight RDMA READ requests */
661 isert_conn->initiator_depth = min_t(u8,
662 event->param.conn.initiator_depth,
663 device->dev_attr.max_qp_init_rd_atom);
664 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
665
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800666 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200667 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
668 if (IS_ERR(isert_conn->conn_pd)) {
669 ret = PTR_ERR(isert_conn->conn_pd);
670 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
671 isert_conn, ret);
672 goto out_pd;
673 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800674
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200675 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
676 IB_ACCESS_LOCAL_WRITE);
677 if (IS_ERR(isert_conn->conn_mr)) {
678 ret = PTR_ERR(isert_conn->conn_mr);
679 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
680 isert_conn, ret);
681 goto out_mr;
682 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800683
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300684 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200685 if (pi_support && !device->pi_capable) {
Sagi Grimberg5f80ff82014-05-20 13:28:11 +0300686 pr_err("Protection information requested but not supported, "
687 "rejecting connect request\n");
688 ret = rdma_reject(cma_id, NULL, 0);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200689 goto out_mr;
690 }
691
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200692 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800693 if (ret)
694 goto out_conn_dev;
695
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200696 ret = isert_rdma_post_recvl(isert_conn);
697 if (ret)
698 goto out_conn_dev;
699
700 ret = isert_rdma_accept(isert_conn);
701 if (ret)
702 goto out_conn_dev;
703
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800704 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300705 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800706 mutex_unlock(&isert_np->np_accept_mutex);
707
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300708 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
709 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800710 return 0;
711
712out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200713 ib_dereg_mr(isert_conn->conn_mr);
714out_mr:
715 ib_dealloc_pd(isert_conn->conn_pd);
716out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800717 isert_device_try_release(device);
718out_rsp_dma_map:
719 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
720 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
721out_req_dma_map:
722 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
723 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
724out_login_buf:
725 kfree(isert_conn->login_buf);
726out:
727 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200728 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800729 return ret;
730}
731
732static void
733isert_connect_release(struct isert_conn *isert_conn)
734{
735 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
736 struct isert_device *device = isert_conn->conn_device;
737 int cq_index;
738
739 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
740
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200741 if (device && device->use_fastreg)
742 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300743
Sagi Grimberg19e20902014-12-02 16:57:26 +0200744 isert_free_rx_descriptors(isert_conn);
745 rdma_destroy_id(isert_conn->conn_cm_id);
746
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800747 if (isert_conn->conn_qp) {
748 cq_index = ((struct isert_cq_desc *)
749 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
750 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200751 mutex_lock(&device_list_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800752 isert_conn->conn_device->cq_active_qps[cq_index]--;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200753 mutex_unlock(&device_list_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800754
Sagi Grimberg19e20902014-12-02 16:57:26 +0200755 ib_destroy_qp(isert_conn->conn_qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756 }
757
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200758 ib_dereg_mr(isert_conn->conn_mr);
759 ib_dealloc_pd(isert_conn->conn_pd);
760
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800761 if (isert_conn->login_buf) {
762 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
763 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
764 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
765 ISCSI_DEF_MAX_RECV_SEG_LEN,
766 DMA_FROM_DEVICE);
767 kfree(isert_conn->login_buf);
768 }
769 kfree(isert_conn);
770
771 if (device)
772 isert_device_try_release(device);
773
774 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
775}
776
777static void
778isert_connected_handler(struct rdma_cm_id *cma_id)
779{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200780 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300781
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200782 pr_info("conn %p\n", isert_conn);
783
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200784 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
785 pr_warn("conn %p connect_release is running\n", isert_conn);
786 return;
787 }
788
789 mutex_lock(&isert_conn->conn_mutex);
790 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
791 isert_conn->state = ISER_CONN_UP;
792 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800793}
794
795static void
796isert_release_conn_kref(struct kref *kref)
797{
798 struct isert_conn *isert_conn = container_of(kref,
799 struct isert_conn, conn_kref);
800
801 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
802 current->comm, current->pid);
803
804 isert_connect_release(isert_conn);
805}
806
807static void
808isert_put_conn(struct isert_conn *isert_conn)
809{
810 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
811}
812
Sagi Grimberg954f2372014-12-02 16:57:17 +0200813/**
814 * isert_conn_terminate() - Initiate connection termination
815 * @isert_conn: isert connection struct
816 *
817 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200818 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200819 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200820 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200821 *
822 * This routine must be called with conn_mutex held. Thus it is
823 * safe to call multiple times.
824 */
825static void
826isert_conn_terminate(struct isert_conn *isert_conn)
827{
828 int err;
829
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200830 switch (isert_conn->state) {
831 case ISER_CONN_TERMINATING:
832 break;
833 case ISER_CONN_UP:
834 /*
835 * No flush completions will occur as we didn't
836 * get to ISER_CONN_FULL_FEATURE yet, complete
837 * to allow teardown progress.
838 */
839 complete(&isert_conn->conn_wait_comp_err);
840 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200841 pr_info("Terminating conn %p state %d\n",
842 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200843 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200844 err = rdma_disconnect(isert_conn->conn_cm_id);
845 if (err)
846 pr_warn("Failed rdma_disconnect isert_conn %p\n",
847 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200848 break;
849 default:
850 pr_warn("conn %p teminating in state %d\n",
851 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200852 }
853}
854
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700855static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200856isert_np_cma_handler(struct isert_np *isert_np,
857 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800858{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200859 pr_debug("isert np %p, handling event %d\n", isert_np, event);
860
861 switch (event) {
862 case RDMA_CM_EVENT_DEVICE_REMOVAL:
863 isert_np->np_cm_id = NULL;
864 break;
865 case RDMA_CM_EVENT_ADDR_CHANGE:
866 isert_np->np_cm_id = isert_setup_id(isert_np);
867 if (IS_ERR(isert_np->np_cm_id)) {
868 pr_err("isert np %p setup id failed: %ld\n",
869 isert_np, PTR_ERR(isert_np->np_cm_id));
870 isert_np->np_cm_id = NULL;
871 }
872 break;
873 default:
874 pr_err("isert np %p Unexpected event %d\n",
875 isert_np, event);
876 }
877
878 return -1;
879}
880
881static int
882isert_disconnected_handler(struct rdma_cm_id *cma_id,
883 enum rdma_cm_event_type event)
884{
885 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700886 struct isert_conn *isert_conn;
887
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200888 if (isert_np->np_cm_id == cma_id)
889 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700890
Sagi Grimberg19e20902014-12-02 16:57:26 +0200891 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800892
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200893 mutex_lock(&isert_conn->conn_mutex);
894 isert_conn_terminate(isert_conn);
895 mutex_unlock(&isert_conn->conn_mutex);
896
897 pr_info("conn %p completing conn_wait\n", isert_conn);
898 complete(&isert_conn->conn_wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700899
900 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901}
902
Sagi Grimberg954f2372014-12-02 16:57:17 +0200903static void
904isert_connect_error(struct rdma_cm_id *cma_id)
905{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200906 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200907
908 isert_put_conn(isert_conn);
909}
910
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800911static int
912isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
913{
914 int ret = 0;
915
916 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
917 event->event, event->status, cma_id->context, cma_id);
918
919 switch (event->event) {
920 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800921 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700922 if (ret)
923 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
924 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800925 break;
926 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800927 isert_connected_handler(cma_id);
928 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300929 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
930 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
931 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300932 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200933 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800934 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200935 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
936 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800937 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg954f2372014-12-02 16:57:17 +0200938 isert_connect_error(cma_id);
939 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800940 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300941 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800942 break;
943 }
944
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800945 return ret;
946}
947
948static int
949isert_post_recv(struct isert_conn *isert_conn, u32 count)
950{
951 struct ib_recv_wr *rx_wr, *rx_wr_failed;
952 int i, ret;
953 unsigned int rx_head = isert_conn->conn_rx_desc_head;
954 struct iser_rx_desc *rx_desc;
955
956 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
957 rx_desc = &isert_conn->conn_rx_descs[rx_head];
958 rx_wr->wr_id = (unsigned long)rx_desc;
959 rx_wr->sg_list = &rx_desc->rx_sg;
960 rx_wr->num_sge = 1;
961 rx_wr->next = rx_wr + 1;
962 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
963 }
964
965 rx_wr--;
966 rx_wr->next = NULL; /* mark end of work requests list */
967
968 isert_conn->post_recv_buf_count += count;
969 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
970 &rx_wr_failed);
971 if (ret) {
972 pr_err("ib_post_recv() failed with ret: %d\n", ret);
973 isert_conn->post_recv_buf_count -= count;
974 } else {
975 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
976 isert_conn->conn_rx_desc_head = rx_head;
977 }
978 return ret;
979}
980
981static int
982isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
983{
984 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
985 struct ib_send_wr send_wr, *send_wr_failed;
986 int ret;
987
988 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
989 ISER_HEADERS_LEN, DMA_TO_DEVICE);
990
991 send_wr.next = NULL;
992 send_wr.wr_id = (unsigned long)tx_desc;
993 send_wr.sg_list = tx_desc->tx_sg;
994 send_wr.num_sge = tx_desc->num_sge;
995 send_wr.opcode = IB_WR_SEND;
996 send_wr.send_flags = IB_SEND_SIGNALED;
997
998 atomic_inc(&isert_conn->post_send_buf_count);
999
1000 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1001 if (ret) {
1002 pr_err("ib_post_send() failed, ret: %d\n", ret);
1003 atomic_dec(&isert_conn->post_send_buf_count);
1004 }
1005
1006 return ret;
1007}
1008
1009static void
1010isert_create_send_desc(struct isert_conn *isert_conn,
1011 struct isert_cmd *isert_cmd,
1012 struct iser_tx_desc *tx_desc)
1013{
1014 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1015
1016 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1017 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1018
1019 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1020 tx_desc->iser_header.flags = ISER_VER;
1021
1022 tx_desc->num_sge = 1;
1023 tx_desc->isert_cmd = isert_cmd;
1024
1025 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1026 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1027 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1028 }
1029}
1030
1031static int
1032isert_init_tx_hdrs(struct isert_conn *isert_conn,
1033 struct iser_tx_desc *tx_desc)
1034{
1035 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1036 u64 dma_addr;
1037
1038 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1039 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1040 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1041 pr_err("ib_dma_mapping_error() failed\n");
1042 return -ENOMEM;
1043 }
1044
1045 tx_desc->dma_addr = dma_addr;
1046 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1047 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1048 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1049
1050 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1051 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1052 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1053
1054 return 0;
1055}
1056
1057static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001058isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1059 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001060{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001061 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1062
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001063 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1064 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1065 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001066 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001067 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001068 /*
1069 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
1070 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
1071 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001072 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001073 if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001074 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001075 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001076 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001077 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001078 return;
1079 }
1080 isert_conn->conn_comp_batch = 0;
1081 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001082 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001083
1084 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001085}
1086
1087static int
1088isert_rdma_post_recvl(struct isert_conn *isert_conn)
1089{
1090 struct ib_recv_wr rx_wr, *rx_wr_fail;
1091 struct ib_sge sge;
1092 int ret;
1093
1094 memset(&sge, 0, sizeof(struct ib_sge));
1095 sge.addr = isert_conn->login_req_dma;
1096 sge.length = ISER_RX_LOGIN_SIZE;
1097 sge.lkey = isert_conn->conn_mr->lkey;
1098
1099 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1100 sge.addr, sge.length, sge.lkey);
1101
1102 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1103 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1104 rx_wr.sg_list = &sge;
1105 rx_wr.num_sge = 1;
1106
1107 isert_conn->post_recv_buf_count++;
1108 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1109 if (ret) {
1110 pr_err("ib_post_recv() failed: %d\n", ret);
1111 isert_conn->post_recv_buf_count--;
1112 }
1113
1114 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1115 return ret;
1116}
1117
1118static int
1119isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1120 u32 length)
1121{
1122 struct isert_conn *isert_conn = conn->context;
1123 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1124 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1125 int ret;
1126
1127 isert_create_send_desc(isert_conn, NULL, tx_desc);
1128
1129 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1130 sizeof(struct iscsi_hdr));
1131
1132 isert_init_tx_hdrs(isert_conn, tx_desc);
1133
1134 if (length > 0) {
1135 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1136
1137 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1138 length, DMA_TO_DEVICE);
1139
1140 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1141
1142 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1143 length, DMA_TO_DEVICE);
1144
1145 tx_dsg->addr = isert_conn->login_rsp_dma;
1146 tx_dsg->length = length;
1147 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1148 tx_desc->num_sge = 2;
1149 }
1150 if (!login->login_failed) {
1151 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001152 if (!conn->sess->sess_ops->SessionType &&
1153 isert_conn->conn_device->use_fastreg) {
1154 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001155 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1156
1157 ret = isert_conn_create_fastreg_pool(isert_conn,
1158 pi_support);
1159 if (ret) {
1160 pr_err("Conn: %p failed to create"
1161 " fastreg pool\n", isert_conn);
1162 return ret;
1163 }
1164 }
1165
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001166 ret = isert_alloc_rx_descriptors(isert_conn);
1167 if (ret)
1168 return ret;
1169
1170 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1171 if (ret)
1172 return ret;
1173
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001174 /* Now we are in FULL_FEATURE phase */
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001175 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001176 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001177 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001178 goto post_send;
1179 }
1180
1181 ret = isert_rdma_post_recvl(isert_conn);
1182 if (ret)
1183 return ret;
1184 }
1185post_send:
1186 ret = isert_post_send(isert_conn, tx_desc);
1187 if (ret)
1188 return ret;
1189
1190 return 0;
1191}
1192
1193static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001194isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001195{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001196 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1197 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001198 struct iscsi_conn *conn = isert_conn->conn;
1199 struct iscsi_login *login = conn->conn_login;
1200 int size;
1201
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001202 pr_info("conn %p\n", isert_conn);
1203
1204 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001205
1206 if (login->first_request) {
1207 struct iscsi_login_req *login_req =
1208 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1209 /*
1210 * Setup the initial iscsi_login values from the leading
1211 * login request PDU.
1212 */
1213 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1214 login->current_stage =
1215 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1216 >> 2;
1217 login->version_min = login_req->min_version;
1218 login->version_max = login_req->max_version;
1219 memcpy(login->isid, login_req->isid, 6);
1220 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1221 login->init_task_tag = login_req->itt;
1222 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1223 login->cid = be16_to_cpu(login_req->cid);
1224 login->tsih = be16_to_cpu(login_req->tsih);
1225 }
1226
1227 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1228
1229 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1230 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1231 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1232 memcpy(login->req_buf, &rx_desc->data[0], size);
1233
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001234 if (login->first_request) {
1235 complete(&isert_conn->conn_login_comp);
1236 return;
1237 }
1238 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001239}
1240
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001241static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001242*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001243{
1244 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1245 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001246 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001247
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001248 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001249 if (!cmd) {
1250 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251 return NULL;
1252 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001253 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001255 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001256
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001257 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001258}
1259
1260static int
1261isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001262 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1263 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001265 struct iscsi_conn *conn = isert_conn->conn;
1266 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1267 struct scatterlist *sg;
1268 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1269 bool dump_payload = false;
1270
1271 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1272 if (rc < 0)
1273 return rc;
1274
1275 imm_data = cmd->immediate_data;
1276 imm_data_len = cmd->first_burst_len;
1277 unsol_data = cmd->unsolicited_data;
1278
1279 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1280 if (rc < 0) {
1281 return 0;
1282 } else if (rc > 0) {
1283 dump_payload = true;
1284 goto sequence_cmd;
1285 }
1286
1287 if (!imm_data)
1288 return 0;
1289
1290 sg = &cmd->se_cmd.t_data_sg[0];
1291 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1292
1293 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1294 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1295
1296 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1297
1298 cmd->write_data_done += imm_data_len;
1299
1300 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1301 spin_lock_bh(&cmd->istate_lock);
1302 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1303 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1304 spin_unlock_bh(&cmd->istate_lock);
1305 }
1306
1307sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001308 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001309
1310 if (!rc && dump_payload == false && unsol_data)
1311 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001312 else if (dump_payload && imm_data)
1313 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001314
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001315 return 0;
1316}
1317
1318static int
1319isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1320 struct iser_rx_desc *rx_desc, unsigned char *buf)
1321{
1322 struct scatterlist *sg_start;
1323 struct iscsi_conn *conn = isert_conn->conn;
1324 struct iscsi_cmd *cmd = NULL;
1325 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1326 u32 unsol_data_len = ntoh24(hdr->dlength);
1327 int rc, sg_nents, sg_off, page_off;
1328
1329 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1330 if (rc < 0)
1331 return rc;
1332 else if (!cmd)
1333 return 0;
1334 /*
1335 * FIXME: Unexpected unsolicited_data out
1336 */
1337 if (!cmd->unsolicited_data) {
1338 pr_err("Received unexpected solicited data payload\n");
1339 dump_stack();
1340 return -1;
1341 }
1342
1343 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1344 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1345
1346 sg_off = cmd->write_data_done / PAGE_SIZE;
1347 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1348 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1349 page_off = cmd->write_data_done % PAGE_SIZE;
1350 /*
1351 * FIXME: Non page-aligned unsolicited_data out
1352 */
1353 if (page_off) {
1354 pr_err("Received unexpected non-page aligned data payload\n");
1355 dump_stack();
1356 return -1;
1357 }
1358 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1359 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1360
1361 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1362 unsol_data_len);
1363
1364 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1365 if (rc < 0)
1366 return rc;
1367
1368 return 0;
1369}
1370
1371static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001372isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001373 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1374 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001375{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001376 struct iscsi_conn *conn = isert_conn->conn;
1377 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1378 int rc;
1379
1380 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1381 if (rc < 0)
1382 return rc;
1383 /*
1384 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1385 */
1386
1387 return iscsit_process_nop_out(conn, cmd, hdr);
1388}
1389
1390static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001391isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001392 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1393 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001394{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001395 struct iscsi_conn *conn = isert_conn->conn;
1396 u32 payload_length = ntoh24(hdr->dlength);
1397 int rc;
1398 unsigned char *text_in;
1399
1400 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1401 if (rc < 0)
1402 return rc;
1403
1404 text_in = kzalloc(payload_length, GFP_KERNEL);
1405 if (!text_in) {
1406 pr_err("Unable to allocate text_in of payload_length: %u\n",
1407 payload_length);
1408 return -ENOMEM;
1409 }
1410 cmd->text_in_ptr = text_in;
1411
1412 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1413
1414 return iscsit_process_text_cmd(conn, cmd, hdr);
1415}
1416
1417static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001418isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1419 uint32_t read_stag, uint64_t read_va,
1420 uint32_t write_stag, uint64_t write_va)
1421{
1422 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1423 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001424 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001425 struct iscsi_cmd *cmd;
1426 struct isert_cmd *isert_cmd;
1427 int ret = -EINVAL;
1428 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1429
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001430 if (sess->sess_ops->SessionType &&
1431 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1432 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1433 " ignoring\n", opcode);
1434 return 0;
1435 }
1436
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001437 switch (opcode) {
1438 case ISCSI_OP_SCSI_CMD:
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);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001444 isert_cmd->read_stag = read_stag;
1445 isert_cmd->read_va = read_va;
1446 isert_cmd->write_stag = write_stag;
1447 isert_cmd->write_va = write_va;
1448
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001449 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001450 rx_desc, (unsigned char *)hdr);
1451 break;
1452 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001453 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001454 if (!cmd)
1455 break;
1456
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001457 isert_cmd = iscsit_priv_cmd(cmd);
1458 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001459 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001460 break;
1461 case ISCSI_OP_SCSI_DATA_OUT:
1462 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1463 (unsigned char *)hdr);
1464 break;
1465 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001466 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001467 if (!cmd)
1468 break;
1469
1470 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1471 (unsigned char *)hdr);
1472 break;
1473 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001474 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001475 if (!cmd)
1476 break;
1477
1478 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1479 if (ret > 0)
1480 wait_for_completion_timeout(&conn->conn_logout_comp,
1481 SECONDS_FOR_LOGOUT_COMP *
1482 HZ);
1483 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001484 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001485 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001486 if (!cmd)
1487 break;
1488
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001489 isert_cmd = iscsit_priv_cmd(cmd);
1490 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001491 rx_desc, (struct iscsi_text *)hdr);
1492 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001493 default:
1494 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1495 dump_stack();
1496 break;
1497 }
1498
1499 return ret;
1500}
1501
1502static void
1503isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1504{
1505 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1506 uint64_t read_va = 0, write_va = 0;
1507 uint32_t read_stag = 0, write_stag = 0;
1508 int rc;
1509
1510 switch (iser_hdr->flags & 0xF0) {
1511 case ISCSI_CTRL:
1512 if (iser_hdr->flags & ISER_RSV) {
1513 read_stag = be32_to_cpu(iser_hdr->read_stag);
1514 read_va = be64_to_cpu(iser_hdr->read_va);
1515 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1516 read_stag, (unsigned long long)read_va);
1517 }
1518 if (iser_hdr->flags & ISER_WSV) {
1519 write_stag = be32_to_cpu(iser_hdr->write_stag);
1520 write_va = be64_to_cpu(iser_hdr->write_va);
1521 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1522 write_stag, (unsigned long long)write_va);
1523 }
1524
1525 pr_debug("ISER ISCSI_CTRL PDU\n");
1526 break;
1527 case ISER_HELLO:
1528 pr_err("iSER Hello message\n");
1529 break;
1530 default:
1531 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1532 break;
1533 }
1534
1535 rc = isert_rx_opcode(isert_conn, rx_desc,
1536 read_stag, read_va, write_stag, write_va);
1537}
1538
1539static void
1540isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1541 unsigned long xfer_len)
1542{
1543 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1544 struct iscsi_hdr *hdr;
1545 u64 rx_dma;
1546 int rx_buflen, outstanding;
1547
1548 if ((char *)desc == isert_conn->login_req_buf) {
1549 rx_dma = isert_conn->login_req_dma;
1550 rx_buflen = ISER_RX_LOGIN_SIZE;
1551 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1552 rx_dma, rx_buflen);
1553 } else {
1554 rx_dma = desc->dma_addr;
1555 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1556 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1557 rx_dma, rx_buflen);
1558 }
1559
1560 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1561
1562 hdr = &desc->iscsi_header;
1563 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1564 hdr->opcode, hdr->itt, hdr->flags,
1565 (int)(xfer_len - ISER_HEADERS_LEN));
1566
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001567 if ((char *)desc == isert_conn->login_req_buf) {
1568 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1569 if (isert_conn->conn) {
1570 struct iscsi_login *login = isert_conn->conn->conn_login;
1571
1572 if (login && !login->first_request)
1573 isert_rx_login_req(isert_conn);
1574 }
1575 mutex_lock(&isert_conn->conn_mutex);
1576 complete(&isert_conn->login_req_comp);
1577 mutex_unlock(&isert_conn->conn_mutex);
1578 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001579 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001580 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001581
1582 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1583 DMA_FROM_DEVICE);
1584
1585 isert_conn->post_recv_buf_count--;
1586 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1587 isert_conn->post_recv_buf_count);
1588
1589 if ((char *)desc == isert_conn->login_req_buf)
1590 return;
1591
1592 outstanding = isert_conn->post_recv_buf_count;
1593 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1594 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1595 ISERT_MIN_POSTED_RX);
1596 err = isert_post_recv(isert_conn, count);
1597 if (err) {
1598 pr_err("isert_post_recv() count: %d failed, %d\n",
1599 count, err);
1600 }
1601 }
1602}
1603
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001604static int
1605isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1606 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1607 enum iser_ib_op_code op, struct isert_data_buf *data)
1608{
1609 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1610
1611 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1612 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1613
1614 data->len = length - offset;
1615 data->offset = offset;
1616 data->sg_off = data->offset / PAGE_SIZE;
1617
1618 data->sg = &sg[data->sg_off];
1619 data->nents = min_t(unsigned int, nents - data->sg_off,
1620 ISCSI_ISER_SG_TABLESIZE);
1621 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1622 PAGE_SIZE);
1623
1624 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1625 data->dma_dir);
1626 if (unlikely(!data->dma_nents)) {
1627 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1628 return -EINVAL;
1629 }
1630
1631 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1632 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1633
1634 return 0;
1635}
1636
1637static void
1638isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1639{
1640 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1641
1642 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1643 memset(data, 0, sizeof(*data));
1644}
1645
1646
1647
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001648static void
1649isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1650{
1651 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001652
Vu Pham90ecc6e2013-08-28 23:23:33 +03001653 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001654
1655 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001656 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001657 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001658 }
1659
Vu Pham90ecc6e2013-08-28 23:23:33 +03001660 if (wr->send_wr) {
1661 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1662 kfree(wr->send_wr);
1663 wr->send_wr = NULL;
1664 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001665
Vu Pham90ecc6e2013-08-28 23:23:33 +03001666 if (wr->ib_sge) {
1667 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1668 kfree(wr->ib_sge);
1669 wr->ib_sge = NULL;
1670 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001671}
1672
1673static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001674isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001675{
1676 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001677 LIST_HEAD(unmap_list);
1678
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001679 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001680
1681 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001682 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001683 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001684 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1685 isert_unmap_data_buf(isert_conn, &wr->prot);
1686 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1687 }
Vu Pham59464ef2013-08-28 23:23:35 +03001688 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001689 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001690 spin_unlock_bh(&isert_conn->conn_lock);
1691 wr->fr_desc = NULL;
1692 }
1693
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001694 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001695 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001696 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001697 }
1698
1699 wr->ib_sge = NULL;
1700 wr->send_wr = NULL;
1701}
1702
1703static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001704isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001705{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001706 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001708 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001709 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001710
1711 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1712
1713 switch (cmd->iscsi_opcode) {
1714 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001715 spin_lock_bh(&conn->cmd_lock);
1716 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001717 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001718 spin_unlock_bh(&conn->cmd_lock);
1719
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001720 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001721 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001722 /*
1723 * Check for special case during comp_err where
1724 * WRITE_PENDING has been handed off from core,
1725 * but requires an extra target_put_sess_cmd()
1726 * before transport_generic_free_cmd() below.
1727 */
1728 if (comp_err &&
1729 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1730 struct se_cmd *se_cmd = &cmd->se_cmd;
1731
1732 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1733 }
1734 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001735
Vu Phamd40945d2013-08-28 23:23:34 +03001736 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001737 transport_generic_free_cmd(&cmd->se_cmd, 0);
1738 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001740 spin_lock_bh(&conn->cmd_lock);
1741 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001742 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001743 spin_unlock_bh(&conn->cmd_lock);
1744
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745 transport_generic_free_cmd(&cmd->se_cmd, 0);
1746 break;
1747 case ISCSI_OP_REJECT:
1748 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001749 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 spin_lock_bh(&conn->cmd_lock);
1751 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001752 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753 spin_unlock_bh(&conn->cmd_lock);
1754
1755 /*
1756 * Handle special case for REJECT when iscsi_add_reject*() has
1757 * overwritten the original iscsi_opcode assignment, and the
1758 * associated cmd->se_cmd needs to be released.
1759 */
1760 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001761 pr_debug("Calling transport_generic_free_cmd from"
1762 " isert_put_cmd for 0x%02x\n",
1763 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001764 transport_generic_free_cmd(&cmd->se_cmd, 0);
1765 break;
1766 }
1767 /*
1768 * Fall-through
1769 */
1770 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001771 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001772 break;
1773 }
1774}
1775
1776static void
1777isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1778{
1779 if (tx_desc->dma_addr != 0) {
1780 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1781 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1782 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1783 tx_desc->dma_addr = 0;
1784 }
1785}
1786
1787static void
1788isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001789 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001790{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001791 if (isert_cmd->pdu_buf_dma != 0) {
1792 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1793 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1794 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1795 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796 }
1797
1798 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001799 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001800}
1801
Sagi Grimberg96b79732014-03-17 12:52:18 +02001802static int
1803isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1804{
1805 struct ib_mr_status mr_status;
1806 int ret;
1807
1808 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1809 if (ret) {
1810 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1811 goto fail_mr_status;
1812 }
1813
1814 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1815 u64 sec_offset_err;
1816 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1817
1818 switch (mr_status.sig_err.err_type) {
1819 case IB_SIG_BAD_GUARD:
1820 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1821 break;
1822 case IB_SIG_BAD_REFTAG:
1823 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1824 break;
1825 case IB_SIG_BAD_APPTAG:
1826 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1827 break;
1828 }
1829 sec_offset_err = mr_status.sig_err.sig_err_offset;
1830 do_div(sec_offset_err, block_size);
1831 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1832
1833 pr_err("isert: PI error found type %d at sector 0x%llx "
1834 "expected 0x%x vs actual 0x%x\n",
1835 mr_status.sig_err.err_type,
1836 (unsigned long long)se_cmd->bad_sector,
1837 mr_status.sig_err.expected,
1838 mr_status.sig_err.actual);
1839 ret = 1;
1840 }
1841
1842fail_mr_status:
1843 return ret;
1844}
1845
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001846static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001847isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1848 struct isert_cmd *isert_cmd)
1849{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001850 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001851 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001852 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001853 struct isert_conn *isert_conn = isert_cmd->conn;
1854 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001855 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001856
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001857 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001858 ret = isert_check_pi_status(se_cmd,
1859 wr->fr_desc->pi_ctx->sig_mr);
1860 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001861 }
1862
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001863 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001864 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001865 if (ret)
1866 transport_send_check_condition_and_sense(se_cmd,
1867 se_cmd->pi_err, 0);
1868 else
1869 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001870}
1871
1872static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001873isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1874 struct isert_cmd *isert_cmd)
1875{
1876 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001877 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001878 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001879 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001880 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001881 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001882
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001883 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001884 ret = isert_check_pi_status(se_cmd,
1885 wr->fr_desc->pi_ctx->sig_mr);
1886 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001887 }
1888
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001889 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001890 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001891 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001892 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001893
Vu Pham90ecc6e2013-08-28 23:23:33 +03001894 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001895 spin_lock_bh(&cmd->istate_lock);
1896 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1897 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1898 spin_unlock_bh(&cmd->istate_lock);
1899
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001900 if (ret)
1901 transport_send_check_condition_and_sense(se_cmd,
1902 se_cmd->pi_err, 0);
1903 else
1904 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001905}
1906
1907static void
1908isert_do_control_comp(struct work_struct *work)
1909{
1910 struct isert_cmd *isert_cmd = container_of(work,
1911 struct isert_cmd, comp_work);
1912 struct isert_conn *isert_conn = isert_cmd->conn;
1913 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001914 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001915
1916 switch (cmd->i_state) {
1917 case ISTATE_SEND_TASKMGTRSP:
1918 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1919
1920 atomic_dec(&isert_conn->post_send_buf_count);
1921 iscsit_tmr_post_handler(cmd, cmd->conn);
1922
1923 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001924 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925 break;
1926 case ISTATE_SEND_REJECT:
1927 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1928 atomic_dec(&isert_conn->post_send_buf_count);
1929
1930 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001931 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001932 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001933 case ISTATE_SEND_LOGOUTRSP:
1934 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001935
1936 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001937 iscsit_logout_post_handler(cmd, cmd->conn);
1938 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001939 case ISTATE_SEND_TEXTRSP:
1940 atomic_dec(&isert_conn->post_send_buf_count);
1941 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001942 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001943 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001944 default:
1945 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1946 dump_stack();
1947 break;
1948 }
1949}
1950
1951static void
1952isert_response_completion(struct iser_tx_desc *tx_desc,
1953 struct isert_cmd *isert_cmd,
1954 struct isert_conn *isert_conn,
1955 struct ib_device *ib_dev)
1956{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001957 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001958 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001959
1960 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001961 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001962 cmd->i_state == ISTATE_SEND_REJECT ||
1963 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001964 isert_unmap_tx_desc(tx_desc, ib_dev);
1965
1966 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1967 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1968 return;
1969 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001970
1971 /**
1972 * If send_wr_num is 0 this means that we got
1973 * RDMA completion and we cleared it and we should
1974 * simply decrement the response post. else the
1975 * response is incorporated in send_wr_num, just
1976 * sub it.
1977 **/
1978 if (wr->send_wr_num)
1979 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1980 else
1981 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982
1983 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001984 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001985}
1986
1987static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001988__isert_send_completion(struct iser_tx_desc *tx_desc,
1989 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990{
1991 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1992 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1993 struct isert_rdma_wr *wr;
1994
1995 if (!isert_cmd) {
1996 atomic_dec(&isert_conn->post_send_buf_count);
1997 isert_unmap_tx_desc(tx_desc, ib_dev);
1998 return;
1999 }
2000 wr = &isert_cmd->rdma_wr;
2001
2002 switch (wr->iser_ib_op) {
2003 case ISER_IB_RECV:
2004 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
2005 dump_stack();
2006 break;
2007 case ISER_IB_SEND:
2008 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
2009 isert_response_completion(tx_desc, isert_cmd,
2010 isert_conn, ib_dev);
2011 break;
2012 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002013 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002014 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002015 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002016 break;
2017 case ISER_IB_RDMA_READ:
2018 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
2019
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002020 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002021 isert_completion_rdma_read(tx_desc, isert_cmd);
2022 break;
2023 default:
2024 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
2025 dump_stack();
2026 break;
2027 }
2028}
2029
2030static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002031isert_send_completion(struct iser_tx_desc *tx_desc,
2032 struct isert_conn *isert_conn)
2033{
2034 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2035 struct iser_tx_desc *t;
2036 /*
2037 * Drain coalesced completion llist starting from comp_llnode_batch
2038 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
2039 */
2040 while (llnode) {
2041 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2042 llnode = llist_next(llnode);
2043 __isert_send_completion(t, isert_conn);
2044 }
2045 __isert_send_completion(tx_desc, isert_conn);
2046}
2047
2048static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002049isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
2050{
2051 struct llist_node *llnode;
2052 struct isert_rdma_wr *wr;
2053 struct iser_tx_desc *t;
2054
2055 mutex_lock(&isert_conn->conn_mutex);
2056 llnode = llist_del_all(&isert_conn->conn_comp_llist);
2057 isert_conn->conn_comp_batch = 0;
2058 mutex_unlock(&isert_conn->conn_mutex);
2059
2060 while (llnode) {
2061 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2062 llnode = llist_next(llnode);
2063 wr = &t->isert_cmd->rdma_wr;
2064
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002065 /**
2066 * If send_wr_num is 0 this means that we got
2067 * RDMA completion and we cleared it and we should
2068 * simply decrement the response post. else the
2069 * response is incorporated in send_wr_num, just
2070 * sub it.
2071 **/
2072 if (wr->send_wr_num)
2073 atomic_sub(wr->send_wr_num,
2074 &isert_conn->post_send_buf_count);
2075 else
2076 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002077
2078 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002079 }
2080}
2081
2082static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002083isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002084{
2085 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002086 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002087 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2088 struct isert_rdma_wr *wr;
2089 struct iser_tx_desc *t;
2090
2091 while (llnode) {
2092 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2093 llnode = llist_next(llnode);
2094 wr = &t->isert_cmd->rdma_wr;
2095
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002096 /**
2097 * If send_wr_num is 0 this means that we got
2098 * RDMA completion and we cleared it and we should
2099 * simply decrement the response post. else the
2100 * response is incorporated in send_wr_num, just
2101 * sub it.
2102 **/
2103 if (wr->send_wr_num)
2104 atomic_sub(wr->send_wr_num,
2105 &isert_conn->post_send_buf_count);
2106 else
2107 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002108
2109 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002110 }
2111 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002112
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002113 if (!isert_cmd)
2114 isert_unmap_tx_desc(tx_desc, ib_dev);
2115 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002116 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002117}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002119static void
2120isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2121{
2122 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2123 struct iscsi_conn *conn = isert_conn->conn;
2124
2125 if (isert_conn->post_recv_buf_count)
2126 return;
2127
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002128 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2129
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002130 if (conn->sess) {
2131 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2132 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002133 }
2134
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002135 while (atomic_read(&isert_conn->post_send_buf_count))
2136 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002137
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002138 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002139 isert_conn_terminate(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002140 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002141
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002142 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2143
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002144 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002145}
2146
2147static void
2148isert_cq_tx_work(struct work_struct *work)
2149{
2150 struct isert_cq_desc *cq_desc = container_of(work,
2151 struct isert_cq_desc, cq_tx_work);
2152 struct isert_device *device = cq_desc->device;
2153 int cq_index = cq_desc->cq_index;
2154 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2155 struct isert_conn *isert_conn;
2156 struct iser_tx_desc *tx_desc;
2157 struct ib_wc wc;
2158
2159 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2160 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2161 isert_conn = wc.qp->qp_context;
2162
2163 if (wc.status == IB_WC_SUCCESS) {
2164 isert_send_completion(tx_desc, isert_conn);
2165 } else {
2166 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2167 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002168 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002169
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002170 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002171 if (tx_desc->llnode_active)
2172 continue;
2173
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002174 atomic_dec(&isert_conn->post_send_buf_count);
2175 isert_cq_tx_comp_err(tx_desc, isert_conn);
2176 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002177 }
2178 }
2179
2180 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2181}
2182
2183static void
2184isert_cq_tx_callback(struct ib_cq *cq, void *context)
2185{
2186 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2187
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002188 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2189}
2190
2191static void
2192isert_cq_rx_work(struct work_struct *work)
2193{
2194 struct isert_cq_desc *cq_desc = container_of(work,
2195 struct isert_cq_desc, cq_rx_work);
2196 struct isert_device *device = cq_desc->device;
2197 int cq_index = cq_desc->cq_index;
2198 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2199 struct isert_conn *isert_conn;
2200 struct iser_rx_desc *rx_desc;
2201 struct ib_wc wc;
2202 unsigned long xfer_len;
2203
2204 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2205 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2206 isert_conn = wc.qp->qp_context;
2207
2208 if (wc.status == IB_WC_SUCCESS) {
2209 xfer_len = (unsigned long)wc.byte_len;
2210 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2211 } else {
2212 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002213 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002214 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002215 pr_debug("RX wc.vendor_err: 0x%08x\n",
2216 wc.vendor_err);
2217 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002218 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002219 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002220 }
2221 }
2222
2223 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2224}
2225
2226static void
2227isert_cq_rx_callback(struct ib_cq *cq, void *context)
2228{
2229 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2230
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2232}
2233
2234static int
2235isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2236{
2237 struct ib_send_wr *wr_failed;
2238 int ret;
2239
2240 atomic_inc(&isert_conn->post_send_buf_count);
2241
2242 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2243 &wr_failed);
2244 if (ret) {
2245 pr_err("ib_post_send failed with %d\n", ret);
2246 atomic_dec(&isert_conn->post_send_buf_count);
2247 return ret;
2248 }
2249 return ret;
2250}
2251
2252static int
2253isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2254{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002255 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002256 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2257 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2258 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2259 &isert_cmd->tx_desc.iscsi_header;
2260
2261 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2262 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2263 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2264 /*
2265 * Attach SENSE DATA payload to iSCSI Response PDU
2266 */
2267 if (cmd->se_cmd.sense_buffer &&
2268 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2269 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2270 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2271 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002272 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002273
2274 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2275 cmd->sense_buffer);
2276 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2277
2278 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2279 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002280 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002281
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002282 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2283 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002284 DMA_TO_DEVICE);
2285
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002286 isert_cmd->pdu_buf_len = pdu_len;
2287 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2288 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2290 isert_cmd->tx_desc.num_sge = 2;
2291 }
2292
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002293 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002294
2295 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2296
2297 return isert_post_response(isert_conn, isert_cmd);
2298}
2299
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002300static void
2301isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2302{
2303 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2304 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2305 struct isert_device *device = isert_conn->conn_device;
2306
2307 spin_lock_bh(&conn->cmd_lock);
2308 if (!list_empty(&cmd->i_conn_node))
2309 list_del_init(&cmd->i_conn_node);
2310 spin_unlock_bh(&conn->cmd_lock);
2311
2312 if (cmd->data_direction == DMA_TO_DEVICE)
2313 iscsit_stop_dataout_timer(cmd);
2314
2315 device->unreg_rdma_mem(isert_cmd, isert_conn);
2316}
2317
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002318static enum target_prot_op
2319isert_get_sup_prot_ops(struct iscsi_conn *conn)
2320{
2321 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2322 struct isert_device *device = isert_conn->conn_device;
2323
2324 if (device->pi_capable)
2325 return TARGET_PROT_ALL;
2326
2327 return TARGET_PROT_NORMAL;
2328}
2329
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002330static int
2331isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2332 bool nopout_response)
2333{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002334 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002335 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2336 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2337
2338 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2339 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2340 &isert_cmd->tx_desc.iscsi_header,
2341 nopout_response);
2342 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002343 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002344
Masanari Iida8b513d02013-05-21 23:13:12 +09002345 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002346
2347 return isert_post_response(isert_conn, isert_cmd);
2348}
2349
2350static int
2351isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2352{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002353 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002354 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2355 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2356
2357 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2358 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2359 &isert_cmd->tx_desc.iscsi_header);
2360 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002361 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002362
2363 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2364
2365 return isert_post_response(isert_conn, isert_cmd);
2366}
2367
2368static int
2369isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2370{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002371 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002372 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2373 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2374
2375 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2376 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2377 &isert_cmd->tx_desc.iscsi_header);
2378 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002379 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002380
2381 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2382
2383 return isert_post_response(isert_conn, isert_cmd);
2384}
2385
2386static int
2387isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2388{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002389 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002390 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2391 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002392 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2393 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2394 struct iscsi_reject *hdr =
2395 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002396
2397 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002398 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002399 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002400
2401 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002402 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002403 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2404 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002405 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2406 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002407 tx_dsg->length = ISCSI_HDR_LEN;
2408 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2409 isert_cmd->tx_desc.num_sge = 2;
2410
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002411 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412
2413 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2414
2415 return isert_post_response(isert_conn, isert_cmd);
2416}
2417
2418static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002419isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2420{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002421 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002422 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2423 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2424 struct iscsi_text_rsp *hdr =
2425 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2426 u32 txt_rsp_len;
2427 int rc;
2428
2429 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002430 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002431 if (rc < 0)
2432 return rc;
2433
2434 txt_rsp_len = rc;
2435 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2436
2437 if (txt_rsp_len) {
2438 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2439 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2440 void *txt_rsp_buf = cmd->buf_ptr;
2441
2442 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2443 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2444
2445 isert_cmd->pdu_buf_len = txt_rsp_len;
2446 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2447 tx_dsg->length = txt_rsp_len;
2448 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2449 isert_cmd->tx_desc.num_sge = 2;
2450 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002451 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002452
2453 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2454
2455 return isert_post_response(isert_conn, isert_cmd);
2456}
2457
2458static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2460 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2461 u32 data_left, u32 offset)
2462{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002463 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002464 struct scatterlist *sg_start, *tmp_sg;
2465 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2466 u32 sg_off, page_off;
2467 int i = 0, sg_nents;
2468
2469 sg_off = offset / PAGE_SIZE;
2470 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2471 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2472 page_off = offset % PAGE_SIZE;
2473
2474 send_wr->sg_list = ib_sge;
2475 send_wr->num_sge = sg_nents;
2476 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2477 /*
2478 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2479 */
2480 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2481 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2482 (unsigned long long)tmp_sg->dma_address,
2483 tmp_sg->length, page_off);
2484
2485 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2486 ib_sge->length = min_t(u32, data_left,
2487 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2488 ib_sge->lkey = isert_conn->conn_mr->lkey;
2489
Vu Pham90ecc6e2013-08-28 23:23:33 +03002490 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2491 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002492 page_off = 0;
2493 data_left -= ib_sge->length;
2494 ib_sge++;
2495 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2496 }
2497
2498 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2499 send_wr->sg_list, send_wr->num_sge);
2500
2501 return sg_nents;
2502}
2503
2504static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002505isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2506 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002507{
2508 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002509 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002510 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002511 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002512 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002513 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002514 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2515 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002516
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002517 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002518
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002519 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2520 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2521 se_cmd->t_data_nents, se_cmd->data_length,
2522 offset, wr->iser_ib_op, &wr->data);
2523 if (ret)
2524 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002525
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002526 data_left = data->len;
2527 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002528
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002529 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002530 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002531 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002532 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002533 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002534 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002535 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002536
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002537 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002538 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2539 GFP_KERNEL);
2540 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002541 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002542 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002543 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002544 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002545
2546 wr->isert_cmd = isert_cmd;
2547 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002548
2549 for (i = 0; i < wr->send_wr_num; i++) {
2550 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2551 data_len = min(data_left, rdma_write_max);
2552
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002553 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002554 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2555 send_wr->opcode = IB_WR_RDMA_WRITE;
2556 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2557 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2558 if (i + 1 == wr->send_wr_num)
2559 send_wr->next = &isert_cmd->tx_desc.send_wr;
2560 else
2561 send_wr->next = &wr->send_wr[i + 1];
2562 } else {
2563 send_wr->opcode = IB_WR_RDMA_READ;
2564 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2565 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2566 if (i + 1 == wr->send_wr_num)
2567 send_wr->send_flags = IB_SEND_SIGNALED;
2568 else
2569 send_wr->next = &wr->send_wr[i + 1];
2570 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002571
2572 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2573 send_wr, data_len, offset);
2574 ib_sge += ib_sge_cnt;
2575
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002576 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002577 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002578 data_left -= data_len;
2579 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002580
2581 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002582unmap_cmd:
2583 isert_unmap_data_buf(isert_conn, data);
2584
Vu Pham90ecc6e2013-08-28 23:23:33 +03002585 return ret;
2586}
2587
2588static int
Vu Pham59464ef2013-08-28 23:23:35 +03002589isert_map_fr_pagelist(struct ib_device *ib_dev,
2590 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2591{
2592 u64 start_addr, end_addr, page, chunk_start = 0;
2593 struct scatterlist *tmp_sg;
2594 int i = 0, new_chunk, last_ent, n_pages;
2595
2596 n_pages = 0;
2597 new_chunk = 1;
2598 last_ent = sg_nents - 1;
2599 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2600 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2601 if (new_chunk)
2602 chunk_start = start_addr;
2603 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2604
2605 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2606 i, (unsigned long long)tmp_sg->dma_address,
2607 tmp_sg->length);
2608
2609 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2610 new_chunk = 0;
2611 continue;
2612 }
2613 new_chunk = 1;
2614
2615 page = chunk_start & PAGE_MASK;
2616 do {
2617 fr_pl[n_pages++] = page;
2618 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2619 n_pages - 1, page);
2620 page += PAGE_SIZE;
2621 } while (page < end_addr);
2622 }
2623
2624 return n_pages;
2625}
2626
2627static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002628isert_fast_reg_mr(struct isert_conn *isert_conn,
2629 struct fast_reg_descriptor *fr_desc,
2630 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002631 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002632 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002633{
Vu Pham59464ef2013-08-28 23:23:35 +03002634 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002635 struct ib_mr *mr;
2636 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002637 struct ib_send_wr fr_wr, inv_wr;
2638 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002639 int ret, pagelist_len;
2640 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002641 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002642
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002643 if (mem->dma_nents == 1) {
2644 sge->lkey = isert_conn->conn_mr->lkey;
2645 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2646 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002647 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2648 __func__, __LINE__, sge->addr, sge->length,
2649 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002650 return 0;
2651 }
2652
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002653 if (ind == ISERT_DATA_KEY_VALID) {
2654 /* Registering data buffer */
2655 mr = fr_desc->data_mr;
2656 frpl = fr_desc->data_frpl;
2657 } else {
2658 /* Registering protection buffer */
2659 mr = fr_desc->pi_ctx->prot_mr;
2660 frpl = fr_desc->pi_ctx->prot_frpl;
2661 }
2662
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002663 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002664
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002665 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002666 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002667
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002668 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002669 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002670
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002671 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002672 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002673 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002674 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002675 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002676 wr = &inv_wr;
2677 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002678 key = (u8)(mr->rkey & 0x000000FF);
2679 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002680 }
2681
2682 /* Prepare FASTREG WR */
2683 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002684 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002685 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002686 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2687 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002688 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2689 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002690 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002691 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002692 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2693
2694 if (!wr)
2695 wr = &fr_wr;
2696 else
2697 wr->next = &fr_wr;
2698
2699 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2700 if (ret) {
2701 pr_err("fast registration failed, ret:%d\n", ret);
2702 return ret;
2703 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002704 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002705
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002706 sge->lkey = mr->lkey;
2707 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002708 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002709
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002710 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2711 __func__, __LINE__, sge->addr, sge->length,
2712 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002713
2714 return ret;
2715}
2716
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002717static inline void
2718isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2719 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002720{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002721 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002722 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2723 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2724 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002725 /*
2726 * At the moment we hard code those, but if in the future
2727 * the target core would like to use it, we will take it
2728 * from se_cmd.
2729 */
2730 domain->sig.dif.apptag_check_mask = 0xffff;
2731 domain->sig.dif.app_escape = true;
2732 domain->sig.dif.ref_escape = true;
2733 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2734 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2735 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002736};
2737
2738static int
2739isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2740{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002741 switch (se_cmd->prot_op) {
2742 case TARGET_PROT_DIN_INSERT:
2743 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002744 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002745 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002746 break;
2747 case TARGET_PROT_DOUT_INSERT:
2748 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002749 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002750 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002751 break;
2752 case TARGET_PROT_DIN_PASS:
2753 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002754 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2755 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002756 break;
2757 default:
2758 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2759 return -EINVAL;
2760 }
2761
2762 return 0;
2763}
2764
2765static inline u8
2766isert_set_prot_checks(u8 prot_checks)
2767{
2768 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2769 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2770 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2771}
2772
2773static int
2774isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2775 struct fast_reg_descriptor *fr_desc,
2776 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2777 struct ib_sge *sig_sge)
2778{
2779 struct ib_send_wr sig_wr, inv_wr;
2780 struct ib_send_wr *bad_wr, *wr = NULL;
2781 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2782 struct ib_sig_attrs sig_attrs;
2783 int ret;
2784 u32 key;
2785
2786 memset(&sig_attrs, 0, sizeof(sig_attrs));
2787 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2788 if (ret)
2789 goto err;
2790
2791 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2792
2793 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2794 memset(&inv_wr, 0, sizeof(inv_wr));
2795 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002796 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002797 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2798 wr = &inv_wr;
2799 /* Bump the key */
2800 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2801 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2802 }
2803
2804 memset(&sig_wr, 0, sizeof(sig_wr));
2805 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002806 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002807 sig_wr.sg_list = data_sge;
2808 sig_wr.num_sge = 1;
2809 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2810 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2811 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2812 if (se_cmd->t_prot_sg)
2813 sig_wr.wr.sig_handover.prot = prot_sge;
2814
2815 if (!wr)
2816 wr = &sig_wr;
2817 else
2818 wr->next = &sig_wr;
2819
2820 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2821 if (ret) {
2822 pr_err("fast registration failed, ret:%d\n", ret);
2823 goto err;
2824 }
2825 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2826
2827 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2828 sig_sge->addr = 0;
2829 sig_sge->length = se_cmd->data_length;
2830 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2831 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2832 /*
2833 * We have protection guards on the wire
2834 * so we need to set a larget transfer
2835 */
2836 sig_sge->length += se_cmd->prot_length;
2837
2838 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2839 sig_sge->addr, sig_sge->length,
2840 sig_sge->lkey);
2841err:
2842 return ret;
2843}
2844
Vu Pham59464ef2013-08-28 23:23:35 +03002845static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002846isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2847 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002848{
2849 struct se_cmd *se_cmd = &cmd->se_cmd;
2850 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002851 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002852 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002853 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002854 struct fast_reg_descriptor *fr_desc = NULL;
2855 u32 offset;
2856 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002857 unsigned long flags;
2858
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002859 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2860
2861 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2862 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2863 se_cmd->t_data_nents, se_cmd->data_length,
2864 offset, wr->iser_ib_op, &wr->data);
2865 if (ret)
2866 return ret;
2867
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002868 if (wr->data.dma_nents != 1 ||
2869 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002870 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2871 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2872 struct fast_reg_descriptor, list);
2873 list_del(&fr_desc->list);
2874 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2875 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002876 }
2877
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002878 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2879 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002880 if (ret)
2881 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002882
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002883 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2884 struct ib_sge prot_sge, sig_sge;
2885
2886 if (se_cmd->t_prot_sg) {
2887 ret = isert_map_data_buf(isert_conn, isert_cmd,
2888 se_cmd->t_prot_sg,
2889 se_cmd->t_prot_nents,
2890 se_cmd->prot_length,
2891 0, wr->iser_ib_op, &wr->prot);
2892 if (ret)
2893 goto unmap_cmd;
2894
2895 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2896 ISERT_PROT_KEY_VALID, &prot_sge);
2897 if (ret)
2898 goto unmap_prot_cmd;
2899 }
2900
2901 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2902 &data_sge, &prot_sge, &sig_sge);
2903 if (ret)
2904 goto unmap_prot_cmd;
2905
2906 fr_desc->ind |= ISERT_PROTECTED;
2907 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2908 } else
2909 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2910
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002911 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002912 wr->send_wr_num = 1;
2913 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2914 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002915 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002916
2917 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002918 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002919 send_wr->num_sge = 1;
2920 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2921 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2922 send_wr->opcode = IB_WR_RDMA_WRITE;
2923 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2924 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002925 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2926 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002927 } else {
2928 send_wr->opcode = IB_WR_RDMA_READ;
2929 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2930 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2931 send_wr->send_flags = IB_SEND_SIGNALED;
2932 }
2933
Vu Pham59464ef2013-08-28 23:23:35 +03002934 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002935unmap_prot_cmd:
2936 if (se_cmd->t_prot_sg)
2937 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002938unmap_cmd:
2939 if (fr_desc) {
2940 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2941 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2942 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2943 }
2944 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002945
Vu Pham59464ef2013-08-28 23:23:35 +03002946 return ret;
2947}
2948
2949static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002950isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2951{
2952 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002953 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002954 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2955 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002956 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002957 struct ib_send_wr *wr_failed;
2958 int rc;
2959
2960 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2961 isert_cmd, se_cmd->data_length);
2962 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002963 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002964 if (rc) {
2965 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2966 return rc;
2967 }
2968
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002969 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2970 /*
2971 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2972 */
2973 isert_create_send_desc(isert_conn, isert_cmd,
2974 &isert_cmd->tx_desc);
2975 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2976 &isert_cmd->tx_desc.iscsi_header);
2977 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2978 isert_init_send_wr(isert_conn, isert_cmd,
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002979 &isert_cmd->tx_desc.send_wr, false);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002980 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002981 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002982 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002983
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002984 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002985
2986 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2987 if (rc) {
2988 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002989 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002990 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002991
2992 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2993 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2994 "READ\n", isert_cmd);
2995 else
2996 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2997 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002998
Vu Pham90ecc6e2013-08-28 23:23:33 +03002999 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003000}
3001
3002static int
3003isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
3004{
3005 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003006 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003007 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
3008 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03003009 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03003010 struct ib_send_wr *wr_failed;
3011 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003012
Vu Pham90ecc6e2013-08-28 23:23:33 +03003013 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
3014 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003015 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03003016 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03003017 if (rc) {
3018 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
3019 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003020 }
3021
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08003022 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003023
3024 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3025 if (rc) {
3026 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08003027 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003028 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03003029 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
3030 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003031
Vu Pham90ecc6e2013-08-28 23:23:33 +03003032 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003033}
3034
3035static int
3036isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3037{
3038 int ret;
3039
3040 switch (state) {
3041 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3042 ret = isert_put_nopin(cmd, conn, false);
3043 break;
3044 default:
3045 pr_err("Unknown immediate state: 0x%02x\n", state);
3046 ret = -EINVAL;
3047 break;
3048 }
3049
3050 return ret;
3051}
3052
3053static int
3054isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3055{
3056 int ret;
3057
3058 switch (state) {
3059 case ISTATE_SEND_LOGOUTRSP:
3060 ret = isert_put_logout_rsp(cmd, conn);
3061 if (!ret) {
3062 pr_debug("Returning iSER Logout -EAGAIN\n");
3063 ret = -EAGAIN;
3064 }
3065 break;
3066 case ISTATE_SEND_NOPIN:
3067 ret = isert_put_nopin(cmd, conn, true);
3068 break;
3069 case ISTATE_SEND_TASKMGTRSP:
3070 ret = isert_put_tm_rsp(cmd, conn);
3071 break;
3072 case ISTATE_SEND_REJECT:
3073 ret = isert_put_reject(cmd, conn);
3074 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003075 case ISTATE_SEND_TEXTRSP:
3076 ret = isert_put_text_rsp(cmd, conn);
3077 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003078 case ISTATE_SEND_STATUS:
3079 /*
3080 * Special case for sending non GOOD SCSI status from TX thread
3081 * context during pre se_cmd excecution failure.
3082 */
3083 ret = isert_put_response(conn, cmd);
3084 break;
3085 default:
3086 pr_err("Unknown response state: 0x%02x\n", state);
3087 ret = -EINVAL;
3088 break;
3089 }
3090
3091 return ret;
3092}
3093
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003094struct rdma_cm_id *
3095isert_setup_id(struct isert_np *isert_np)
3096{
3097 struct iscsi_np *np = isert_np->np;
3098 struct rdma_cm_id *id;
3099 struct sockaddr *sa;
3100 int ret;
3101
3102 sa = (struct sockaddr *)&np->np_sockaddr;
3103 pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3104
3105 id = rdma_create_id(isert_cma_handler, isert_np,
3106 RDMA_PS_TCP, IB_QPT_RC);
3107 if (IS_ERR(id)) {
3108 pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3109 ret = PTR_ERR(id);
3110 goto out;
3111 }
3112 pr_debug("id %p context %p\n", id, id->context);
3113
3114 ret = rdma_bind_addr(id, sa);
3115 if (ret) {
3116 pr_err("rdma_bind_addr() failed: %d\n", ret);
3117 goto out_id;
3118 }
3119
3120 ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3121 if (ret) {
3122 pr_err("rdma_listen() failed: %d\n", ret);
3123 goto out_id;
3124 }
3125
3126 return id;
3127out_id:
3128 rdma_destroy_id(id);
3129out:
3130 return ERR_PTR(ret);
3131}
3132
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003133static int
3134isert_setup_np(struct iscsi_np *np,
3135 struct __kernel_sockaddr_storage *ksockaddr)
3136{
3137 struct isert_np *isert_np;
3138 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003139 int ret;
3140
3141 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3142 if (!isert_np) {
3143 pr_err("Unable to allocate struct isert_np\n");
3144 return -ENOMEM;
3145 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003146 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003147 mutex_init(&isert_np->np_accept_mutex);
3148 INIT_LIST_HEAD(&isert_np->np_accept_list);
3149 init_completion(&isert_np->np_login_comp);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003150 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003151
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003152 /*
3153 * Setup the np->np_sockaddr from the passed sockaddr setup
3154 * in iscsi_target_configfs.c code..
3155 */
3156 memcpy(&np->np_sockaddr, ksockaddr,
3157 sizeof(struct __kernel_sockaddr_storage));
3158
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003159 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003160 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003161 ret = PTR_ERR(isert_lid);
3162 goto out;
3163 }
3164
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003165 isert_np->np_cm_id = isert_lid;
3166 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003167
3168 return 0;
3169
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003170out:
3171 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003172
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003173 return ret;
3174}
3175
3176static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003177isert_rdma_accept(struct isert_conn *isert_conn)
3178{
3179 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3180 struct rdma_conn_param cp;
3181 int ret;
3182
3183 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003184 cp.initiator_depth = isert_conn->initiator_depth;
3185 cp.retry_count = 7;
3186 cp.rnr_retry_count = 7;
3187
3188 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3189
3190 ret = rdma_accept(cm_id, &cp);
3191 if (ret) {
3192 pr_err("rdma_accept() failed with: %d\n", ret);
3193 return ret;
3194 }
3195
3196 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3197
3198 return 0;
3199}
3200
3201static int
3202isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3203{
3204 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3205 int ret;
3206
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003207 pr_info("before login_req comp conn: %p\n", isert_conn);
3208 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3209 if (ret) {
3210 pr_err("isert_conn %p interrupted before got login req\n",
3211 isert_conn);
3212 return ret;
3213 }
3214 reinit_completion(&isert_conn->login_req_comp);
3215
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003216 /*
3217 * For login requests after the first PDU, isert_rx_login_req() will
3218 * kick schedule_delayed_work(&conn->login_work) as the packet is
3219 * received, which turns this callback from iscsi_target_do_login_rx()
3220 * into a NOP.
3221 */
3222 if (!login->first_request)
3223 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003224
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003225 isert_rx_login_req(isert_conn);
3226
3227 pr_info("before conn_login_comp conn: %p\n", conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003228 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3229 if (ret)
3230 return ret;
3231
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003232 pr_info("processing login->req: %p\n", login->req);
3233
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003234 return 0;
3235}
3236
3237static void
3238isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3239 struct isert_conn *isert_conn)
3240{
3241 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3242 struct rdma_route *cm_route = &cm_id->route;
3243 struct sockaddr_in *sock_in;
3244 struct sockaddr_in6 *sock_in6;
3245
3246 conn->login_family = np->np_sockaddr.ss_family;
3247
3248 if (np->np_sockaddr.ss_family == AF_INET6) {
3249 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3250 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3251 &sock_in6->sin6_addr.in6_u);
3252 conn->login_port = ntohs(sock_in6->sin6_port);
3253
3254 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3255 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3256 &sock_in6->sin6_addr.in6_u);
3257 conn->local_port = ntohs(sock_in6->sin6_port);
3258 } else {
3259 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3260 sprintf(conn->login_ip, "%pI4",
3261 &sock_in->sin_addr.s_addr);
3262 conn->login_port = ntohs(sock_in->sin_port);
3263
3264 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3265 sprintf(conn->local_ip, "%pI4",
3266 &sock_in->sin_addr.s_addr);
3267 conn->local_port = ntohs(sock_in->sin_port);
3268 }
3269}
3270
3271static int
3272isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3273{
3274 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3275 struct isert_conn *isert_conn;
3276 int max_accept = 0, ret;
3277
3278accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003279 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003280 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003281 return -ENODEV;
3282
3283 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003284 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003285 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003286 pr_debug("np_thread_state %d for isert_accept_np\n",
3287 np->np_thread_state);
3288 /**
3289 * No point in stalling here when np_thread
3290 * is in state RESET/SHUTDOWN/EXIT - bail
3291 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003292 return -ENODEV;
3293 }
3294 spin_unlock_bh(&np->np_thread_lock);
3295
3296 mutex_lock(&isert_np->np_accept_mutex);
3297 if (list_empty(&isert_np->np_accept_list)) {
3298 mutex_unlock(&isert_np->np_accept_mutex);
3299 max_accept++;
3300 goto accept_wait;
3301 }
3302 isert_conn = list_first_entry(&isert_np->np_accept_list,
3303 struct isert_conn, conn_accept_node);
3304 list_del_init(&isert_conn->conn_accept_node);
3305 mutex_unlock(&isert_np->np_accept_mutex);
3306
3307 conn->context = isert_conn;
3308 isert_conn->conn = conn;
3309 max_accept = 0;
3310
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003311 isert_set_conn_info(np, conn, isert_conn);
3312
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003313 pr_debug("Processing isert_conn: %p\n", isert_conn);
3314
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003315 return 0;
3316}
3317
3318static void
3319isert_free_np(struct iscsi_np *np)
3320{
3321 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3322
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003323 if (isert_np->np_cm_id)
3324 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003325
3326 np->np_context = NULL;
3327 kfree(isert_np);
3328}
3329
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003330static void isert_release_work(struct work_struct *work)
3331{
3332 struct isert_conn *isert_conn = container_of(work,
3333 struct isert_conn,
3334 release_work);
3335
3336 pr_info("Starting release conn %p\n", isert_conn);
3337
3338 wait_for_completion(&isert_conn->conn_wait);
3339
3340 mutex_lock(&isert_conn->conn_mutex);
3341 isert_conn->state = ISER_CONN_DOWN;
3342 mutex_unlock(&isert_conn->conn_mutex);
3343
3344 pr_info("Destroying conn %p\n", isert_conn);
3345 isert_put_conn(isert_conn);
3346}
3347
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003348static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003349{
3350 struct isert_conn *isert_conn = conn->context;
3351
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003352 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003353
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003354 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003355 /*
3356 * Only wait for conn_wait_comp_err if the isert_conn made it
3357 * into full feature phase..
3358 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003359 if (isert_conn->state == ISER_CONN_INIT) {
3360 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003361 return;
3362 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003363 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003364 mutex_unlock(&isert_conn->conn_mutex);
3365
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003366 wait_for_completion(&isert_conn->conn_wait_comp_err);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003367
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003368 INIT_WORK(&isert_conn->release_work, isert_release_work);
3369 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003370}
3371
3372static void isert_free_conn(struct iscsi_conn *conn)
3373{
3374 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003375
3376 isert_put_conn(isert_conn);
3377}
3378
3379static struct iscsit_transport iser_target_transport = {
3380 .name = "IB/iSER",
3381 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003382 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003383 .owner = THIS_MODULE,
3384 .iscsit_setup_np = isert_setup_np,
3385 .iscsit_accept_np = isert_accept_np,
3386 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003387 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003388 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003389 .iscsit_get_login_rx = isert_get_login_rx,
3390 .iscsit_put_login_tx = isert_put_login_tx,
3391 .iscsit_immediate_queue = isert_immediate_queue,
3392 .iscsit_response_queue = isert_response_queue,
3393 .iscsit_get_dataout = isert_get_dataout,
3394 .iscsit_queue_data_in = isert_put_datain,
3395 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003396 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003397 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003398};
3399
3400static int __init isert_init(void)
3401{
3402 int ret;
3403
3404 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3405 if (!isert_rx_wq) {
3406 pr_err("Unable to allocate isert_rx_wq\n");
3407 return -ENOMEM;
3408 }
3409
3410 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3411 if (!isert_comp_wq) {
3412 pr_err("Unable to allocate isert_comp_wq\n");
3413 ret = -ENOMEM;
3414 goto destroy_rx_wq;
3415 }
3416
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003417 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3418 WQ_UNBOUND_MAX_ACTIVE);
3419 if (!isert_release_wq) {
3420 pr_err("Unable to allocate isert_release_wq\n");
3421 ret = -ENOMEM;
3422 goto destroy_comp_wq;
3423 }
3424
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003425 iscsit_register_transport(&iser_target_transport);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003426 pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3427
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003428 return 0;
3429
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003430destroy_comp_wq:
3431 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003432destroy_rx_wq:
3433 destroy_workqueue(isert_rx_wq);
3434 return ret;
3435}
3436
3437static void __exit isert_exit(void)
3438{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003439 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003440 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003441 destroy_workqueue(isert_comp_wq);
3442 destroy_workqueue(isert_rx_wq);
3443 iscsit_unregister_transport(&iser_target_transport);
3444 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3445}
3446
3447MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3448MODULE_VERSION("0.1");
3449MODULE_AUTHOR("nab@Linux-iSCSI.org");
3450MODULE_LICENSE("GPL");
3451
3452module_init(isert_init);
3453module_exit(isert_exit);