blob: 108548437c9f34ee2f93f5420a037d1267053f19 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020038#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080040
41static DEFINE_MUTEX(device_list_mutex);
42static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080043static 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
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020064static inline bool
65isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
66{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020067 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020068 cmd->prot_op != TARGET_PROT_NORMAL);
69}
70
71
Vu Phamd40945d2013-08-28 23:23:34 +030072static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080073isert_qp_event_callback(struct ib_event *e, void *context)
74{
75 struct isert_conn *isert_conn = (struct isert_conn *)context;
76
77 pr_err("isert_qp_event_callback event: %d\n", e->event);
78 switch (e->event) {
79 case IB_EVENT_COMM_EST:
80 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
81 break;
82 case IB_EVENT_QP_LAST_WQE_REACHED:
83 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
84 break;
85 default:
86 break;
87 }
88}
89
90static int
91isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
92{
93 int ret;
94
95 ret = ib_query_device(ib_dev, devattr);
96 if (ret) {
97 pr_err("ib_query_device() failed: %d\n", ret);
98 return ret;
99 }
100 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
101 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
102
103 return 0;
104}
105
106static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200107isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800108{
109 struct isert_device *device = isert_conn->conn_device;
110 struct ib_qp_init_attr attr;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200111 struct isert_comp *comp;
112 int ret, i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800113
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800114 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200115 for (i = 0; i < device->comps_used; i++)
116 if (device->comps[i].active_qps <
117 device->comps[min].active_qps)
118 min = i;
119 comp = &device->comps[min];
120 comp->active_qps++;
121 pr_info("conn %p, using comp %p min_index: %d\n",
122 isert_conn, comp, min);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800123 mutex_unlock(&device_list_mutex);
124
125 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
126 attr.event_handler = isert_qp_event_callback;
127 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200128 attr.send_cq = comp->cq;
129 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800130 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200131 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800132 /*
133 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700134 * work-around for RDMA_READs with ConnectX-2.
135 *
136 * Also, still make sure to have at least two SGEs for
137 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800138 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700139 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800140 isert_conn->max_sge = attr.cap.max_send_sge;
141
142 attr.cap.max_recv_sge = 1;
143 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
144 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200145 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200146 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800147
148 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
149 cma_id->device);
150 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
151 isert_conn->conn_pd->device);
152
153 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
154 if (ret) {
155 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200156 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800157 }
158 isert_conn->conn_qp = cma_id->qp;
159 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
160
161 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200162err:
163 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200164 comp->active_qps--;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200165 mutex_unlock(&device_list_mutex);
166
167 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800168}
169
170static void
171isert_cq_event_callback(struct ib_event *e, void *context)
172{
173 pr_debug("isert_cq_event_callback event: %d\n", e->event);
174}
175
176static int
177isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
178{
179 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
180 struct iser_rx_desc *rx_desc;
181 struct ib_sge *rx_sg;
182 u64 dma_addr;
183 int i, j;
184
185 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
186 sizeof(struct iser_rx_desc), GFP_KERNEL);
187 if (!isert_conn->conn_rx_descs)
188 goto fail;
189
190 rx_desc = isert_conn->conn_rx_descs;
191
192 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
193 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
194 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
195 if (ib_dma_mapping_error(ib_dev, dma_addr))
196 goto dma_map_fail;
197
198 rx_desc->dma_addr = dma_addr;
199
200 rx_sg = &rx_desc->rx_sg;
201 rx_sg->addr = rx_desc->dma_addr;
202 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
203 rx_sg->lkey = isert_conn->conn_mr->lkey;
204 }
205
206 isert_conn->conn_rx_desc_head = 0;
207 return 0;
208
209dma_map_fail:
210 rx_desc = isert_conn->conn_rx_descs;
211 for (j = 0; j < i; j++, rx_desc++) {
212 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
213 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
214 }
215 kfree(isert_conn->conn_rx_descs);
216 isert_conn->conn_rx_descs = NULL;
217fail:
218 return -ENOMEM;
219}
220
221static void
222isert_free_rx_descriptors(struct isert_conn *isert_conn)
223{
224 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
225 struct iser_rx_desc *rx_desc;
226 int i;
227
228 if (!isert_conn->conn_rx_descs)
229 return;
230
231 rx_desc = isert_conn->conn_rx_descs;
232 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
233 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
234 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
235 }
236
237 kfree(isert_conn->conn_rx_descs);
238 isert_conn->conn_rx_descs = NULL;
239}
240
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200241static void isert_cq_work(struct work_struct *);
242static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800243
244static int
245isert_create_device_ib_res(struct isert_device *device)
246{
247 struct ib_device *ib_dev = device->ib_device;
Vu Pham59464ef2013-08-28 23:23:35 +0300248 struct ib_device_attr *dev_attr;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200249 int ret = 0, i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200250 int max_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251
Vu Pham59464ef2013-08-28 23:23:35 +0300252 dev_attr = &device->dev_attr;
253 ret = isert_query_device(ib_dev, dev_attr);
254 if (ret)
255 return ret;
256
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200257 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000258
Vu Phamd40945d2013-08-28 23:23:34 +0300259 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200260 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
261 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200262 device->use_fastreg = 1;
263 device->reg_rdma_mem = isert_reg_rdma;
264 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300265 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200266 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300267 device->reg_rdma_mem = isert_map_rdma;
268 device->unreg_rdma_mem = isert_unmap_cmd;
269 }
Vu Phamd40945d2013-08-28 23:23:34 +0300270
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200271 /* Check signature cap */
272 device->pi_capable = dev_attr->device_cap_flags &
273 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
274
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200275 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
276 device->ib_device->num_comp_vectors));
277 pr_info("Using %d CQs, %s supports %d vectors support "
278 "Fast registration %d pi_capable %d\n",
279 device->comps_used, device->ib_device->name,
280 device->ib_device->num_comp_vectors, device->use_fastreg,
281 device->pi_capable);
282
283 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
284 GFP_KERNEL);
285 if (!device->comps) {
286 pr_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800287 return -ENOMEM;
288 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800289
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200290 for (i = 0; i < device->comps_used; i++) {
291 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800292
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200293 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200294 INIT_WORK(&comp->work, isert_cq_work);
295 comp->cq = ib_create_cq(device->ib_device,
296 isert_cq_callback,
297 isert_cq_event_callback,
298 (void *)comp,
299 max_cqe, i);
300 if (IS_ERR(comp->cq)) {
301 ret = PTR_ERR(comp->cq);
302 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800303 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800304 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800305
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200306 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800307 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800308 goto out_cq;
309 }
310
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800311 return 0;
312
313out_cq:
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200314 for (i = 0; i < device->comps_used; i++) {
315 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800316
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200317 if (comp->cq) {
318 cancel_work_sync(&comp->work);
319 ib_destroy_cq(comp->cq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800320 }
321 }
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200322 kfree(device->comps);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800323
324 return ret;
325}
326
327static void
328isert_free_device_ib_res(struct isert_device *device)
329{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800330 int i;
331
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200332 pr_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800333
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200334 for (i = 0; i < device->comps_used; i++) {
335 struct isert_comp *comp = &device->comps[i];
336
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200337 cancel_work_sync(&comp->work);
338 ib_destroy_cq(comp->cq);
339 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800340 }
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200341 kfree(device->comps);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800342}
343
344static void
345isert_device_try_release(struct isert_device *device)
346{
347 mutex_lock(&device_list_mutex);
348 device->refcount--;
349 if (!device->refcount) {
350 isert_free_device_ib_res(device);
351 list_del(&device->dev_node);
352 kfree(device);
353 }
354 mutex_unlock(&device_list_mutex);
355}
356
357static struct isert_device *
358isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
359{
360 struct isert_device *device;
361 int ret;
362
363 mutex_lock(&device_list_mutex);
364 list_for_each_entry(device, &device_list, dev_node) {
365 if (device->ib_device->node_guid == cma_id->device->node_guid) {
366 device->refcount++;
367 mutex_unlock(&device_list_mutex);
368 return device;
369 }
370 }
371
372 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
373 if (!device) {
374 mutex_unlock(&device_list_mutex);
375 return ERR_PTR(-ENOMEM);
376 }
377
378 INIT_LIST_HEAD(&device->dev_node);
379
380 device->ib_device = cma_id->device;
381 ret = isert_create_device_ib_res(device);
382 if (ret) {
383 kfree(device);
384 mutex_unlock(&device_list_mutex);
385 return ERR_PTR(ret);
386 }
387
388 device->refcount++;
389 list_add_tail(&device->dev_node, &device_list);
390 mutex_unlock(&device_list_mutex);
391
392 return device;
393}
394
Vu Pham59464ef2013-08-28 23:23:35 +0300395static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200396isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300397{
398 struct fast_reg_descriptor *fr_desc, *tmp;
399 int i = 0;
400
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200401 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300402 return;
403
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200404 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300405
406 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200407 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300408 list_del(&fr_desc->list);
409 ib_free_fast_reg_page_list(fr_desc->data_frpl);
410 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200411 if (fr_desc->pi_ctx) {
412 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
413 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
414 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
415 kfree(fr_desc->pi_ctx);
416 }
Vu Pham59464ef2013-08-28 23:23:35 +0300417 kfree(fr_desc);
418 ++i;
419 }
420
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200421 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300422 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200423 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300424}
425
426static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200427isert_create_pi_ctx(struct fast_reg_descriptor *desc,
428 struct ib_device *device,
429 struct ib_pd *pd)
430{
431 struct ib_mr_init_attr mr_init_attr;
432 struct pi_context *pi_ctx;
433 int ret;
434
435 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
436 if (!pi_ctx) {
437 pr_err("Failed to allocate pi context\n");
438 return -ENOMEM;
439 }
440
441 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
442 ISCSI_ISER_SG_TABLESIZE);
443 if (IS_ERR(pi_ctx->prot_frpl)) {
444 pr_err("Failed to allocate prot frpl err=%ld\n",
445 PTR_ERR(pi_ctx->prot_frpl));
446 ret = PTR_ERR(pi_ctx->prot_frpl);
447 goto err_pi_ctx;
448 }
449
450 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
451 if (IS_ERR(pi_ctx->prot_mr)) {
452 pr_err("Failed to allocate prot frmr err=%ld\n",
453 PTR_ERR(pi_ctx->prot_mr));
454 ret = PTR_ERR(pi_ctx->prot_mr);
455 goto err_prot_frpl;
456 }
457 desc->ind |= ISERT_PROT_KEY_VALID;
458
459 memset(&mr_init_attr, 0, sizeof(mr_init_attr));
460 mr_init_attr.max_reg_descriptors = 2;
461 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
462 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
463 if (IS_ERR(pi_ctx->sig_mr)) {
464 pr_err("Failed to allocate signature enabled mr err=%ld\n",
465 PTR_ERR(pi_ctx->sig_mr));
466 ret = PTR_ERR(pi_ctx->sig_mr);
467 goto err_prot_mr;
468 }
469
470 desc->pi_ctx = pi_ctx;
471 desc->ind |= ISERT_SIG_KEY_VALID;
472 desc->ind &= ~ISERT_PROTECTED;
473
474 return 0;
475
476err_prot_mr:
477 ib_dereg_mr(desc->pi_ctx->prot_mr);
478err_prot_frpl:
479 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
480err_pi_ctx:
481 kfree(desc->pi_ctx);
482
483 return ret;
484}
485
486static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200487isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200488 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200489{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200490 int ret;
491
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200492 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
493 ISCSI_ISER_SG_TABLESIZE);
494 if (IS_ERR(fr_desc->data_frpl)) {
495 pr_err("Failed to allocate data frpl err=%ld\n",
496 PTR_ERR(fr_desc->data_frpl));
497 return PTR_ERR(fr_desc->data_frpl);
498 }
499
500 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
501 if (IS_ERR(fr_desc->data_mr)) {
502 pr_err("Failed to allocate data frmr err=%ld\n",
503 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200504 ret = PTR_ERR(fr_desc->data_mr);
505 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200506 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200507 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200508
Sagi Grimberg570db172014-12-02 16:57:31 +0200509 pr_debug("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200510
511 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200512
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200513err_data_frpl:
514 ib_free_fast_reg_page_list(fr_desc->data_frpl);
515
516 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200517}
518
519static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200520isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300521{
522 struct fast_reg_descriptor *fr_desc;
523 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700524 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
525 struct se_node_acl *se_nacl = se_sess->se_node_acl;
526 int i, ret, tag_num;
527 /*
528 * Setup the number of FRMRs based upon the number of tags
529 * available to session in iscsi_target_locate_portal().
530 */
531 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
532 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300533
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200534 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700535 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300536 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
537 if (!fr_desc) {
538 pr_err("Failed to allocate fast_reg descriptor\n");
539 ret = -ENOMEM;
540 goto err;
541 }
542
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200543 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg570db172014-12-02 16:57:31 +0200544 isert_conn->conn_pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200545 if (ret) {
546 pr_err("Failed to create fastreg descriptor err=%d\n",
547 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800548 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300549 goto err;
550 }
551
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200552 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
553 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300554 }
555
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200556 pr_debug("Creating conn %p fastreg pool size=%d",
557 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300558
559 return 0;
560
561err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200562 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300563 return ret;
564}
565
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800566static int
567isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
568{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200569 struct isert_np *isert_np = cma_id->context;
570 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800571 struct isert_conn *isert_conn;
572 struct isert_device *device;
573 struct ib_device *ib_dev = cma_id->device;
574 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300575
576 spin_lock_bh(&np->np_thread_lock);
577 if (!np->enabled) {
578 spin_unlock_bh(&np->np_thread_lock);
579 pr_debug("iscsi_np is not enabled, reject connect request\n");
580 return rdma_reject(cma_id, NULL, 0);
581 }
582 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800583
584 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
585 cma_id, cma_id->context);
586
587 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
588 if (!isert_conn) {
589 pr_err("Unable to allocate isert_conn\n");
590 return -ENOMEM;
591 }
592 isert_conn->state = ISER_CONN_INIT;
593 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
594 init_completion(&isert_conn->conn_login_comp);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200595 init_completion(&isert_conn->login_req_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800596 init_completion(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800597 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700598 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300599 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700600 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800601
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800602 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800603
604 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
605 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
606 if (!isert_conn->login_buf) {
607 pr_err("Unable to allocate isert_conn->login_buf\n");
608 ret = -ENOMEM;
609 goto out;
610 }
611
612 isert_conn->login_req_buf = isert_conn->login_buf;
613 isert_conn->login_rsp_buf = isert_conn->login_buf +
614 ISCSI_DEF_MAX_RECV_SEG_LEN;
615 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
616 isert_conn->login_buf, isert_conn->login_req_buf,
617 isert_conn->login_rsp_buf);
618
619 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
620 (void *)isert_conn->login_req_buf,
621 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
622
623 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
624 if (ret) {
625 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
626 ret);
627 isert_conn->login_req_dma = 0;
628 goto out_login_buf;
629 }
630
631 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
632 (void *)isert_conn->login_rsp_buf,
633 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
634
635 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
636 if (ret) {
637 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
638 ret);
639 isert_conn->login_rsp_dma = 0;
640 goto out_req_dma_map;
641 }
642
643 device = isert_device_find_by_ib_dev(cma_id);
644 if (IS_ERR(device)) {
645 ret = PTR_ERR(device);
646 goto out_rsp_dma_map;
647 }
648
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300649 /* Set max inflight RDMA READ requests */
650 isert_conn->initiator_depth = min_t(u8,
651 event->param.conn.initiator_depth,
652 device->dev_attr.max_qp_init_rd_atom);
653 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
654
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800655 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200656 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
657 if (IS_ERR(isert_conn->conn_pd)) {
658 ret = PTR_ERR(isert_conn->conn_pd);
659 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
660 isert_conn, ret);
661 goto out_pd;
662 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800663
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200664 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
665 IB_ACCESS_LOCAL_WRITE);
666 if (IS_ERR(isert_conn->conn_mr)) {
667 ret = PTR_ERR(isert_conn->conn_mr);
668 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
669 isert_conn, ret);
670 goto out_mr;
671 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800672
Sagi Grimberg570db172014-12-02 16:57:31 +0200673 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800674 if (ret)
675 goto out_conn_dev;
676
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200677 ret = isert_rdma_post_recvl(isert_conn);
678 if (ret)
679 goto out_conn_dev;
680
681 ret = isert_rdma_accept(isert_conn);
682 if (ret)
683 goto out_conn_dev;
684
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800685 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300686 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800687 mutex_unlock(&isert_np->np_accept_mutex);
688
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300689 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
690 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800691 return 0;
692
693out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200694 ib_dereg_mr(isert_conn->conn_mr);
695out_mr:
696 ib_dealloc_pd(isert_conn->conn_pd);
697out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698 isert_device_try_release(device);
699out_rsp_dma_map:
700 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
701 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
702out_req_dma_map:
703 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
704 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
705out_login_buf:
706 kfree(isert_conn->login_buf);
707out:
708 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200709 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800710 return ret;
711}
712
713static void
714isert_connect_release(struct isert_conn *isert_conn)
715{
716 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
717 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800718
719 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
720
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200721 if (device && device->use_fastreg)
722 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300723
Sagi Grimberg19e20902014-12-02 16:57:26 +0200724 isert_free_rx_descriptors(isert_conn);
725 rdma_destroy_id(isert_conn->conn_cm_id);
726
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800727 if (isert_conn->conn_qp) {
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200728 struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
729
730 pr_debug("dec completion context %p active_qps\n", comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200731 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200732 comp->active_qps--;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200733 mutex_unlock(&device_list_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800734
Sagi Grimberg19e20902014-12-02 16:57:26 +0200735 ib_destroy_qp(isert_conn->conn_qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800736 }
737
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200738 ib_dereg_mr(isert_conn->conn_mr);
739 ib_dealloc_pd(isert_conn->conn_pd);
740
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800741 if (isert_conn->login_buf) {
742 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
743 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
744 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
745 ISCSI_DEF_MAX_RECV_SEG_LEN,
746 DMA_FROM_DEVICE);
747 kfree(isert_conn->login_buf);
748 }
749 kfree(isert_conn);
750
751 if (device)
752 isert_device_try_release(device);
753
754 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
755}
756
757static void
758isert_connected_handler(struct rdma_cm_id *cma_id)
759{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200760 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300761
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200762 pr_info("conn %p\n", isert_conn);
763
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200764 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
765 pr_warn("conn %p connect_release is running\n", isert_conn);
766 return;
767 }
768
769 mutex_lock(&isert_conn->conn_mutex);
770 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
771 isert_conn->state = ISER_CONN_UP;
772 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800773}
774
775static void
776isert_release_conn_kref(struct kref *kref)
777{
778 struct isert_conn *isert_conn = container_of(kref,
779 struct isert_conn, conn_kref);
780
781 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
782 current->comm, current->pid);
783
784 isert_connect_release(isert_conn);
785}
786
787static void
788isert_put_conn(struct isert_conn *isert_conn)
789{
790 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
791}
792
Sagi Grimberg954f2372014-12-02 16:57:17 +0200793/**
794 * isert_conn_terminate() - Initiate connection termination
795 * @isert_conn: isert connection struct
796 *
797 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200798 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200799 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200800 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200801 *
802 * This routine must be called with conn_mutex held. Thus it is
803 * safe to call multiple times.
804 */
805static void
806isert_conn_terminate(struct isert_conn *isert_conn)
807{
808 int err;
809
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200810 switch (isert_conn->state) {
811 case ISER_CONN_TERMINATING:
812 break;
813 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200814 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200815 pr_info("Terminating conn %p state %d\n",
816 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200817 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200818 err = rdma_disconnect(isert_conn->conn_cm_id);
819 if (err)
820 pr_warn("Failed rdma_disconnect isert_conn %p\n",
821 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200822 break;
823 default:
824 pr_warn("conn %p teminating in state %d\n",
825 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200826 }
827}
828
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700829static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200830isert_np_cma_handler(struct isert_np *isert_np,
831 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800832{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200833 pr_debug("isert np %p, handling event %d\n", isert_np, event);
834
835 switch (event) {
836 case RDMA_CM_EVENT_DEVICE_REMOVAL:
837 isert_np->np_cm_id = NULL;
838 break;
839 case RDMA_CM_EVENT_ADDR_CHANGE:
840 isert_np->np_cm_id = isert_setup_id(isert_np);
841 if (IS_ERR(isert_np->np_cm_id)) {
842 pr_err("isert np %p setup id failed: %ld\n",
843 isert_np, PTR_ERR(isert_np->np_cm_id));
844 isert_np->np_cm_id = NULL;
845 }
846 break;
847 default:
848 pr_err("isert np %p Unexpected event %d\n",
849 isert_np, event);
850 }
851
852 return -1;
853}
854
855static int
856isert_disconnected_handler(struct rdma_cm_id *cma_id,
857 enum rdma_cm_event_type event)
858{
859 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700860 struct isert_conn *isert_conn;
861
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200862 if (isert_np->np_cm_id == cma_id)
863 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700864
Sagi Grimberg19e20902014-12-02 16:57:26 +0200865 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800866
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200867 mutex_lock(&isert_conn->conn_mutex);
868 isert_conn_terminate(isert_conn);
869 mutex_unlock(&isert_conn->conn_mutex);
870
871 pr_info("conn %p completing conn_wait\n", isert_conn);
872 complete(&isert_conn->conn_wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700873
874 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800875}
876
Sagi Grimberg954f2372014-12-02 16:57:17 +0200877static void
878isert_connect_error(struct rdma_cm_id *cma_id)
879{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200880 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200881
882 isert_put_conn(isert_conn);
883}
884
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800885static int
886isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
887{
888 int ret = 0;
889
890 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
891 event->event, event->status, cma_id->context, cma_id);
892
893 switch (event->event) {
894 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700896 if (ret)
897 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
898 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800899 break;
900 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901 isert_connected_handler(cma_id);
902 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300903 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
904 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
905 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300906 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200907 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800908 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200909 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
910 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800911 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg954f2372014-12-02 16:57:17 +0200912 isert_connect_error(cma_id);
913 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800914 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300915 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800916 break;
917 }
918
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800919 return ret;
920}
921
922static int
923isert_post_recv(struct isert_conn *isert_conn, u32 count)
924{
925 struct ib_recv_wr *rx_wr, *rx_wr_failed;
926 int i, ret;
927 unsigned int rx_head = isert_conn->conn_rx_desc_head;
928 struct iser_rx_desc *rx_desc;
929
930 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
931 rx_desc = &isert_conn->conn_rx_descs[rx_head];
Sagi Grimbergb0a191e2014-12-02 16:57:39 +0200932 rx_wr->wr_id = (uintptr_t)rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 rx_wr->sg_list = &rx_desc->rx_sg;
934 rx_wr->num_sge = 1;
935 rx_wr->next = rx_wr + 1;
936 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
937 }
938
939 rx_wr--;
940 rx_wr->next = NULL; /* mark end of work requests list */
941
942 isert_conn->post_recv_buf_count += count;
943 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
944 &rx_wr_failed);
945 if (ret) {
946 pr_err("ib_post_recv() failed with ret: %d\n", ret);
947 isert_conn->post_recv_buf_count -= count;
948 } else {
949 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
950 isert_conn->conn_rx_desc_head = rx_head;
951 }
952 return ret;
953}
954
955static int
956isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
957{
958 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
959 struct ib_send_wr send_wr, *send_wr_failed;
960 int ret;
961
962 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
963 ISER_HEADERS_LEN, DMA_TO_DEVICE);
964
965 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +0200966 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800967 send_wr.sg_list = tx_desc->tx_sg;
968 send_wr.num_sge = tx_desc->num_sge;
969 send_wr.opcode = IB_WR_SEND;
970 send_wr.send_flags = IB_SEND_SIGNALED;
971
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800972 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200973 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800974 pr_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800975
976 return ret;
977}
978
979static void
980isert_create_send_desc(struct isert_conn *isert_conn,
981 struct isert_cmd *isert_cmd,
982 struct iser_tx_desc *tx_desc)
983{
984 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
985
986 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
987 ISER_HEADERS_LEN, DMA_TO_DEVICE);
988
989 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
990 tx_desc->iser_header.flags = ISER_VER;
991
992 tx_desc->num_sge = 1;
993 tx_desc->isert_cmd = isert_cmd;
994
995 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
996 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
997 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
998 }
999}
1000
1001static int
1002isert_init_tx_hdrs(struct isert_conn *isert_conn,
1003 struct iser_tx_desc *tx_desc)
1004{
1005 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1006 u64 dma_addr;
1007
1008 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1009 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1010 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1011 pr_err("ib_dma_mapping_error() failed\n");
1012 return -ENOMEM;
1013 }
1014
1015 tx_desc->dma_addr = dma_addr;
1016 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1017 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1018 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1019
1020 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1021 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1022 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1023
1024 return 0;
1025}
1026
1027static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001028isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001029 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001030{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001031 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1032
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001034 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001035 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001036 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001037 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001038 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001039}
1040
1041static int
1042isert_rdma_post_recvl(struct isert_conn *isert_conn)
1043{
1044 struct ib_recv_wr rx_wr, *rx_wr_fail;
1045 struct ib_sge sge;
1046 int ret;
1047
1048 memset(&sge, 0, sizeof(struct ib_sge));
1049 sge.addr = isert_conn->login_req_dma;
1050 sge.length = ISER_RX_LOGIN_SIZE;
1051 sge.lkey = isert_conn->conn_mr->lkey;
1052
1053 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1054 sge.addr, sge.length, sge.lkey);
1055
1056 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001057 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001058 rx_wr.sg_list = &sge;
1059 rx_wr.num_sge = 1;
1060
1061 isert_conn->post_recv_buf_count++;
1062 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1063 if (ret) {
1064 pr_err("ib_post_recv() failed: %d\n", ret);
1065 isert_conn->post_recv_buf_count--;
1066 }
1067
1068 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1069 return ret;
1070}
1071
1072static int
1073isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1074 u32 length)
1075{
1076 struct isert_conn *isert_conn = conn->context;
1077 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1078 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1079 int ret;
1080
1081 isert_create_send_desc(isert_conn, NULL, tx_desc);
1082
1083 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1084 sizeof(struct iscsi_hdr));
1085
1086 isert_init_tx_hdrs(isert_conn, tx_desc);
1087
1088 if (length > 0) {
1089 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1090
1091 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1092 length, DMA_TO_DEVICE);
1093
1094 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1095
1096 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1097 length, DMA_TO_DEVICE);
1098
1099 tx_dsg->addr = isert_conn->login_rsp_dma;
1100 tx_dsg->length = length;
1101 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1102 tx_desc->num_sge = 2;
1103 }
1104 if (!login->login_failed) {
1105 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001106 if (!conn->sess->sess_ops->SessionType &&
1107 isert_conn->conn_device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001108 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001109 if (ret) {
1110 pr_err("Conn: %p failed to create"
1111 " fastreg pool\n", isert_conn);
1112 return ret;
1113 }
1114 }
1115
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001116 ret = isert_alloc_rx_descriptors(isert_conn);
1117 if (ret)
1118 return ret;
1119
1120 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1121 if (ret)
1122 return ret;
1123
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001124 /* Now we are in FULL_FEATURE phase */
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001125 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001126 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001127 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001128 goto post_send;
1129 }
1130
1131 ret = isert_rdma_post_recvl(isert_conn);
1132 if (ret)
1133 return ret;
1134 }
1135post_send:
1136 ret = isert_post_send(isert_conn, tx_desc);
1137 if (ret)
1138 return ret;
1139
1140 return 0;
1141}
1142
1143static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001144isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001145{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001146 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1147 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001148 struct iscsi_conn *conn = isert_conn->conn;
1149 struct iscsi_login *login = conn->conn_login;
1150 int size;
1151
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001152 pr_info("conn %p\n", isert_conn);
1153
1154 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001155
1156 if (login->first_request) {
1157 struct iscsi_login_req *login_req =
1158 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1159 /*
1160 * Setup the initial iscsi_login values from the leading
1161 * login request PDU.
1162 */
1163 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1164 login->current_stage =
1165 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1166 >> 2;
1167 login->version_min = login_req->min_version;
1168 login->version_max = login_req->max_version;
1169 memcpy(login->isid, login_req->isid, 6);
1170 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1171 login->init_task_tag = login_req->itt;
1172 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1173 login->cid = be16_to_cpu(login_req->cid);
1174 login->tsih = be16_to_cpu(login_req->tsih);
1175 }
1176
1177 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1178
1179 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1180 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1181 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1182 memcpy(login->req_buf, &rx_desc->data[0], size);
1183
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001184 if (login->first_request) {
1185 complete(&isert_conn->conn_login_comp);
1186 return;
1187 }
1188 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001189}
1190
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001192*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001193{
1194 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1195 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001196 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001197
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001198 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001199 if (!cmd) {
1200 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001201 return NULL;
1202 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001203 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001204 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001205 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001206
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001207 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001208}
1209
1210static int
1211isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001212 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1213 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001214{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001215 struct iscsi_conn *conn = isert_conn->conn;
1216 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1217 struct scatterlist *sg;
1218 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1219 bool dump_payload = false;
1220
1221 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1222 if (rc < 0)
1223 return rc;
1224
1225 imm_data = cmd->immediate_data;
1226 imm_data_len = cmd->first_burst_len;
1227 unsol_data = cmd->unsolicited_data;
1228
1229 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1230 if (rc < 0) {
1231 return 0;
1232 } else if (rc > 0) {
1233 dump_payload = true;
1234 goto sequence_cmd;
1235 }
1236
1237 if (!imm_data)
1238 return 0;
1239
1240 sg = &cmd->se_cmd.t_data_sg[0];
1241 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1242
1243 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1244 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1245
1246 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1247
1248 cmd->write_data_done += imm_data_len;
1249
1250 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1251 spin_lock_bh(&cmd->istate_lock);
1252 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1253 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1254 spin_unlock_bh(&cmd->istate_lock);
1255 }
1256
1257sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001258 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001259
1260 if (!rc && dump_payload == false && unsol_data)
1261 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001262 else if (dump_payload && imm_data)
1263 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001265 return 0;
1266}
1267
1268static int
1269isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1270 struct iser_rx_desc *rx_desc, unsigned char *buf)
1271{
1272 struct scatterlist *sg_start;
1273 struct iscsi_conn *conn = isert_conn->conn;
1274 struct iscsi_cmd *cmd = NULL;
1275 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1276 u32 unsol_data_len = ntoh24(hdr->dlength);
1277 int rc, sg_nents, sg_off, page_off;
1278
1279 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1280 if (rc < 0)
1281 return rc;
1282 else if (!cmd)
1283 return 0;
1284 /*
1285 * FIXME: Unexpected unsolicited_data out
1286 */
1287 if (!cmd->unsolicited_data) {
1288 pr_err("Received unexpected solicited data payload\n");
1289 dump_stack();
1290 return -1;
1291 }
1292
1293 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1294 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1295
1296 sg_off = cmd->write_data_done / PAGE_SIZE;
1297 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1298 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1299 page_off = cmd->write_data_done % PAGE_SIZE;
1300 /*
1301 * FIXME: Non page-aligned unsolicited_data out
1302 */
1303 if (page_off) {
1304 pr_err("Received unexpected non-page aligned data payload\n");
1305 dump_stack();
1306 return -1;
1307 }
1308 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1309 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1310
1311 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1312 unsol_data_len);
1313
1314 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1315 if (rc < 0)
1316 return rc;
1317
1318 return 0;
1319}
1320
1321static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001322isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001323 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1324 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001325{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001326 struct iscsi_conn *conn = isert_conn->conn;
1327 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1328 int rc;
1329
1330 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1331 if (rc < 0)
1332 return rc;
1333 /*
1334 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1335 */
1336
1337 return iscsit_process_nop_out(conn, cmd, hdr);
1338}
1339
1340static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001341isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001342 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1343 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001344{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001345 struct iscsi_conn *conn = isert_conn->conn;
1346 u32 payload_length = ntoh24(hdr->dlength);
1347 int rc;
1348 unsigned char *text_in;
1349
1350 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1351 if (rc < 0)
1352 return rc;
1353
1354 text_in = kzalloc(payload_length, GFP_KERNEL);
1355 if (!text_in) {
1356 pr_err("Unable to allocate text_in of payload_length: %u\n",
1357 payload_length);
1358 return -ENOMEM;
1359 }
1360 cmd->text_in_ptr = text_in;
1361
1362 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1363
1364 return iscsit_process_text_cmd(conn, cmd, hdr);
1365}
1366
1367static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001368isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1369 uint32_t read_stag, uint64_t read_va,
1370 uint32_t write_stag, uint64_t write_va)
1371{
1372 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1373 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001374 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001375 struct iscsi_cmd *cmd;
1376 struct isert_cmd *isert_cmd;
1377 int ret = -EINVAL;
1378 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1379
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001380 if (sess->sess_ops->SessionType &&
1381 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1382 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1383 " ignoring\n", opcode);
1384 return 0;
1385 }
1386
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001387 switch (opcode) {
1388 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001389 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001390 if (!cmd)
1391 break;
1392
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001393 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001394 isert_cmd->read_stag = read_stag;
1395 isert_cmd->read_va = read_va;
1396 isert_cmd->write_stag = write_stag;
1397 isert_cmd->write_va = write_va;
1398
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001399 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400 rx_desc, (unsigned char *)hdr);
1401 break;
1402 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001403 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001404 if (!cmd)
1405 break;
1406
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001407 isert_cmd = iscsit_priv_cmd(cmd);
1408 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001409 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001410 break;
1411 case ISCSI_OP_SCSI_DATA_OUT:
1412 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1413 (unsigned char *)hdr);
1414 break;
1415 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001416 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001417 if (!cmd)
1418 break;
1419
1420 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1421 (unsigned char *)hdr);
1422 break;
1423 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001424 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001425 if (!cmd)
1426 break;
1427
1428 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001429 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001430 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001431 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001432 if (!cmd)
1433 break;
1434
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001435 isert_cmd = iscsit_priv_cmd(cmd);
1436 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001437 rx_desc, (struct iscsi_text *)hdr);
1438 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001439 default:
1440 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1441 dump_stack();
1442 break;
1443 }
1444
1445 return ret;
1446}
1447
1448static void
1449isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1450{
1451 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1452 uint64_t read_va = 0, write_va = 0;
1453 uint32_t read_stag = 0, write_stag = 0;
1454 int rc;
1455
1456 switch (iser_hdr->flags & 0xF0) {
1457 case ISCSI_CTRL:
1458 if (iser_hdr->flags & ISER_RSV) {
1459 read_stag = be32_to_cpu(iser_hdr->read_stag);
1460 read_va = be64_to_cpu(iser_hdr->read_va);
1461 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1462 read_stag, (unsigned long long)read_va);
1463 }
1464 if (iser_hdr->flags & ISER_WSV) {
1465 write_stag = be32_to_cpu(iser_hdr->write_stag);
1466 write_va = be64_to_cpu(iser_hdr->write_va);
1467 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1468 write_stag, (unsigned long long)write_va);
1469 }
1470
1471 pr_debug("ISER ISCSI_CTRL PDU\n");
1472 break;
1473 case ISER_HELLO:
1474 pr_err("iSER Hello message\n");
1475 break;
1476 default:
1477 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1478 break;
1479 }
1480
1481 rc = isert_rx_opcode(isert_conn, rx_desc,
1482 read_stag, read_va, write_stag, write_va);
1483}
1484
1485static void
1486isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02001487 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001488{
1489 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1490 struct iscsi_hdr *hdr;
1491 u64 rx_dma;
1492 int rx_buflen, outstanding;
1493
1494 if ((char *)desc == isert_conn->login_req_buf) {
1495 rx_dma = isert_conn->login_req_dma;
1496 rx_buflen = ISER_RX_LOGIN_SIZE;
1497 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1498 rx_dma, rx_buflen);
1499 } else {
1500 rx_dma = desc->dma_addr;
1501 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1502 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1503 rx_dma, rx_buflen);
1504 }
1505
1506 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1507
1508 hdr = &desc->iscsi_header;
1509 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1510 hdr->opcode, hdr->itt, hdr->flags,
1511 (int)(xfer_len - ISER_HEADERS_LEN));
1512
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001513 if ((char *)desc == isert_conn->login_req_buf) {
1514 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1515 if (isert_conn->conn) {
1516 struct iscsi_login *login = isert_conn->conn->conn_login;
1517
1518 if (login && !login->first_request)
1519 isert_rx_login_req(isert_conn);
1520 }
1521 mutex_lock(&isert_conn->conn_mutex);
1522 complete(&isert_conn->login_req_comp);
1523 mutex_unlock(&isert_conn->conn_mutex);
1524 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001525 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001526 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527
1528 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1529 DMA_FROM_DEVICE);
1530
1531 isert_conn->post_recv_buf_count--;
1532 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1533 isert_conn->post_recv_buf_count);
1534
1535 if ((char *)desc == isert_conn->login_req_buf)
1536 return;
1537
1538 outstanding = isert_conn->post_recv_buf_count;
1539 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1540 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1541 ISERT_MIN_POSTED_RX);
1542 err = isert_post_recv(isert_conn, count);
1543 if (err) {
1544 pr_err("isert_post_recv() count: %d failed, %d\n",
1545 count, err);
1546 }
1547 }
1548}
1549
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001550static int
1551isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1552 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1553 enum iser_ib_op_code op, struct isert_data_buf *data)
1554{
1555 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1556
1557 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1558 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1559
1560 data->len = length - offset;
1561 data->offset = offset;
1562 data->sg_off = data->offset / PAGE_SIZE;
1563
1564 data->sg = &sg[data->sg_off];
1565 data->nents = min_t(unsigned int, nents - data->sg_off,
1566 ISCSI_ISER_SG_TABLESIZE);
1567 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1568 PAGE_SIZE);
1569
1570 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1571 data->dma_dir);
1572 if (unlikely(!data->dma_nents)) {
1573 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1574 return -EINVAL;
1575 }
1576
1577 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1578 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1579
1580 return 0;
1581}
1582
1583static void
1584isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1585{
1586 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1587
1588 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1589 memset(data, 0, sizeof(*data));
1590}
1591
1592
1593
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001594static void
1595isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1596{
1597 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001598
Vu Pham90ecc6e2013-08-28 23:23:33 +03001599 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001600
1601 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001602 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001603 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001604 }
1605
Vu Pham90ecc6e2013-08-28 23:23:33 +03001606 if (wr->send_wr) {
1607 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1608 kfree(wr->send_wr);
1609 wr->send_wr = NULL;
1610 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611
Vu Pham90ecc6e2013-08-28 23:23:33 +03001612 if (wr->ib_sge) {
1613 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1614 kfree(wr->ib_sge);
1615 wr->ib_sge = NULL;
1616 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001617}
1618
1619static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001620isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001621{
1622 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001623 LIST_HEAD(unmap_list);
1624
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001625 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001626
1627 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001628 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001629 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001630 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1631 isert_unmap_data_buf(isert_conn, &wr->prot);
1632 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1633 }
Vu Pham59464ef2013-08-28 23:23:35 +03001634 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001635 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001636 spin_unlock_bh(&isert_conn->conn_lock);
1637 wr->fr_desc = NULL;
1638 }
1639
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001640 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001641 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001642 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001643 }
1644
1645 wr->ib_sge = NULL;
1646 wr->send_wr = NULL;
1647}
1648
1649static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001650isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001652 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001653 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001654 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001655 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001656
1657 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1658
1659 switch (cmd->iscsi_opcode) {
1660 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001661 spin_lock_bh(&conn->cmd_lock);
1662 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001663 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001664 spin_unlock_bh(&conn->cmd_lock);
1665
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001666 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001667 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001668 /*
1669 * Check for special case during comp_err where
1670 * WRITE_PENDING has been handed off from core,
1671 * but requires an extra target_put_sess_cmd()
1672 * before transport_generic_free_cmd() below.
1673 */
1674 if (comp_err &&
1675 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1676 struct se_cmd *se_cmd = &cmd->se_cmd;
1677
1678 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1679 }
1680 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001681
Vu Phamd40945d2013-08-28 23:23:34 +03001682 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001683 transport_generic_free_cmd(&cmd->se_cmd, 0);
1684 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001685 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001686 spin_lock_bh(&conn->cmd_lock);
1687 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001688 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001689 spin_unlock_bh(&conn->cmd_lock);
1690
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001691 transport_generic_free_cmd(&cmd->se_cmd, 0);
1692 break;
1693 case ISCSI_OP_REJECT:
1694 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001695 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696 spin_lock_bh(&conn->cmd_lock);
1697 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001698 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001699 spin_unlock_bh(&conn->cmd_lock);
1700
1701 /*
1702 * Handle special case for REJECT when iscsi_add_reject*() has
1703 * overwritten the original iscsi_opcode assignment, and the
1704 * associated cmd->se_cmd needs to be released.
1705 */
1706 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001707 pr_debug("Calling transport_generic_free_cmd from"
1708 " isert_put_cmd for 0x%02x\n",
1709 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001710 transport_generic_free_cmd(&cmd->se_cmd, 0);
1711 break;
1712 }
1713 /*
1714 * Fall-through
1715 */
1716 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001717 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001718 break;
1719 }
1720}
1721
1722static void
1723isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1724{
1725 if (tx_desc->dma_addr != 0) {
1726 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1727 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1728 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1729 tx_desc->dma_addr = 0;
1730 }
1731}
1732
1733static void
1734isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001735 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001736{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001737 if (isert_cmd->pdu_buf_dma != 0) {
1738 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1739 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1740 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1741 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001742 }
1743
1744 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001745 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001746}
1747
Sagi Grimberg96b79732014-03-17 12:52:18 +02001748static int
1749isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1750{
1751 struct ib_mr_status mr_status;
1752 int ret;
1753
1754 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1755 if (ret) {
1756 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1757 goto fail_mr_status;
1758 }
1759
1760 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1761 u64 sec_offset_err;
1762 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1763
1764 switch (mr_status.sig_err.err_type) {
1765 case IB_SIG_BAD_GUARD:
1766 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1767 break;
1768 case IB_SIG_BAD_REFTAG:
1769 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1770 break;
1771 case IB_SIG_BAD_APPTAG:
1772 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1773 break;
1774 }
1775 sec_offset_err = mr_status.sig_err.sig_err_offset;
1776 do_div(sec_offset_err, block_size);
1777 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1778
1779 pr_err("isert: PI error found type %d at sector 0x%llx "
1780 "expected 0x%x vs actual 0x%x\n",
1781 mr_status.sig_err.err_type,
1782 (unsigned long long)se_cmd->bad_sector,
1783 mr_status.sig_err.expected,
1784 mr_status.sig_err.actual);
1785 ret = 1;
1786 }
1787
1788fail_mr_status:
1789 return ret;
1790}
1791
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001792static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001793isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1794 struct isert_cmd *isert_cmd)
1795{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001796 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001797 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001798 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001799 struct isert_conn *isert_conn = isert_cmd->conn;
1800 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001801 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001802
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001803 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001804 ret = isert_check_pi_status(se_cmd,
1805 wr->fr_desc->pi_ctx->sig_mr);
1806 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001807 }
1808
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001809 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001810 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001811 if (ret)
1812 transport_send_check_condition_and_sense(se_cmd,
1813 se_cmd->pi_err, 0);
1814 else
1815 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001816}
1817
1818static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001819isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1820 struct isert_cmd *isert_cmd)
1821{
1822 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001823 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001824 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001825 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001826 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001827 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001828
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001829 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001830 ret = isert_check_pi_status(se_cmd,
1831 wr->fr_desc->pi_ctx->sig_mr);
1832 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001833 }
1834
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001836 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001837 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001838 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001839
Vu Pham90ecc6e2013-08-28 23:23:33 +03001840 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001841 spin_lock_bh(&cmd->istate_lock);
1842 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1843 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1844 spin_unlock_bh(&cmd->istate_lock);
1845
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001846 if (ret)
1847 transport_send_check_condition_and_sense(se_cmd,
1848 se_cmd->pi_err, 0);
1849 else
1850 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001851}
1852
1853static void
1854isert_do_control_comp(struct work_struct *work)
1855{
1856 struct isert_cmd *isert_cmd = container_of(work,
1857 struct isert_cmd, comp_work);
1858 struct isert_conn *isert_conn = isert_cmd->conn;
1859 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001860 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861
1862 switch (cmd->i_state) {
1863 case ISTATE_SEND_TASKMGTRSP:
1864 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1865
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001866 iscsit_tmr_post_handler(cmd, cmd->conn);
1867
1868 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001869 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001870 break;
1871 case ISTATE_SEND_REJECT:
1872 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001873
1874 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001875 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001876 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001877 case ISTATE_SEND_LOGOUTRSP:
1878 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001879
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001880 iscsit_logout_post_handler(cmd, cmd->conn);
1881 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001882 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001883 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001884 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001885 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001886 default:
1887 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1888 dump_stack();
1889 break;
1890 }
1891}
1892
1893static void
1894isert_response_completion(struct iser_tx_desc *tx_desc,
1895 struct isert_cmd *isert_cmd,
1896 struct isert_conn *isert_conn,
1897 struct ib_device *ib_dev)
1898{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001899 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001900
1901 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001902 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001903 cmd->i_state == ISTATE_SEND_REJECT ||
1904 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001905 isert_unmap_tx_desc(tx_desc, ib_dev);
1906
1907 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1908 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1909 return;
1910 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001911
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001912 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001913 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001914}
1915
1916static void
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001917isert_send_completion(struct iser_tx_desc *tx_desc,
1918 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001919{
1920 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1921 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1922 struct isert_rdma_wr *wr;
1923
1924 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925 isert_unmap_tx_desc(tx_desc, ib_dev);
1926 return;
1927 }
1928 wr = &isert_cmd->rdma_wr;
1929
1930 switch (wr->iser_ib_op) {
1931 case ISER_IB_RECV:
1932 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1933 dump_stack();
1934 break;
1935 case ISER_IB_SEND:
1936 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1937 isert_response_completion(tx_desc, isert_cmd,
1938 isert_conn, ib_dev);
1939 break;
1940 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001941 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001942 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001943 break;
1944 case ISER_IB_RDMA_READ:
1945 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1946
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001947 isert_completion_rdma_read(tx_desc, isert_cmd);
1948 break;
1949 default:
1950 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1951 dump_stack();
1952 break;
1953 }
1954}
1955
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001956/**
1957 * is_isert_tx_desc() - Indicate if the completion wr_id
1958 * is a TX descriptor or not.
1959 * @isert_conn: iser connection
1960 * @wr_id: completion WR identifier
1961 *
1962 * Since we cannot rely on wc opcode in FLUSH errors
1963 * we must work around it by checking if the wr_id address
1964 * falls in the iser connection rx_descs buffer. If so
1965 * it is an RX descriptor, otherwize it is a TX.
1966 */
1967static inline bool
1968is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969{
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001970 void *start = isert_conn->conn_rx_descs;
1971 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
1972
1973 if (wr_id >= start && wr_id < start + len)
1974 return false;
1975
1976 return true;
1977}
1978
1979static void
1980isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
1981{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001982 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001983 pr_info("conn %p completing conn_wait_comp_err\n",
1984 isert_conn);
1985 complete(&isert_conn->conn_wait_comp_err);
1986 } else
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001987 if (is_isert_tx_desc(isert_conn, (void *)wc->wr_id)) {
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02001988 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1989 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001990 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001991
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001992 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
1993 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02001994 if (!isert_cmd)
1995 isert_unmap_tx_desc(desc, ib_dev);
1996 else
1997 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02001998 } else {
1999 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002000 if (!isert_conn->post_recv_buf_count)
2001 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002002 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003}
2004
2005static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002006isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002007{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002008 struct isert_conn *isert_conn;
2009 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002010 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002011
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002012 isert_conn = wc->qp->qp_context;
2013 if (likely(wc->status == IB_WC_SUCCESS)) {
2014 if (wc->opcode == IB_WC_RECV) {
2015 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
2016 isert_rx_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002017 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002018 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2019 isert_send_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002020 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002021 } else {
2022 if (wc->status != IB_WC_WR_FLUSH_ERR)
2023 pr_err("wr id %llx status %d vend_err %x\n",
2024 wc->wr_id, wc->status, wc->vendor_err);
2025 else
2026 pr_debug("flush error: wr id %llx\n", wc->wr_id);
2027
2028 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2029 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002031}
2032
2033static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002034isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002035{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002036 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002037 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002038 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002039 struct ib_wc *const wcs = comp->wcs;
2040 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002041
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002042 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2043 for (i = 0; i < n; i++)
2044 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002045
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002046 completed += n;
2047 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002048 break;
2049 }
2050
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002051 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052}
2053
2054static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002055isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002056{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002057 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002058
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002059 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002060}
2061
2062static int
2063isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2064{
2065 struct ib_send_wr *wr_failed;
2066 int ret;
2067
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002068 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2069 &wr_failed);
2070 if (ret) {
2071 pr_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002072 return ret;
2073 }
2074 return ret;
2075}
2076
2077static int
2078isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2079{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002080 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2082 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2083 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2084 &isert_cmd->tx_desc.iscsi_header;
2085
2086 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2087 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2088 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2089 /*
2090 * Attach SENSE DATA payload to iSCSI Response PDU
2091 */
2092 if (cmd->se_cmd.sense_buffer &&
2093 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2094 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2095 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2096 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002097 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002098
2099 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2100 cmd->sense_buffer);
2101 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2102
2103 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2104 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002105 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002106
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002107 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2108 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002109 DMA_TO_DEVICE);
2110
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002111 isert_cmd->pdu_buf_len = pdu_len;
2112 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2113 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002114 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2115 isert_cmd->tx_desc.num_sge = 2;
2116 }
2117
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002118 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002119
2120 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2121
2122 return isert_post_response(isert_conn, isert_cmd);
2123}
2124
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002125static void
2126isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2127{
2128 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2129 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2130 struct isert_device *device = isert_conn->conn_device;
2131
2132 spin_lock_bh(&conn->cmd_lock);
2133 if (!list_empty(&cmd->i_conn_node))
2134 list_del_init(&cmd->i_conn_node);
2135 spin_unlock_bh(&conn->cmd_lock);
2136
2137 if (cmd->data_direction == DMA_TO_DEVICE)
2138 iscsit_stop_dataout_timer(cmd);
2139
2140 device->unreg_rdma_mem(isert_cmd, isert_conn);
2141}
2142
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002143static enum target_prot_op
2144isert_get_sup_prot_ops(struct iscsi_conn *conn)
2145{
2146 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2147 struct isert_device *device = isert_conn->conn_device;
2148
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002149 if (conn->tpg->tpg_attrib.t10_pi) {
2150 if (device->pi_capable) {
2151 pr_info("conn %p PI offload enabled\n", isert_conn);
2152 isert_conn->pi_support = true;
2153 return TARGET_PROT_ALL;
2154 }
2155 }
2156
2157 pr_info("conn %p PI offload disabled\n", isert_conn);
2158 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002159
2160 return TARGET_PROT_NORMAL;
2161}
2162
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002163static int
2164isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2165 bool nopout_response)
2166{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002167 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002168 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2169 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2170
2171 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2172 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2173 &isert_cmd->tx_desc.iscsi_header,
2174 nopout_response);
2175 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002176 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002177
Masanari Iida8b513d02013-05-21 23:13:12 +09002178 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179
2180 return isert_post_response(isert_conn, isert_cmd);
2181}
2182
2183static int
2184isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2185{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002186 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2188 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2189
2190 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2191 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2192 &isert_cmd->tx_desc.iscsi_header);
2193 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002194 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002195
2196 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2197
2198 return isert_post_response(isert_conn, isert_cmd);
2199}
2200
2201static int
2202isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2203{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002204 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002205 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2206 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2207
2208 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2209 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2210 &isert_cmd->tx_desc.iscsi_header);
2211 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002212 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002213
2214 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2215
2216 return isert_post_response(isert_conn, isert_cmd);
2217}
2218
2219static int
2220isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2221{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002222 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002223 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2224 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002225 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2226 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2227 struct iscsi_reject *hdr =
2228 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002229
2230 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002231 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002232 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002233
2234 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002235 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002236 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2237 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002238 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2239 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002240 tx_dsg->length = ISCSI_HDR_LEN;
2241 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2242 isert_cmd->tx_desc.num_sge = 2;
2243
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002244 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002245
2246 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2247
2248 return isert_post_response(isert_conn, isert_cmd);
2249}
2250
2251static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002252isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2253{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002254 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002255 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2256 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2257 struct iscsi_text_rsp *hdr =
2258 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2259 u32 txt_rsp_len;
2260 int rc;
2261
2262 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002263 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002264 if (rc < 0)
2265 return rc;
2266
2267 txt_rsp_len = rc;
2268 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2269
2270 if (txt_rsp_len) {
2271 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2272 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2273 void *txt_rsp_buf = cmd->buf_ptr;
2274
2275 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2276 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2277
2278 isert_cmd->pdu_buf_len = txt_rsp_len;
2279 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2280 tx_dsg->length = txt_rsp_len;
2281 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2282 isert_cmd->tx_desc.num_sge = 2;
2283 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002284 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002285
2286 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2287
2288 return isert_post_response(isert_conn, isert_cmd);
2289}
2290
2291static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002292isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2293 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2294 u32 data_left, u32 offset)
2295{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002296 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002297 struct scatterlist *sg_start, *tmp_sg;
2298 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2299 u32 sg_off, page_off;
2300 int i = 0, sg_nents;
2301
2302 sg_off = offset / PAGE_SIZE;
2303 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2304 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2305 page_off = offset % PAGE_SIZE;
2306
2307 send_wr->sg_list = ib_sge;
2308 send_wr->num_sge = sg_nents;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002309 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002310 /*
2311 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2312 */
2313 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2314 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2315 (unsigned long long)tmp_sg->dma_address,
2316 tmp_sg->length, page_off);
2317
2318 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2319 ib_sge->length = min_t(u32, data_left,
2320 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2321 ib_sge->lkey = isert_conn->conn_mr->lkey;
2322
Vu Pham90ecc6e2013-08-28 23:23:33 +03002323 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2324 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002325 page_off = 0;
2326 data_left -= ib_sge->length;
2327 ib_sge++;
2328 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2329 }
2330
2331 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2332 send_wr->sg_list, send_wr->num_sge);
2333
2334 return sg_nents;
2335}
2336
2337static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002338isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2339 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340{
2341 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002342 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002343 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002344 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002345 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002346 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002347 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2348 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002350 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002351
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002352 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2353 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2354 se_cmd->t_data_nents, se_cmd->data_length,
2355 offset, wr->iser_ib_op, &wr->data);
2356 if (ret)
2357 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002358
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002359 data_left = data->len;
2360 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002361
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002362 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002363 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002364 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002365 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002366 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002367 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002368 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002370 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002371 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2372 GFP_KERNEL);
2373 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002374 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002375 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002376 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002377 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002378
2379 wr->isert_cmd = isert_cmd;
2380 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002381
2382 for (i = 0; i < wr->send_wr_num; i++) {
2383 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2384 data_len = min(data_left, rdma_write_max);
2385
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002386 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002387 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2388 send_wr->opcode = IB_WR_RDMA_WRITE;
2389 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2390 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2391 if (i + 1 == wr->send_wr_num)
2392 send_wr->next = &isert_cmd->tx_desc.send_wr;
2393 else
2394 send_wr->next = &wr->send_wr[i + 1];
2395 } else {
2396 send_wr->opcode = IB_WR_RDMA_READ;
2397 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2398 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2399 if (i + 1 == wr->send_wr_num)
2400 send_wr->send_flags = IB_SEND_SIGNALED;
2401 else
2402 send_wr->next = &wr->send_wr[i + 1];
2403 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002404
2405 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2406 send_wr, data_len, offset);
2407 ib_sge += ib_sge_cnt;
2408
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002409 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002410 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411 data_left -= data_len;
2412 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002413
2414 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002415unmap_cmd:
2416 isert_unmap_data_buf(isert_conn, data);
2417
Vu Pham90ecc6e2013-08-28 23:23:33 +03002418 return ret;
2419}
2420
2421static int
Vu Pham59464ef2013-08-28 23:23:35 +03002422isert_map_fr_pagelist(struct ib_device *ib_dev,
2423 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2424{
2425 u64 start_addr, end_addr, page, chunk_start = 0;
2426 struct scatterlist *tmp_sg;
2427 int i = 0, new_chunk, last_ent, n_pages;
2428
2429 n_pages = 0;
2430 new_chunk = 1;
2431 last_ent = sg_nents - 1;
2432 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2433 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2434 if (new_chunk)
2435 chunk_start = start_addr;
2436 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2437
2438 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2439 i, (unsigned long long)tmp_sg->dma_address,
2440 tmp_sg->length);
2441
2442 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2443 new_chunk = 0;
2444 continue;
2445 }
2446 new_chunk = 1;
2447
2448 page = chunk_start & PAGE_MASK;
2449 do {
2450 fr_pl[n_pages++] = page;
2451 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2452 n_pages - 1, page);
2453 page += PAGE_SIZE;
2454 } while (page < end_addr);
2455 }
2456
2457 return n_pages;
2458}
2459
2460static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002461isert_fast_reg_mr(struct isert_conn *isert_conn,
2462 struct fast_reg_descriptor *fr_desc,
2463 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002464 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002465 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002466{
Vu Pham59464ef2013-08-28 23:23:35 +03002467 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002468 struct ib_mr *mr;
2469 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002470 struct ib_send_wr fr_wr, inv_wr;
2471 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002472 int ret, pagelist_len;
2473 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002474 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002475
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002476 if (mem->dma_nents == 1) {
2477 sge->lkey = isert_conn->conn_mr->lkey;
2478 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2479 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002480 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2481 __func__, __LINE__, sge->addr, sge->length,
2482 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002483 return 0;
2484 }
2485
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002486 if (ind == ISERT_DATA_KEY_VALID) {
2487 /* Registering data buffer */
2488 mr = fr_desc->data_mr;
2489 frpl = fr_desc->data_frpl;
2490 } else {
2491 /* Registering protection buffer */
2492 mr = fr_desc->pi_ctx->prot_mr;
2493 frpl = fr_desc->pi_ctx->prot_frpl;
2494 }
2495
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002496 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002497
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002498 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002499 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002500
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002501 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002502 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002503
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002504 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002505 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002506 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002507 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002508 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002509 wr = &inv_wr;
2510 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002511 key = (u8)(mr->rkey & 0x000000FF);
2512 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002513 }
2514
2515 /* Prepare FASTREG WR */
2516 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002517 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002518 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002519 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2520 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002521 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2522 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002523 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002524 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002525 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2526
2527 if (!wr)
2528 wr = &fr_wr;
2529 else
2530 wr->next = &fr_wr;
2531
2532 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2533 if (ret) {
2534 pr_err("fast registration failed, ret:%d\n", ret);
2535 return ret;
2536 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002537 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002538
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002539 sge->lkey = mr->lkey;
2540 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002541 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002542
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002543 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2544 __func__, __LINE__, sge->addr, sge->length,
2545 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002546
2547 return ret;
2548}
2549
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002550static inline void
2551isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2552 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002553{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002554 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002555 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2556 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2557 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002558 /*
2559 * At the moment we hard code those, but if in the future
2560 * the target core would like to use it, we will take it
2561 * from se_cmd.
2562 */
2563 domain->sig.dif.apptag_check_mask = 0xffff;
2564 domain->sig.dif.app_escape = true;
2565 domain->sig.dif.ref_escape = true;
2566 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2567 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2568 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002569};
2570
2571static int
2572isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2573{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002574 switch (se_cmd->prot_op) {
2575 case TARGET_PROT_DIN_INSERT:
2576 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002577 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002578 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002579 break;
2580 case TARGET_PROT_DOUT_INSERT:
2581 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002582 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002583 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002584 break;
2585 case TARGET_PROT_DIN_PASS:
2586 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002587 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2588 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002589 break;
2590 default:
2591 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2592 return -EINVAL;
2593 }
2594
2595 return 0;
2596}
2597
2598static inline u8
2599isert_set_prot_checks(u8 prot_checks)
2600{
2601 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2602 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2603 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2604}
2605
2606static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002607isert_reg_sig_mr(struct isert_conn *isert_conn,
2608 struct se_cmd *se_cmd,
2609 struct isert_rdma_wr *rdma_wr,
2610 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002611{
2612 struct ib_send_wr sig_wr, inv_wr;
2613 struct ib_send_wr *bad_wr, *wr = NULL;
2614 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2615 struct ib_sig_attrs sig_attrs;
2616 int ret;
2617 u32 key;
2618
2619 memset(&sig_attrs, 0, sizeof(sig_attrs));
2620 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2621 if (ret)
2622 goto err;
2623
2624 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2625
2626 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2627 memset(&inv_wr, 0, sizeof(inv_wr));
2628 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002629 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002630 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2631 wr = &inv_wr;
2632 /* Bump the key */
2633 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2634 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2635 }
2636
2637 memset(&sig_wr, 0, sizeof(sig_wr));
2638 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002639 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002640 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002641 sig_wr.num_sge = 1;
2642 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2643 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2644 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2645 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002646 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002647
2648 if (!wr)
2649 wr = &sig_wr;
2650 else
2651 wr->next = &sig_wr;
2652
2653 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2654 if (ret) {
2655 pr_err("fast registration failed, ret:%d\n", ret);
2656 goto err;
2657 }
2658 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2659
Sagi Grimberg570db172014-12-02 16:57:31 +02002660 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2661 rdma_wr->ib_sg[SIG].addr = 0;
2662 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002663 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2664 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2665 /*
2666 * We have protection guards on the wire
2667 * so we need to set a larget transfer
2668 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002669 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002670
2671 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002672 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2673 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002674err:
2675 return ret;
2676}
2677
Vu Pham59464ef2013-08-28 23:23:35 +03002678static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002679isert_handle_prot_cmd(struct isert_conn *isert_conn,
2680 struct isert_cmd *isert_cmd,
2681 struct isert_rdma_wr *wr)
2682{
2683 struct isert_device *device = isert_conn->conn_device;
2684 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2685 int ret;
2686
2687 if (!wr->fr_desc->pi_ctx) {
2688 ret = isert_create_pi_ctx(wr->fr_desc,
2689 device->ib_device,
2690 isert_conn->conn_pd);
2691 if (ret) {
2692 pr_err("conn %p failed to allocate pi_ctx\n",
2693 isert_conn);
2694 return ret;
2695 }
2696 }
2697
2698 if (se_cmd->t_prot_sg) {
2699 ret = isert_map_data_buf(isert_conn, isert_cmd,
2700 se_cmd->t_prot_sg,
2701 se_cmd->t_prot_nents,
2702 se_cmd->prot_length,
2703 0, wr->iser_ib_op, &wr->prot);
2704 if (ret) {
2705 pr_err("conn %p failed to map protection buffer\n",
2706 isert_conn);
2707 return ret;
2708 }
2709
2710 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2711 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2712 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2713 if (ret) {
2714 pr_err("conn %p failed to fast reg mr\n",
2715 isert_conn);
2716 goto unmap_prot_cmd;
2717 }
2718 }
2719
2720 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2721 if (ret) {
2722 pr_err("conn %p failed to fast reg mr\n",
2723 isert_conn);
2724 goto unmap_prot_cmd;
2725 }
2726 wr->fr_desc->ind |= ISERT_PROTECTED;
2727
2728 return 0;
2729
2730unmap_prot_cmd:
2731 if (se_cmd->t_prot_sg)
2732 isert_unmap_data_buf(isert_conn, &wr->prot);
2733
2734 return ret;
2735}
2736
2737static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002738isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2739 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002740{
2741 struct se_cmd *se_cmd = &cmd->se_cmd;
2742 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002743 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002744 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002745 struct ib_send_wr *send_wr;
2746 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002747 u32 offset;
2748 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002749 unsigned long flags;
2750
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002751 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2752
2753 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2754 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2755 se_cmd->t_data_nents, se_cmd->data_length,
2756 offset, wr->iser_ib_op, &wr->data);
2757 if (ret)
2758 return ret;
2759
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002760 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002761 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2762 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2763 struct fast_reg_descriptor, list);
2764 list_del(&fr_desc->list);
2765 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2766 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002767 }
2768
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002769 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002770 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002771 if (ret)
2772 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002773
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002774 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002775 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002776 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002777 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002778
Sagi Grimberg570db172014-12-02 16:57:31 +02002779 ib_sg = &wr->ib_sg[SIG];
2780 } else {
2781 ib_sg = &wr->ib_sg[DATA];
2782 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002783
Sagi Grimberg570db172014-12-02 16:57:31 +02002784 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002785 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002786 wr->send_wr_num = 1;
2787 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2788 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002789 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002790
2791 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002792 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002793 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002794 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002795 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2796 send_wr->opcode = IB_WR_RDMA_WRITE;
2797 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2798 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002799 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002800 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002801 } else {
2802 send_wr->opcode = IB_WR_RDMA_READ;
2803 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2804 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2805 send_wr->send_flags = IB_SEND_SIGNALED;
2806 }
2807
Vu Pham59464ef2013-08-28 23:23:35 +03002808 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002809
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002810unmap_cmd:
2811 if (fr_desc) {
2812 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2813 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2814 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2815 }
2816 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002817
Vu Pham59464ef2013-08-28 23:23:35 +03002818 return ret;
2819}
2820
2821static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002822isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2823{
2824 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002825 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002826 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2827 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002828 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002829 struct ib_send_wr *wr_failed;
2830 int rc;
2831
2832 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2833 isert_cmd, se_cmd->data_length);
2834 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002835 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002836 if (rc) {
2837 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2838 return rc;
2839 }
2840
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002841 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002842 /*
2843 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2844 */
2845 isert_create_send_desc(isert_conn, isert_cmd,
2846 &isert_cmd->tx_desc);
2847 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2848 &isert_cmd->tx_desc.iscsi_header);
2849 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2850 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002851 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002852 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002853 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002854 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002855
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002856 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002857 if (rc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002858 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002859
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002860 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002861 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2862 "READ\n", isert_cmd);
2863 else
2864 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2865 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002866
Vu Pham90ecc6e2013-08-28 23:23:33 +03002867 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002868}
2869
2870static int
2871isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2872{
2873 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002874 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002875 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2876 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002877 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002878 struct ib_send_wr *wr_failed;
2879 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002880
Vu Pham90ecc6e2013-08-28 23:23:33 +03002881 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2882 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002883 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002884 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002885 if (rc) {
2886 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2887 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002888 }
2889
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002890 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002891 if (rc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002892 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002893
Vu Pham90ecc6e2013-08-28 23:23:33 +03002894 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2895 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896
Vu Pham90ecc6e2013-08-28 23:23:33 +03002897 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002898}
2899
2900static int
2901isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2902{
2903 int ret;
2904
2905 switch (state) {
2906 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2907 ret = isert_put_nopin(cmd, conn, false);
2908 break;
2909 default:
2910 pr_err("Unknown immediate state: 0x%02x\n", state);
2911 ret = -EINVAL;
2912 break;
2913 }
2914
2915 return ret;
2916}
2917
2918static int
2919isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2920{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002921 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002922 int ret;
2923
2924 switch (state) {
2925 case ISTATE_SEND_LOGOUTRSP:
2926 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002927 if (!ret)
2928 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002929 break;
2930 case ISTATE_SEND_NOPIN:
2931 ret = isert_put_nopin(cmd, conn, true);
2932 break;
2933 case ISTATE_SEND_TASKMGTRSP:
2934 ret = isert_put_tm_rsp(cmd, conn);
2935 break;
2936 case ISTATE_SEND_REJECT:
2937 ret = isert_put_reject(cmd, conn);
2938 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002939 case ISTATE_SEND_TEXTRSP:
2940 ret = isert_put_text_rsp(cmd, conn);
2941 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002942 case ISTATE_SEND_STATUS:
2943 /*
2944 * Special case for sending non GOOD SCSI status from TX thread
2945 * context during pre se_cmd excecution failure.
2946 */
2947 ret = isert_put_response(conn, cmd);
2948 break;
2949 default:
2950 pr_err("Unknown response state: 0x%02x\n", state);
2951 ret = -EINVAL;
2952 break;
2953 }
2954
2955 return ret;
2956}
2957
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002958struct rdma_cm_id *
2959isert_setup_id(struct isert_np *isert_np)
2960{
2961 struct iscsi_np *np = isert_np->np;
2962 struct rdma_cm_id *id;
2963 struct sockaddr *sa;
2964 int ret;
2965
2966 sa = (struct sockaddr *)&np->np_sockaddr;
2967 pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2968
2969 id = rdma_create_id(isert_cma_handler, isert_np,
2970 RDMA_PS_TCP, IB_QPT_RC);
2971 if (IS_ERR(id)) {
2972 pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2973 ret = PTR_ERR(id);
2974 goto out;
2975 }
2976 pr_debug("id %p context %p\n", id, id->context);
2977
2978 ret = rdma_bind_addr(id, sa);
2979 if (ret) {
2980 pr_err("rdma_bind_addr() failed: %d\n", ret);
2981 goto out_id;
2982 }
2983
2984 ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
2985 if (ret) {
2986 pr_err("rdma_listen() failed: %d\n", ret);
2987 goto out_id;
2988 }
2989
2990 return id;
2991out_id:
2992 rdma_destroy_id(id);
2993out:
2994 return ERR_PTR(ret);
2995}
2996
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002997static int
2998isert_setup_np(struct iscsi_np *np,
2999 struct __kernel_sockaddr_storage *ksockaddr)
3000{
3001 struct isert_np *isert_np;
3002 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003003 int ret;
3004
3005 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3006 if (!isert_np) {
3007 pr_err("Unable to allocate struct isert_np\n");
3008 return -ENOMEM;
3009 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003010 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003011 mutex_init(&isert_np->np_accept_mutex);
3012 INIT_LIST_HEAD(&isert_np->np_accept_list);
3013 init_completion(&isert_np->np_login_comp);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003014 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003015
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003016 /*
3017 * Setup the np->np_sockaddr from the passed sockaddr setup
3018 * in iscsi_target_configfs.c code..
3019 */
3020 memcpy(&np->np_sockaddr, ksockaddr,
3021 sizeof(struct __kernel_sockaddr_storage));
3022
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003023 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003024 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003025 ret = PTR_ERR(isert_lid);
3026 goto out;
3027 }
3028
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003029 isert_np->np_cm_id = isert_lid;
3030 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003031
3032 return 0;
3033
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003034out:
3035 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003036
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003037 return ret;
3038}
3039
3040static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003041isert_rdma_accept(struct isert_conn *isert_conn)
3042{
3043 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3044 struct rdma_conn_param cp;
3045 int ret;
3046
3047 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003048 cp.initiator_depth = isert_conn->initiator_depth;
3049 cp.retry_count = 7;
3050 cp.rnr_retry_count = 7;
3051
3052 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3053
3054 ret = rdma_accept(cm_id, &cp);
3055 if (ret) {
3056 pr_err("rdma_accept() failed with: %d\n", ret);
3057 return ret;
3058 }
3059
3060 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3061
3062 return 0;
3063}
3064
3065static int
3066isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3067{
3068 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3069 int ret;
3070
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003071 pr_info("before login_req comp conn: %p\n", isert_conn);
3072 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3073 if (ret) {
3074 pr_err("isert_conn %p interrupted before got login req\n",
3075 isert_conn);
3076 return ret;
3077 }
3078 reinit_completion(&isert_conn->login_req_comp);
3079
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003080 /*
3081 * For login requests after the first PDU, isert_rx_login_req() will
3082 * kick schedule_delayed_work(&conn->login_work) as the packet is
3083 * received, which turns this callback from iscsi_target_do_login_rx()
3084 * into a NOP.
3085 */
3086 if (!login->first_request)
3087 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003088
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003089 isert_rx_login_req(isert_conn);
3090
3091 pr_info("before conn_login_comp conn: %p\n", conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003092 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3093 if (ret)
3094 return ret;
3095
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003096 pr_info("processing login->req: %p\n", login->req);
3097
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003098 return 0;
3099}
3100
3101static void
3102isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3103 struct isert_conn *isert_conn)
3104{
3105 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3106 struct rdma_route *cm_route = &cm_id->route;
3107 struct sockaddr_in *sock_in;
3108 struct sockaddr_in6 *sock_in6;
3109
3110 conn->login_family = np->np_sockaddr.ss_family;
3111
3112 if (np->np_sockaddr.ss_family == AF_INET6) {
3113 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3114 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3115 &sock_in6->sin6_addr.in6_u);
3116 conn->login_port = ntohs(sock_in6->sin6_port);
3117
3118 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3119 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3120 &sock_in6->sin6_addr.in6_u);
3121 conn->local_port = ntohs(sock_in6->sin6_port);
3122 } else {
3123 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3124 sprintf(conn->login_ip, "%pI4",
3125 &sock_in->sin_addr.s_addr);
3126 conn->login_port = ntohs(sock_in->sin_port);
3127
3128 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3129 sprintf(conn->local_ip, "%pI4",
3130 &sock_in->sin_addr.s_addr);
3131 conn->local_port = ntohs(sock_in->sin_port);
3132 }
3133}
3134
3135static int
3136isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3137{
3138 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3139 struct isert_conn *isert_conn;
3140 int max_accept = 0, ret;
3141
3142accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003143 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003144 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003145 return -ENODEV;
3146
3147 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003148 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003149 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003150 pr_debug("np_thread_state %d for isert_accept_np\n",
3151 np->np_thread_state);
3152 /**
3153 * No point in stalling here when np_thread
3154 * is in state RESET/SHUTDOWN/EXIT - bail
3155 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003156 return -ENODEV;
3157 }
3158 spin_unlock_bh(&np->np_thread_lock);
3159
3160 mutex_lock(&isert_np->np_accept_mutex);
3161 if (list_empty(&isert_np->np_accept_list)) {
3162 mutex_unlock(&isert_np->np_accept_mutex);
3163 max_accept++;
3164 goto accept_wait;
3165 }
3166 isert_conn = list_first_entry(&isert_np->np_accept_list,
3167 struct isert_conn, conn_accept_node);
3168 list_del_init(&isert_conn->conn_accept_node);
3169 mutex_unlock(&isert_np->np_accept_mutex);
3170
3171 conn->context = isert_conn;
3172 isert_conn->conn = conn;
3173 max_accept = 0;
3174
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003175 isert_set_conn_info(np, conn, isert_conn);
3176
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003177 pr_debug("Processing isert_conn: %p\n", isert_conn);
3178
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003179 return 0;
3180}
3181
3182static void
3183isert_free_np(struct iscsi_np *np)
3184{
3185 struct isert_np *isert_np = (struct isert_np *)np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003186 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003187
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003188 if (isert_np->np_cm_id)
3189 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003190
Sagi Grimberg268e6812014-12-02 16:57:36 +02003191 /*
3192 * FIXME: At this point we don't have a good way to insure
3193 * that at this point we don't have hanging connections that
3194 * completed RDMA establishment but didn't start iscsi login
3195 * process. So work-around this by cleaning up what ever piled
3196 * up in np_accept_list.
3197 */
3198 mutex_lock(&isert_np->np_accept_mutex);
3199 if (!list_empty(&isert_np->np_accept_list)) {
3200 pr_info("Still have isert connections, cleaning up...\n");
3201 list_for_each_entry_safe(isert_conn, n,
3202 &isert_np->np_accept_list,
3203 conn_accept_node) {
3204 pr_info("cleaning isert_conn %p state (%d)\n",
3205 isert_conn, isert_conn->state);
3206 isert_connect_release(isert_conn);
3207 }
3208 }
3209 mutex_unlock(&isert_np->np_accept_mutex);
3210
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003211 np->np_context = NULL;
3212 kfree(isert_np);
3213}
3214
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003215static void isert_release_work(struct work_struct *work)
3216{
3217 struct isert_conn *isert_conn = container_of(work,
3218 struct isert_conn,
3219 release_work);
3220
3221 pr_info("Starting release conn %p\n", isert_conn);
3222
3223 wait_for_completion(&isert_conn->conn_wait);
3224
3225 mutex_lock(&isert_conn->conn_mutex);
3226 isert_conn->state = ISER_CONN_DOWN;
3227 mutex_unlock(&isert_conn->conn_mutex);
3228
3229 pr_info("Destroying conn %p\n", isert_conn);
3230 isert_put_conn(isert_conn);
3231}
3232
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003233static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003234isert_wait4logout(struct isert_conn *isert_conn)
3235{
3236 struct iscsi_conn *conn = isert_conn->conn;
3237
3238 if (isert_conn->logout_posted) {
3239 pr_info("conn %p wait for conn_logout_comp\n", isert_conn);
3240 wait_for_completion_timeout(&conn->conn_logout_comp,
3241 SECONDS_FOR_LOGOUT_COMP * HZ);
3242 }
3243}
3244
3245static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003246isert_wait4cmds(struct iscsi_conn *conn)
3247{
3248 if (conn->sess) {
3249 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3250 target_wait_for_sess_cmds(conn->sess->se_sess);
3251 }
3252}
3253
3254static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003255isert_wait4flush(struct isert_conn *isert_conn)
3256{
3257 struct ib_recv_wr *bad_wr;
3258
3259 init_completion(&isert_conn->conn_wait_comp_err);
3260 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3261 /* post an indication that all flush errors were consumed */
3262 if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
3263 pr_err("conn %p failed to post beacon", isert_conn);
3264 return;
3265 }
3266
3267 wait_for_completion(&isert_conn->conn_wait_comp_err);
3268}
3269
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003270static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003271{
3272 struct isert_conn *isert_conn = conn->context;
3273
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003274 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003275
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003276 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003277 /*
3278 * Only wait for conn_wait_comp_err if the isert_conn made it
3279 * into full feature phase..
3280 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003281 if (isert_conn->state == ISER_CONN_INIT) {
3282 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003283 return;
3284 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003285 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003286 mutex_unlock(&isert_conn->conn_mutex);
3287
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003288 isert_wait4cmds(conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003289 isert_wait4flush(isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003290 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003291
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003292 INIT_WORK(&isert_conn->release_work, isert_release_work);
3293 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003294}
3295
3296static void isert_free_conn(struct iscsi_conn *conn)
3297{
3298 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003299
3300 isert_put_conn(isert_conn);
3301}
3302
3303static struct iscsit_transport iser_target_transport = {
3304 .name = "IB/iSER",
3305 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003306 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003307 .owner = THIS_MODULE,
3308 .iscsit_setup_np = isert_setup_np,
3309 .iscsit_accept_np = isert_accept_np,
3310 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003311 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003312 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003313 .iscsit_get_login_rx = isert_get_login_rx,
3314 .iscsit_put_login_tx = isert_put_login_tx,
3315 .iscsit_immediate_queue = isert_immediate_queue,
3316 .iscsit_response_queue = isert_response_queue,
3317 .iscsit_get_dataout = isert_get_dataout,
3318 .iscsit_queue_data_in = isert_put_datain,
3319 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003320 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003321 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003322};
3323
3324static int __init isert_init(void)
3325{
3326 int ret;
3327
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003328 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3329 if (!isert_comp_wq) {
3330 pr_err("Unable to allocate isert_comp_wq\n");
3331 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003332 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003333 }
3334
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003335 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3336 WQ_UNBOUND_MAX_ACTIVE);
3337 if (!isert_release_wq) {
3338 pr_err("Unable to allocate isert_release_wq\n");
3339 ret = -ENOMEM;
3340 goto destroy_comp_wq;
3341 }
3342
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003343 iscsit_register_transport(&iser_target_transport);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003344 pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3345
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003346 return 0;
3347
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003348destroy_comp_wq:
3349 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003350
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003351 return ret;
3352}
3353
3354static void __exit isert_exit(void)
3355{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003356 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003357 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003358 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003359 iscsit_unregister_transport(&iser_target_transport);
3360 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3361}
3362
3363MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3364MODULE_VERSION("0.1");
3365MODULE_AUTHOR("nab@Linux-iSCSI.org");
3366MODULE_LICENSE("GPL");
3367
3368module_init(isert_init);
3369module_exit(isert_exit);