blob: a0fd77bf8f883924b78485ad550b7c587274e4b8 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
Nicholas Bellinger95b60f02013-11-05 13:16:12 -080025#include <linux/llist.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080026#include <rdma/ib_verbs.h>
27#include <rdma/rdma_cm.h>
28#include <target/target_core_base.h>
29#include <target/target_core_fabric.h>
30#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030031#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032
33#include "isert_proto.h"
34#include "ib_isert.h"
35
36#define ISERT_MAX_CONN 8
37#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
39
40static DEFINE_MUTEX(device_list_mutex);
41static LIST_HEAD(device_list);
42static struct workqueue_struct *isert_rx_wq;
43static struct workqueue_struct *isert_comp_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044
45static void
Vu Phamd40945d2013-08-28 23:23:34 +030046isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47static int
48isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
49 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030050static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020051isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030052static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020053isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020055static int
56isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020057static int
58isert_rdma_post_recvl(struct isert_conn *isert_conn);
59static int
60isert_rdma_accept(struct isert_conn *isert_conn);
Vu Phamd40945d2013-08-28 23:23:34 +030061
62static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080063isert_qp_event_callback(struct ib_event *e, void *context)
64{
65 struct isert_conn *isert_conn = (struct isert_conn *)context;
66
67 pr_err("isert_qp_event_callback event: %d\n", e->event);
68 switch (e->event) {
69 case IB_EVENT_COMM_EST:
70 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
71 break;
72 case IB_EVENT_QP_LAST_WQE_REACHED:
73 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
74 break;
75 default:
76 break;
77 }
78}
79
80static int
81isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
82{
83 int ret;
84
85 ret = ib_query_device(ib_dev, devattr);
86 if (ret) {
87 pr_err("ib_query_device() failed: %d\n", ret);
88 return ret;
89 }
90 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
91 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
92
93 return 0;
94}
95
96static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +020097isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
98 u8 protection)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080099{
100 struct isert_device *device = isert_conn->conn_device;
101 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800102 int ret, index, min_index = 0;
103
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104 mutex_lock(&device_list_mutex);
105 for (index = 0; index < device->cqs_used; index++)
106 if (device->cq_active_qps[index] <
107 device->cq_active_qps[min_index])
108 min_index = index;
109 device->cq_active_qps[min_index]++;
110 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
111 mutex_unlock(&device_list_mutex);
112
113 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
114 attr.event_handler = isert_qp_event_callback;
115 attr.qp_context = isert_conn;
116 attr.send_cq = device->dev_tx_cq[min_index];
117 attr.recv_cq = device->dev_rx_cq[min_index];
118 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
119 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
120 /*
121 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700122 * work-around for RDMA_READs with ConnectX-2.
123 *
124 * Also, still make sure to have at least two SGEs for
125 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800126 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700127 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800128 isert_conn->max_sge = attr.cap.max_send_sge;
129
130 attr.cap.max_recv_sge = 1;
131 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
132 attr.qp_type = IB_QPT_RC;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200133 if (protection)
134 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800135
136 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
137 cma_id->device);
138 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
139 isert_conn->conn_pd->device);
140
141 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
142 if (ret) {
143 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
144 return ret;
145 }
146 isert_conn->conn_qp = cma_id->qp;
147 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
148
149 return 0;
150}
151
152static void
153isert_cq_event_callback(struct ib_event *e, void *context)
154{
155 pr_debug("isert_cq_event_callback event: %d\n", e->event);
156}
157
158static int
159isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
160{
161 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
162 struct iser_rx_desc *rx_desc;
163 struct ib_sge *rx_sg;
164 u64 dma_addr;
165 int i, j;
166
167 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
168 sizeof(struct iser_rx_desc), GFP_KERNEL);
169 if (!isert_conn->conn_rx_descs)
170 goto fail;
171
172 rx_desc = isert_conn->conn_rx_descs;
173
174 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
175 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
176 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
177 if (ib_dma_mapping_error(ib_dev, dma_addr))
178 goto dma_map_fail;
179
180 rx_desc->dma_addr = dma_addr;
181
182 rx_sg = &rx_desc->rx_sg;
183 rx_sg->addr = rx_desc->dma_addr;
184 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
185 rx_sg->lkey = isert_conn->conn_mr->lkey;
186 }
187
188 isert_conn->conn_rx_desc_head = 0;
189 return 0;
190
191dma_map_fail:
192 rx_desc = isert_conn->conn_rx_descs;
193 for (j = 0; j < i; j++, rx_desc++) {
194 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
195 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
196 }
197 kfree(isert_conn->conn_rx_descs);
198 isert_conn->conn_rx_descs = NULL;
199fail:
200 return -ENOMEM;
201}
202
203static void
204isert_free_rx_descriptors(struct isert_conn *isert_conn)
205{
206 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
207 struct iser_rx_desc *rx_desc;
208 int i;
209
210 if (!isert_conn->conn_rx_descs)
211 return;
212
213 rx_desc = isert_conn->conn_rx_descs;
214 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
215 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
216 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
217 }
218
219 kfree(isert_conn->conn_rx_descs);
220 isert_conn->conn_rx_descs = NULL;
221}
222
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800223static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800224static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800225static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800226static void isert_cq_rx_callback(struct ib_cq *, void *);
227
228static int
229isert_create_device_ib_res(struct isert_device *device)
230{
231 struct ib_device *ib_dev = device->ib_device;
232 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300233 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800234 int ret = 0, i, j;
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000235 int max_rx_cqe, max_tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800236
Vu Pham59464ef2013-08-28 23:23:35 +0300237 dev_attr = &device->dev_attr;
238 ret = isert_query_device(ib_dev, dev_attr);
239 if (ret)
240 return ret;
241
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000242 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
243 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
244
Vu Phamd40945d2013-08-28 23:23:34 +0300245 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200246 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
247 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200248 device->use_fastreg = 1;
249 device->reg_rdma_mem = isert_reg_rdma;
250 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300251 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200252 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300253 device->reg_rdma_mem = isert_map_rdma;
254 device->unreg_rdma_mem = isert_unmap_cmd;
255 }
Vu Phamd40945d2013-08-28 23:23:34 +0300256
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200257 /* Check signature cap */
258 device->pi_capable = dev_attr->device_cap_flags &
259 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
260
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800261 device->cqs_used = min_t(int, num_online_cpus(),
262 device->ib_device->num_comp_vectors);
263 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200264 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200265 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800266 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200267 device->ib_device->num_comp_vectors, device->use_fastreg,
268 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800269 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
270 device->cqs_used, GFP_KERNEL);
271 if (!device->cq_desc) {
272 pr_err("Unable to allocate device->cq_desc\n");
273 return -ENOMEM;
274 }
275 cq_desc = device->cq_desc;
276
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277 for (i = 0; i < device->cqs_used; i++) {
278 cq_desc[i].device = device;
279 cq_desc[i].cq_index = i;
280
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800281 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800282 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
283 isert_cq_rx_callback,
284 isert_cq_event_callback,
285 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000286 max_rx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800287 if (IS_ERR(device->dev_rx_cq[i])) {
288 ret = PTR_ERR(device->dev_rx_cq[i]);
289 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800290 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800291 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800292
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800293 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800294 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
295 isert_cq_tx_callback,
296 isert_cq_event_callback,
297 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000298 max_tx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800299 if (IS_ERR(device->dev_tx_cq[i])) {
300 ret = PTR_ERR(device->dev_tx_cq[i]);
301 device->dev_tx_cq[i] = NULL;
302 goto out_cq;
303 }
304
305 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
306 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800307 goto out_cq;
308
Wei Yongjun94a71112013-10-29 09:56:34 +0800309 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
310 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800311 goto out_cq;
312 }
313
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800314 return 0;
315
316out_cq:
317 for (j = 0; j < i; j++) {
318 cq_desc = &device->cq_desc[j];
319
320 if (device->dev_rx_cq[j]) {
321 cancel_work_sync(&cq_desc->cq_rx_work);
322 ib_destroy_cq(device->dev_rx_cq[j]);
323 }
324 if (device->dev_tx_cq[j]) {
325 cancel_work_sync(&cq_desc->cq_tx_work);
326 ib_destroy_cq(device->dev_tx_cq[j]);
327 }
328 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800329 kfree(device->cq_desc);
330
331 return ret;
332}
333
334static void
335isert_free_device_ib_res(struct isert_device *device)
336{
337 struct isert_cq_desc *cq_desc;
338 int i;
339
340 for (i = 0; i < device->cqs_used; i++) {
341 cq_desc = &device->cq_desc[i];
342
343 cancel_work_sync(&cq_desc->cq_rx_work);
344 cancel_work_sync(&cq_desc->cq_tx_work);
345 ib_destroy_cq(device->dev_rx_cq[i]);
346 ib_destroy_cq(device->dev_tx_cq[i]);
347 device->dev_rx_cq[i] = NULL;
348 device->dev_tx_cq[i] = NULL;
349 }
350
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800351 kfree(device->cq_desc);
352}
353
354static void
355isert_device_try_release(struct isert_device *device)
356{
357 mutex_lock(&device_list_mutex);
358 device->refcount--;
359 if (!device->refcount) {
360 isert_free_device_ib_res(device);
361 list_del(&device->dev_node);
362 kfree(device);
363 }
364 mutex_unlock(&device_list_mutex);
365}
366
367static struct isert_device *
368isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
369{
370 struct isert_device *device;
371 int ret;
372
373 mutex_lock(&device_list_mutex);
374 list_for_each_entry(device, &device_list, dev_node) {
375 if (device->ib_device->node_guid == cma_id->device->node_guid) {
376 device->refcount++;
377 mutex_unlock(&device_list_mutex);
378 return device;
379 }
380 }
381
382 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
383 if (!device) {
384 mutex_unlock(&device_list_mutex);
385 return ERR_PTR(-ENOMEM);
386 }
387
388 INIT_LIST_HEAD(&device->dev_node);
389
390 device->ib_device = cma_id->device;
391 ret = isert_create_device_ib_res(device);
392 if (ret) {
393 kfree(device);
394 mutex_unlock(&device_list_mutex);
395 return ERR_PTR(ret);
396 }
397
398 device->refcount++;
399 list_add_tail(&device->dev_node, &device_list);
400 mutex_unlock(&device_list_mutex);
401
402 return device;
403}
404
Vu Pham59464ef2013-08-28 23:23:35 +0300405static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200406isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300407{
408 struct fast_reg_descriptor *fr_desc, *tmp;
409 int i = 0;
410
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200411 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300412 return;
413
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200414 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300415
416 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200417 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300418 list_del(&fr_desc->list);
419 ib_free_fast_reg_page_list(fr_desc->data_frpl);
420 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200421 if (fr_desc->pi_ctx) {
422 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
423 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
424 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
425 kfree(fr_desc->pi_ctx);
426 }
Vu Pham59464ef2013-08-28 23:23:35 +0300427 kfree(fr_desc);
428 ++i;
429 }
430
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200431 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300432 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200433 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300434}
435
436static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200437isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200438 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200439{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200440 int ret;
441
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200442 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
443 ISCSI_ISER_SG_TABLESIZE);
444 if (IS_ERR(fr_desc->data_frpl)) {
445 pr_err("Failed to allocate data frpl err=%ld\n",
446 PTR_ERR(fr_desc->data_frpl));
447 return PTR_ERR(fr_desc->data_frpl);
448 }
449
450 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
451 if (IS_ERR(fr_desc->data_mr)) {
452 pr_err("Failed to allocate data frmr err=%ld\n",
453 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200454 ret = PTR_ERR(fr_desc->data_mr);
455 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200456 }
457 pr_debug("Create fr_desc %p page_list %p\n",
458 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200459 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200460
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200461 if (protection) {
462 struct ib_mr_init_attr mr_init_attr = {0};
463 struct pi_context *pi_ctx;
464
465 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
466 if (!fr_desc->pi_ctx) {
467 pr_err("Failed to allocate pi context\n");
468 ret = -ENOMEM;
469 goto err_data_mr;
470 }
471 pi_ctx = fr_desc->pi_ctx;
472
473 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
474 ISCSI_ISER_SG_TABLESIZE);
475 if (IS_ERR(pi_ctx->prot_frpl)) {
476 pr_err("Failed to allocate prot frpl err=%ld\n",
477 PTR_ERR(pi_ctx->prot_frpl));
478 ret = PTR_ERR(pi_ctx->prot_frpl);
479 goto err_pi_ctx;
480 }
481
482 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
483 if (IS_ERR(pi_ctx->prot_mr)) {
484 pr_err("Failed to allocate prot frmr err=%ld\n",
485 PTR_ERR(pi_ctx->prot_mr));
486 ret = PTR_ERR(pi_ctx->prot_mr);
487 goto err_prot_frpl;
488 }
489 fr_desc->ind |= ISERT_PROT_KEY_VALID;
490
491 mr_init_attr.max_reg_descriptors = 2;
492 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
493 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
494 if (IS_ERR(pi_ctx->sig_mr)) {
495 pr_err("Failed to allocate signature enabled mr err=%ld\n",
496 PTR_ERR(pi_ctx->sig_mr));
497 ret = PTR_ERR(pi_ctx->sig_mr);
498 goto err_prot_mr;
499 }
500 fr_desc->ind |= ISERT_SIG_KEY_VALID;
501 }
502 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200503
504 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200505err_prot_mr:
506 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
507err_prot_frpl:
508 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
509err_pi_ctx:
510 kfree(fr_desc->pi_ctx);
511err_data_mr:
512 ib_dereg_mr(fr_desc->data_mr);
513err_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 Grimbergd3e125d2014-02-19 17:50:23 +0200520isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
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 Grimbergd3e125d2014-02-19 17:50:23 +0200544 isert_conn->conn_pd, fr_desc,
545 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200546 if (ret) {
547 pr_err("Failed to create fastreg descriptor err=%d\n",
548 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800549 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300550 goto err;
551 }
552
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200553 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
554 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300555 }
556
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200557 pr_debug("Creating conn %p fastreg pool size=%d",
558 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300559
560 return 0;
561
562err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200563 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300564 return ret;
565}
566
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800567static int
568isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
569{
570 struct iscsi_np *np = cma_id->context;
571 struct isert_np *isert_np = np->np_context;
572 struct isert_conn *isert_conn;
573 struct isert_device *device;
574 struct ib_device *ib_dev = cma_id->device;
575 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300576 u8 pi_support;
577
578 spin_lock_bh(&np->np_thread_lock);
579 if (!np->enabled) {
580 spin_unlock_bh(&np->np_thread_lock);
581 pr_debug("iscsi_np is not enabled, reject connect request\n");
582 return rdma_reject(cma_id, NULL, 0);
583 }
584 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800585
586 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
587 cma_id, cma_id->context);
588
589 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
590 if (!isert_conn) {
591 pr_err("Unable to allocate isert_conn\n");
592 return -ENOMEM;
593 }
594 isert_conn->state = ISER_CONN_INIT;
595 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
596 init_completion(&isert_conn->conn_login_comp);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200597 init_completion(&isert_conn->login_req_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800598 init_completion(&isert_conn->conn_wait);
599 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800600 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700601 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300602 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700603 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800604
605 cma_id->context = isert_conn;
606 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800607
608 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
609 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
610 if (!isert_conn->login_buf) {
611 pr_err("Unable to allocate isert_conn->login_buf\n");
612 ret = -ENOMEM;
613 goto out;
614 }
615
616 isert_conn->login_req_buf = isert_conn->login_buf;
617 isert_conn->login_rsp_buf = isert_conn->login_buf +
618 ISCSI_DEF_MAX_RECV_SEG_LEN;
619 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
620 isert_conn->login_buf, isert_conn->login_req_buf,
621 isert_conn->login_rsp_buf);
622
623 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
624 (void *)isert_conn->login_req_buf,
625 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
626
627 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
628 if (ret) {
629 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
630 ret);
631 isert_conn->login_req_dma = 0;
632 goto out_login_buf;
633 }
634
635 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
636 (void *)isert_conn->login_rsp_buf,
637 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
638
639 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
640 if (ret) {
641 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
642 ret);
643 isert_conn->login_rsp_dma = 0;
644 goto out_req_dma_map;
645 }
646
647 device = isert_device_find_by_ib_dev(cma_id);
648 if (IS_ERR(device)) {
649 ret = PTR_ERR(device);
650 goto out_rsp_dma_map;
651 }
652
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300653 /* Set max inflight RDMA READ requests */
654 isert_conn->initiator_depth = min_t(u8,
655 event->param.conn.initiator_depth,
656 device->dev_attr.max_qp_init_rd_atom);
657 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
658
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800659 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200660 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
661 if (IS_ERR(isert_conn->conn_pd)) {
662 ret = PTR_ERR(isert_conn->conn_pd);
663 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
664 isert_conn, ret);
665 goto out_pd;
666 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800667
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200668 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
669 IB_ACCESS_LOCAL_WRITE);
670 if (IS_ERR(isert_conn->conn_mr)) {
671 ret = PTR_ERR(isert_conn->conn_mr);
672 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
673 isert_conn, ret);
674 goto out_mr;
675 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800676
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300677 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200678 if (pi_support && !device->pi_capable) {
Sagi Grimberg5f80ff82014-05-20 13:28:11 +0300679 pr_err("Protection information requested but not supported, "
680 "rejecting connect request\n");
681 ret = rdma_reject(cma_id, NULL, 0);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200682 goto out_mr;
683 }
684
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200685 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 if (ret)
687 goto out_conn_dev;
688
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200689 ret = isert_rdma_post_recvl(isert_conn);
690 if (ret)
691 goto out_conn_dev;
692
693 ret = isert_rdma_accept(isert_conn);
694 if (ret)
695 goto out_conn_dev;
696
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800697 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300698 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800699 mutex_unlock(&isert_np->np_accept_mutex);
700
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300701 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
702 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800703 return 0;
704
705out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200706 ib_dereg_mr(isert_conn->conn_mr);
707out_mr:
708 ib_dealloc_pd(isert_conn->conn_pd);
709out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800710 isert_device_try_release(device);
711out_rsp_dma_map:
712 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
713 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
714out_req_dma_map:
715 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
716 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
717out_login_buf:
718 kfree(isert_conn->login_buf);
719out:
720 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200721 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800722 return ret;
723}
724
725static void
726isert_connect_release(struct isert_conn *isert_conn)
727{
728 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
729 struct isert_device *device = isert_conn->conn_device;
730 int cq_index;
731
732 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
733
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200734 if (device && device->use_fastreg)
735 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300736
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737 if (isert_conn->conn_qp) {
738 cq_index = ((struct isert_cq_desc *)
739 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
740 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
741 isert_conn->conn_device->cq_active_qps[cq_index]--;
742
743 rdma_destroy_qp(isert_conn->conn_cm_id);
744 }
745
746 isert_free_rx_descriptors(isert_conn);
747 rdma_destroy_id(isert_conn->conn_cm_id);
748
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200749 ib_dereg_mr(isert_conn->conn_mr);
750 ib_dealloc_pd(isert_conn->conn_pd);
751
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800752 if (isert_conn->login_buf) {
753 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
754 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
755 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
756 ISCSI_DEF_MAX_RECV_SEG_LEN,
757 DMA_FROM_DEVICE);
758 kfree(isert_conn->login_buf);
759 }
760 kfree(isert_conn);
761
762 if (device)
763 isert_device_try_release(device);
764
765 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
766}
767
768static void
769isert_connected_handler(struct rdma_cm_id *cma_id)
770{
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300771 struct isert_conn *isert_conn = cma_id->context;
772
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200773 pr_info("conn %p\n", isert_conn);
774
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200775 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
776 pr_warn("conn %p connect_release is running\n", isert_conn);
777 return;
778 }
779
780 mutex_lock(&isert_conn->conn_mutex);
781 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
782 isert_conn->state = ISER_CONN_UP;
783 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800784}
785
786static void
787isert_release_conn_kref(struct kref *kref)
788{
789 struct isert_conn *isert_conn = container_of(kref,
790 struct isert_conn, conn_kref);
791
792 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
793 current->comm, current->pid);
794
795 isert_connect_release(isert_conn);
796}
797
798static void
799isert_put_conn(struct isert_conn *isert_conn)
800{
801 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
802}
803
Sagi Grimberg954f2372014-12-02 16:57:17 +0200804/**
805 * isert_conn_terminate() - Initiate connection termination
806 * @isert_conn: isert connection struct
807 *
808 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200809 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200810 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200811 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200812 *
813 * This routine must be called with conn_mutex held. Thus it is
814 * safe to call multiple times.
815 */
816static void
817isert_conn_terminate(struct isert_conn *isert_conn)
818{
819 int err;
820
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200821 switch (isert_conn->state) {
822 case ISER_CONN_TERMINATING:
823 break;
824 case ISER_CONN_UP:
825 /*
826 * No flush completions will occur as we didn't
827 * get to ISER_CONN_FULL_FEATURE yet, complete
828 * to allow teardown progress.
829 */
830 complete(&isert_conn->conn_wait_comp_err);
831 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200832 pr_info("Terminating conn %p state %d\n",
833 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200834 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200835 err = rdma_disconnect(isert_conn->conn_cm_id);
836 if (err)
837 pr_warn("Failed rdma_disconnect isert_conn %p\n",
838 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200839 break;
840 default:
841 pr_warn("conn %p teminating in state %d\n",
842 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200843 }
844}
845
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700846static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200847isert_disconnected_handler(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800848{
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700849 struct isert_conn *isert_conn;
850
851 if (!cma_id->qp) {
852 struct isert_np *isert_np = cma_id->context;
853
854 isert_np->np_cm_id = NULL;
855 return -1;
856 }
857
858 isert_conn = (struct isert_conn *)cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800859
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200860 mutex_lock(&isert_conn->conn_mutex);
861 isert_conn_terminate(isert_conn);
862 mutex_unlock(&isert_conn->conn_mutex);
863
864 pr_info("conn %p completing conn_wait\n", isert_conn);
865 complete(&isert_conn->conn_wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700866
867 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800868}
869
Sagi Grimberg954f2372014-12-02 16:57:17 +0200870static void
871isert_connect_error(struct rdma_cm_id *cma_id)
872{
873 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
874
875 isert_put_conn(isert_conn);
876}
877
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800878static int
879isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
880{
881 int ret = 0;
882
883 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
884 event->event, event->status, cma_id->context, cma_id);
885
886 switch (event->event) {
887 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800888 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700889 if (ret)
890 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
891 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800892 break;
893 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800894 isert_connected_handler(cma_id);
895 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300896 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
897 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
898 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300899 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200900 ret = isert_disconnected_handler(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200902 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
903 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800904 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg954f2372014-12-02 16:57:17 +0200905 isert_connect_error(cma_id);
906 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800907 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300908 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800909 break;
910 }
911
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800912 return ret;
913}
914
915static int
916isert_post_recv(struct isert_conn *isert_conn, u32 count)
917{
918 struct ib_recv_wr *rx_wr, *rx_wr_failed;
919 int i, ret;
920 unsigned int rx_head = isert_conn->conn_rx_desc_head;
921 struct iser_rx_desc *rx_desc;
922
923 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
924 rx_desc = &isert_conn->conn_rx_descs[rx_head];
925 rx_wr->wr_id = (unsigned long)rx_desc;
926 rx_wr->sg_list = &rx_desc->rx_sg;
927 rx_wr->num_sge = 1;
928 rx_wr->next = rx_wr + 1;
929 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
930 }
931
932 rx_wr--;
933 rx_wr->next = NULL; /* mark end of work requests list */
934
935 isert_conn->post_recv_buf_count += count;
936 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
937 &rx_wr_failed);
938 if (ret) {
939 pr_err("ib_post_recv() failed with ret: %d\n", ret);
940 isert_conn->post_recv_buf_count -= count;
941 } else {
942 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
943 isert_conn->conn_rx_desc_head = rx_head;
944 }
945 return ret;
946}
947
948static int
949isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
950{
951 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
952 struct ib_send_wr send_wr, *send_wr_failed;
953 int ret;
954
955 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
956 ISER_HEADERS_LEN, DMA_TO_DEVICE);
957
958 send_wr.next = NULL;
959 send_wr.wr_id = (unsigned long)tx_desc;
960 send_wr.sg_list = tx_desc->tx_sg;
961 send_wr.num_sge = tx_desc->num_sge;
962 send_wr.opcode = IB_WR_SEND;
963 send_wr.send_flags = IB_SEND_SIGNALED;
964
965 atomic_inc(&isert_conn->post_send_buf_count);
966
967 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
968 if (ret) {
969 pr_err("ib_post_send() failed, ret: %d\n", ret);
970 atomic_dec(&isert_conn->post_send_buf_count);
971 }
972
973 return ret;
974}
975
976static void
977isert_create_send_desc(struct isert_conn *isert_conn,
978 struct isert_cmd *isert_cmd,
979 struct iser_tx_desc *tx_desc)
980{
981 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
982
983 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
984 ISER_HEADERS_LEN, DMA_TO_DEVICE);
985
986 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
987 tx_desc->iser_header.flags = ISER_VER;
988
989 tx_desc->num_sge = 1;
990 tx_desc->isert_cmd = isert_cmd;
991
992 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
993 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
994 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
995 }
996}
997
998static int
999isert_init_tx_hdrs(struct isert_conn *isert_conn,
1000 struct iser_tx_desc *tx_desc)
1001{
1002 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1003 u64 dma_addr;
1004
1005 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1006 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1007 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1008 pr_err("ib_dma_mapping_error() failed\n");
1009 return -ENOMEM;
1010 }
1011
1012 tx_desc->dma_addr = dma_addr;
1013 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1014 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1015 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1016
1017 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1018 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1019 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1020
1021 return 0;
1022}
1023
1024static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001025isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1026 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001027{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001028 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1029
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001030 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1031 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1032 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001033 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001034 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001035 /*
1036 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
1037 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
1038 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001039 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001040 if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001041 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001042 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001043 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001044 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001045 return;
1046 }
1047 isert_conn->conn_comp_batch = 0;
1048 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001049 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001050
1051 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001052}
1053
1054static int
1055isert_rdma_post_recvl(struct isert_conn *isert_conn)
1056{
1057 struct ib_recv_wr rx_wr, *rx_wr_fail;
1058 struct ib_sge sge;
1059 int ret;
1060
1061 memset(&sge, 0, sizeof(struct ib_sge));
1062 sge.addr = isert_conn->login_req_dma;
1063 sge.length = ISER_RX_LOGIN_SIZE;
1064 sge.lkey = isert_conn->conn_mr->lkey;
1065
1066 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1067 sge.addr, sge.length, sge.lkey);
1068
1069 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1070 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1071 rx_wr.sg_list = &sge;
1072 rx_wr.num_sge = 1;
1073
1074 isert_conn->post_recv_buf_count++;
1075 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1076 if (ret) {
1077 pr_err("ib_post_recv() failed: %d\n", ret);
1078 isert_conn->post_recv_buf_count--;
1079 }
1080
1081 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1082 return ret;
1083}
1084
1085static int
1086isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1087 u32 length)
1088{
1089 struct isert_conn *isert_conn = conn->context;
1090 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1091 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1092 int ret;
1093
1094 isert_create_send_desc(isert_conn, NULL, tx_desc);
1095
1096 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1097 sizeof(struct iscsi_hdr));
1098
1099 isert_init_tx_hdrs(isert_conn, tx_desc);
1100
1101 if (length > 0) {
1102 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1103
1104 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1105 length, DMA_TO_DEVICE);
1106
1107 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1108
1109 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1110 length, DMA_TO_DEVICE);
1111
1112 tx_dsg->addr = isert_conn->login_rsp_dma;
1113 tx_dsg->length = length;
1114 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1115 tx_desc->num_sge = 2;
1116 }
1117 if (!login->login_failed) {
1118 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001119 if (!conn->sess->sess_ops->SessionType &&
1120 isert_conn->conn_device->use_fastreg) {
1121 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001122 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1123
1124 ret = isert_conn_create_fastreg_pool(isert_conn,
1125 pi_support);
1126 if (ret) {
1127 pr_err("Conn: %p failed to create"
1128 " fastreg pool\n", isert_conn);
1129 return ret;
1130 }
1131 }
1132
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001133 ret = isert_alloc_rx_descriptors(isert_conn);
1134 if (ret)
1135 return ret;
1136
1137 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1138 if (ret)
1139 return ret;
1140
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001141 /* Now we are in FULL_FEATURE phase */
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001142 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001143 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001144 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001145 goto post_send;
1146 }
1147
1148 ret = isert_rdma_post_recvl(isert_conn);
1149 if (ret)
1150 return ret;
1151 }
1152post_send:
1153 ret = isert_post_send(isert_conn, tx_desc);
1154 if (ret)
1155 return ret;
1156
1157 return 0;
1158}
1159
1160static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001161isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001162{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001163 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1164 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001165 struct iscsi_conn *conn = isert_conn->conn;
1166 struct iscsi_login *login = conn->conn_login;
1167 int size;
1168
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001169 pr_info("conn %p\n", isert_conn);
1170
1171 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001172
1173 if (login->first_request) {
1174 struct iscsi_login_req *login_req =
1175 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1176 /*
1177 * Setup the initial iscsi_login values from the leading
1178 * login request PDU.
1179 */
1180 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1181 login->current_stage =
1182 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1183 >> 2;
1184 login->version_min = login_req->min_version;
1185 login->version_max = login_req->max_version;
1186 memcpy(login->isid, login_req->isid, 6);
1187 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1188 login->init_task_tag = login_req->itt;
1189 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1190 login->cid = be16_to_cpu(login_req->cid);
1191 login->tsih = be16_to_cpu(login_req->tsih);
1192 }
1193
1194 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1195
1196 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1197 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1198 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1199 memcpy(login->req_buf, &rx_desc->data[0], size);
1200
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001201 if (login->first_request) {
1202 complete(&isert_conn->conn_login_comp);
1203 return;
1204 }
1205 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001206}
1207
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001208static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001209*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001210{
1211 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1212 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001213 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001214
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001215 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001216 if (!cmd) {
1217 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001218 return NULL;
1219 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001220 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001221 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001222 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001223
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001224 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001225}
1226
1227static int
1228isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001229 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1230 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001231{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001232 struct iscsi_conn *conn = isert_conn->conn;
1233 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1234 struct scatterlist *sg;
1235 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1236 bool dump_payload = false;
1237
1238 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1239 if (rc < 0)
1240 return rc;
1241
1242 imm_data = cmd->immediate_data;
1243 imm_data_len = cmd->first_burst_len;
1244 unsol_data = cmd->unsolicited_data;
1245
1246 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1247 if (rc < 0) {
1248 return 0;
1249 } else if (rc > 0) {
1250 dump_payload = true;
1251 goto sequence_cmd;
1252 }
1253
1254 if (!imm_data)
1255 return 0;
1256
1257 sg = &cmd->se_cmd.t_data_sg[0];
1258 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1259
1260 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1261 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1262
1263 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1264
1265 cmd->write_data_done += imm_data_len;
1266
1267 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1268 spin_lock_bh(&cmd->istate_lock);
1269 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1270 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1271 spin_unlock_bh(&cmd->istate_lock);
1272 }
1273
1274sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001275 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001276
1277 if (!rc && dump_payload == false && unsol_data)
1278 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001279 else if (dump_payload && imm_data)
1280 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001281
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282 return 0;
1283}
1284
1285static int
1286isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1287 struct iser_rx_desc *rx_desc, unsigned char *buf)
1288{
1289 struct scatterlist *sg_start;
1290 struct iscsi_conn *conn = isert_conn->conn;
1291 struct iscsi_cmd *cmd = NULL;
1292 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1293 u32 unsol_data_len = ntoh24(hdr->dlength);
1294 int rc, sg_nents, sg_off, page_off;
1295
1296 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1297 if (rc < 0)
1298 return rc;
1299 else if (!cmd)
1300 return 0;
1301 /*
1302 * FIXME: Unexpected unsolicited_data out
1303 */
1304 if (!cmd->unsolicited_data) {
1305 pr_err("Received unexpected solicited data payload\n");
1306 dump_stack();
1307 return -1;
1308 }
1309
1310 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1311 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1312
1313 sg_off = cmd->write_data_done / PAGE_SIZE;
1314 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1315 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1316 page_off = cmd->write_data_done % PAGE_SIZE;
1317 /*
1318 * FIXME: Non page-aligned unsolicited_data out
1319 */
1320 if (page_off) {
1321 pr_err("Received unexpected non-page aligned data payload\n");
1322 dump_stack();
1323 return -1;
1324 }
1325 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1326 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1327
1328 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1329 unsol_data_len);
1330
1331 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1332 if (rc < 0)
1333 return rc;
1334
1335 return 0;
1336}
1337
1338static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001339isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001340 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1341 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001342{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001343 struct iscsi_conn *conn = isert_conn->conn;
1344 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1345 int rc;
1346
1347 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1348 if (rc < 0)
1349 return rc;
1350 /*
1351 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1352 */
1353
1354 return iscsit_process_nop_out(conn, cmd, hdr);
1355}
1356
1357static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001358isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001359 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1360 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001361{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001362 struct iscsi_conn *conn = isert_conn->conn;
1363 u32 payload_length = ntoh24(hdr->dlength);
1364 int rc;
1365 unsigned char *text_in;
1366
1367 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1368 if (rc < 0)
1369 return rc;
1370
1371 text_in = kzalloc(payload_length, GFP_KERNEL);
1372 if (!text_in) {
1373 pr_err("Unable to allocate text_in of payload_length: %u\n",
1374 payload_length);
1375 return -ENOMEM;
1376 }
1377 cmd->text_in_ptr = text_in;
1378
1379 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1380
1381 return iscsit_process_text_cmd(conn, cmd, hdr);
1382}
1383
1384static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001385isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1386 uint32_t read_stag, uint64_t read_va,
1387 uint32_t write_stag, uint64_t write_va)
1388{
1389 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1390 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001391 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001392 struct iscsi_cmd *cmd;
1393 struct isert_cmd *isert_cmd;
1394 int ret = -EINVAL;
1395 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1396
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001397 if (sess->sess_ops->SessionType &&
1398 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1399 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1400 " ignoring\n", opcode);
1401 return 0;
1402 }
1403
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001404 switch (opcode) {
1405 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001406 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001407 if (!cmd)
1408 break;
1409
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001410 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001411 isert_cmd->read_stag = read_stag;
1412 isert_cmd->read_va = read_va;
1413 isert_cmd->write_stag = write_stag;
1414 isert_cmd->write_va = write_va;
1415
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001416 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001417 rx_desc, (unsigned char *)hdr);
1418 break;
1419 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001420 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001421 if (!cmd)
1422 break;
1423
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001424 isert_cmd = iscsit_priv_cmd(cmd);
1425 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001426 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001427 break;
1428 case ISCSI_OP_SCSI_DATA_OUT:
1429 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1430 (unsigned char *)hdr);
1431 break;
1432 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001433 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001434 if (!cmd)
1435 break;
1436
1437 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1438 (unsigned char *)hdr);
1439 break;
1440 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001441 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001442 if (!cmd)
1443 break;
1444
1445 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1446 if (ret > 0)
1447 wait_for_completion_timeout(&conn->conn_logout_comp,
1448 SECONDS_FOR_LOGOUT_COMP *
1449 HZ);
1450 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001451 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001452 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001453 if (!cmd)
1454 break;
1455
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001456 isert_cmd = iscsit_priv_cmd(cmd);
1457 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001458 rx_desc, (struct iscsi_text *)hdr);
1459 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001460 default:
1461 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1462 dump_stack();
1463 break;
1464 }
1465
1466 return ret;
1467}
1468
1469static void
1470isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1471{
1472 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1473 uint64_t read_va = 0, write_va = 0;
1474 uint32_t read_stag = 0, write_stag = 0;
1475 int rc;
1476
1477 switch (iser_hdr->flags & 0xF0) {
1478 case ISCSI_CTRL:
1479 if (iser_hdr->flags & ISER_RSV) {
1480 read_stag = be32_to_cpu(iser_hdr->read_stag);
1481 read_va = be64_to_cpu(iser_hdr->read_va);
1482 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1483 read_stag, (unsigned long long)read_va);
1484 }
1485 if (iser_hdr->flags & ISER_WSV) {
1486 write_stag = be32_to_cpu(iser_hdr->write_stag);
1487 write_va = be64_to_cpu(iser_hdr->write_va);
1488 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1489 write_stag, (unsigned long long)write_va);
1490 }
1491
1492 pr_debug("ISER ISCSI_CTRL PDU\n");
1493 break;
1494 case ISER_HELLO:
1495 pr_err("iSER Hello message\n");
1496 break;
1497 default:
1498 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1499 break;
1500 }
1501
1502 rc = isert_rx_opcode(isert_conn, rx_desc,
1503 read_stag, read_va, write_stag, write_va);
1504}
1505
1506static void
1507isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1508 unsigned long xfer_len)
1509{
1510 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1511 struct iscsi_hdr *hdr;
1512 u64 rx_dma;
1513 int rx_buflen, outstanding;
1514
1515 if ((char *)desc == isert_conn->login_req_buf) {
1516 rx_dma = isert_conn->login_req_dma;
1517 rx_buflen = ISER_RX_LOGIN_SIZE;
1518 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1519 rx_dma, rx_buflen);
1520 } else {
1521 rx_dma = desc->dma_addr;
1522 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1523 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1524 rx_dma, rx_buflen);
1525 }
1526
1527 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1528
1529 hdr = &desc->iscsi_header;
1530 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1531 hdr->opcode, hdr->itt, hdr->flags,
1532 (int)(xfer_len - ISER_HEADERS_LEN));
1533
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001534 if ((char *)desc == isert_conn->login_req_buf) {
1535 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1536 if (isert_conn->conn) {
1537 struct iscsi_login *login = isert_conn->conn->conn_login;
1538
1539 if (login && !login->first_request)
1540 isert_rx_login_req(isert_conn);
1541 }
1542 mutex_lock(&isert_conn->conn_mutex);
1543 complete(&isert_conn->login_req_comp);
1544 mutex_unlock(&isert_conn->conn_mutex);
1545 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001546 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001547 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001548
1549 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1550 DMA_FROM_DEVICE);
1551
1552 isert_conn->post_recv_buf_count--;
1553 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1554 isert_conn->post_recv_buf_count);
1555
1556 if ((char *)desc == isert_conn->login_req_buf)
1557 return;
1558
1559 outstanding = isert_conn->post_recv_buf_count;
1560 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1561 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1562 ISERT_MIN_POSTED_RX);
1563 err = isert_post_recv(isert_conn, count);
1564 if (err) {
1565 pr_err("isert_post_recv() count: %d failed, %d\n",
1566 count, err);
1567 }
1568 }
1569}
1570
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001571static int
1572isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1573 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1574 enum iser_ib_op_code op, struct isert_data_buf *data)
1575{
1576 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1577
1578 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1579 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1580
1581 data->len = length - offset;
1582 data->offset = offset;
1583 data->sg_off = data->offset / PAGE_SIZE;
1584
1585 data->sg = &sg[data->sg_off];
1586 data->nents = min_t(unsigned int, nents - data->sg_off,
1587 ISCSI_ISER_SG_TABLESIZE);
1588 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1589 PAGE_SIZE);
1590
1591 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1592 data->dma_dir);
1593 if (unlikely(!data->dma_nents)) {
1594 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1595 return -EINVAL;
1596 }
1597
1598 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1599 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1600
1601 return 0;
1602}
1603
1604static void
1605isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1606{
1607 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1608
1609 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1610 memset(data, 0, sizeof(*data));
1611}
1612
1613
1614
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001615static void
1616isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1617{
1618 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001619
Vu Pham90ecc6e2013-08-28 23:23:33 +03001620 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001621
1622 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001623 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001624 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001625 }
1626
Vu Pham90ecc6e2013-08-28 23:23:33 +03001627 if (wr->send_wr) {
1628 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1629 kfree(wr->send_wr);
1630 wr->send_wr = NULL;
1631 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001632
Vu Pham90ecc6e2013-08-28 23:23:33 +03001633 if (wr->ib_sge) {
1634 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1635 kfree(wr->ib_sge);
1636 wr->ib_sge = NULL;
1637 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638}
1639
1640static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001641isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001642{
1643 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001644 LIST_HEAD(unmap_list);
1645
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001646 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001647
1648 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001649 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001650 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001651 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1652 isert_unmap_data_buf(isert_conn, &wr->prot);
1653 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1654 }
Vu Pham59464ef2013-08-28 23:23:35 +03001655 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001656 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001657 spin_unlock_bh(&isert_conn->conn_lock);
1658 wr->fr_desc = NULL;
1659 }
1660
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001661 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001662 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001663 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001664 }
1665
1666 wr->ib_sge = NULL;
1667 wr->send_wr = NULL;
1668}
1669
1670static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001671isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001672{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001673 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001674 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001675 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001676 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001677
1678 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1679
1680 switch (cmd->iscsi_opcode) {
1681 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001682 spin_lock_bh(&conn->cmd_lock);
1683 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001684 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001685 spin_unlock_bh(&conn->cmd_lock);
1686
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001687 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001688 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001689 /*
1690 * Check for special case during comp_err where
1691 * WRITE_PENDING has been handed off from core,
1692 * but requires an extra target_put_sess_cmd()
1693 * before transport_generic_free_cmd() below.
1694 */
1695 if (comp_err &&
1696 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1697 struct se_cmd *se_cmd = &cmd->se_cmd;
1698
1699 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1700 }
1701 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702
Vu Phamd40945d2013-08-28 23:23:34 +03001703 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001704 transport_generic_free_cmd(&cmd->se_cmd, 0);
1705 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001707 spin_lock_bh(&conn->cmd_lock);
1708 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001709 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001710 spin_unlock_bh(&conn->cmd_lock);
1711
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001712 transport_generic_free_cmd(&cmd->se_cmd, 0);
1713 break;
1714 case ISCSI_OP_REJECT:
1715 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001716 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001717 spin_lock_bh(&conn->cmd_lock);
1718 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001719 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001720 spin_unlock_bh(&conn->cmd_lock);
1721
1722 /*
1723 * Handle special case for REJECT when iscsi_add_reject*() has
1724 * overwritten the original iscsi_opcode assignment, and the
1725 * associated cmd->se_cmd needs to be released.
1726 */
1727 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001728 pr_debug("Calling transport_generic_free_cmd from"
1729 " isert_put_cmd for 0x%02x\n",
1730 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001731 transport_generic_free_cmd(&cmd->se_cmd, 0);
1732 break;
1733 }
1734 /*
1735 * Fall-through
1736 */
1737 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001738 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739 break;
1740 }
1741}
1742
1743static void
1744isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1745{
1746 if (tx_desc->dma_addr != 0) {
1747 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1748 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1749 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1750 tx_desc->dma_addr = 0;
1751 }
1752}
1753
1754static void
1755isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001756 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001757{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001758 if (isert_cmd->pdu_buf_dma != 0) {
1759 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1760 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1761 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1762 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001763 }
1764
1765 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001766 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001767}
1768
Sagi Grimberg96b79732014-03-17 12:52:18 +02001769static int
1770isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1771{
1772 struct ib_mr_status mr_status;
1773 int ret;
1774
1775 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1776 if (ret) {
1777 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1778 goto fail_mr_status;
1779 }
1780
1781 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1782 u64 sec_offset_err;
1783 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1784
1785 switch (mr_status.sig_err.err_type) {
1786 case IB_SIG_BAD_GUARD:
1787 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1788 break;
1789 case IB_SIG_BAD_REFTAG:
1790 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1791 break;
1792 case IB_SIG_BAD_APPTAG:
1793 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1794 break;
1795 }
1796 sec_offset_err = mr_status.sig_err.sig_err_offset;
1797 do_div(sec_offset_err, block_size);
1798 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1799
1800 pr_err("isert: PI error found type %d at sector 0x%llx "
1801 "expected 0x%x vs actual 0x%x\n",
1802 mr_status.sig_err.err_type,
1803 (unsigned long long)se_cmd->bad_sector,
1804 mr_status.sig_err.expected,
1805 mr_status.sig_err.actual);
1806 ret = 1;
1807 }
1808
1809fail_mr_status:
1810 return ret;
1811}
1812
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001813static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001814isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1815 struct isert_cmd *isert_cmd)
1816{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001817 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001818 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001819 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001820 struct isert_conn *isert_conn = isert_cmd->conn;
1821 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001822 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001823
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001824 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001825 ret = isert_check_pi_status(se_cmd,
1826 wr->fr_desc->pi_ctx->sig_mr);
1827 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001828 }
1829
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001830 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001831 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001832 if (ret)
1833 transport_send_check_condition_and_sense(se_cmd,
1834 se_cmd->pi_err, 0);
1835 else
1836 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001837}
1838
1839static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001840isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1841 struct isert_cmd *isert_cmd)
1842{
1843 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001844 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001845 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001846 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001847 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001848 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001849
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001850 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001851 ret = isert_check_pi_status(se_cmd,
1852 wr->fr_desc->pi_ctx->sig_mr);
1853 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001854 }
1855
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001856 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001857 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001858 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001859 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001860
Vu Pham90ecc6e2013-08-28 23:23:33 +03001861 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001862 spin_lock_bh(&cmd->istate_lock);
1863 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1864 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1865 spin_unlock_bh(&cmd->istate_lock);
1866
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001867 if (ret)
1868 transport_send_check_condition_and_sense(se_cmd,
1869 se_cmd->pi_err, 0);
1870 else
1871 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001872}
1873
1874static void
1875isert_do_control_comp(struct work_struct *work)
1876{
1877 struct isert_cmd *isert_cmd = container_of(work,
1878 struct isert_cmd, comp_work);
1879 struct isert_conn *isert_conn = isert_cmd->conn;
1880 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001881 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001882
1883 switch (cmd->i_state) {
1884 case ISTATE_SEND_TASKMGTRSP:
1885 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1886
1887 atomic_dec(&isert_conn->post_send_buf_count);
1888 iscsit_tmr_post_handler(cmd, cmd->conn);
1889
1890 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001891 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001892 break;
1893 case ISTATE_SEND_REJECT:
1894 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1895 atomic_dec(&isert_conn->post_send_buf_count);
1896
1897 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001898 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001899 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001900 case ISTATE_SEND_LOGOUTRSP:
1901 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001902
1903 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001904 iscsit_logout_post_handler(cmd, cmd->conn);
1905 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001906 case ISTATE_SEND_TEXTRSP:
1907 atomic_dec(&isert_conn->post_send_buf_count);
1908 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001909 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001910 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001911 default:
1912 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1913 dump_stack();
1914 break;
1915 }
1916}
1917
1918static void
1919isert_response_completion(struct iser_tx_desc *tx_desc,
1920 struct isert_cmd *isert_cmd,
1921 struct isert_conn *isert_conn,
1922 struct ib_device *ib_dev)
1923{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001924 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001925 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001926
1927 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001928 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001929 cmd->i_state == ISTATE_SEND_REJECT ||
1930 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001931 isert_unmap_tx_desc(tx_desc, ib_dev);
1932
1933 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1934 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1935 return;
1936 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001937
1938 /**
1939 * If send_wr_num is 0 this means that we got
1940 * RDMA completion and we cleared it and we should
1941 * simply decrement the response post. else the
1942 * response is incorporated in send_wr_num, just
1943 * sub it.
1944 **/
1945 if (wr->send_wr_num)
1946 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1947 else
1948 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001949
1950 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001951 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001952}
1953
1954static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001955__isert_send_completion(struct iser_tx_desc *tx_desc,
1956 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001957{
1958 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1959 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1960 struct isert_rdma_wr *wr;
1961
1962 if (!isert_cmd) {
1963 atomic_dec(&isert_conn->post_send_buf_count);
1964 isert_unmap_tx_desc(tx_desc, ib_dev);
1965 return;
1966 }
1967 wr = &isert_cmd->rdma_wr;
1968
1969 switch (wr->iser_ib_op) {
1970 case ISER_IB_RECV:
1971 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1972 dump_stack();
1973 break;
1974 case ISER_IB_SEND:
1975 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1976 isert_response_completion(tx_desc, isert_cmd,
1977 isert_conn, ib_dev);
1978 break;
1979 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001980 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001981 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001982 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001983 break;
1984 case ISER_IB_RDMA_READ:
1985 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1986
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001987 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001988 isert_completion_rdma_read(tx_desc, isert_cmd);
1989 break;
1990 default:
1991 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1992 dump_stack();
1993 break;
1994 }
1995}
1996
1997static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001998isert_send_completion(struct iser_tx_desc *tx_desc,
1999 struct isert_conn *isert_conn)
2000{
2001 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2002 struct iser_tx_desc *t;
2003 /*
2004 * Drain coalesced completion llist starting from comp_llnode_batch
2005 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
2006 */
2007 while (llnode) {
2008 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2009 llnode = llist_next(llnode);
2010 __isert_send_completion(t, isert_conn);
2011 }
2012 __isert_send_completion(tx_desc, isert_conn);
2013}
2014
2015static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002016isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
2017{
2018 struct llist_node *llnode;
2019 struct isert_rdma_wr *wr;
2020 struct iser_tx_desc *t;
2021
2022 mutex_lock(&isert_conn->conn_mutex);
2023 llnode = llist_del_all(&isert_conn->conn_comp_llist);
2024 isert_conn->conn_comp_batch = 0;
2025 mutex_unlock(&isert_conn->conn_mutex);
2026
2027 while (llnode) {
2028 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2029 llnode = llist_next(llnode);
2030 wr = &t->isert_cmd->rdma_wr;
2031
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002032 /**
2033 * If send_wr_num is 0 this means that we got
2034 * RDMA completion and we cleared it and we should
2035 * simply decrement the response post. else the
2036 * response is incorporated in send_wr_num, just
2037 * sub it.
2038 **/
2039 if (wr->send_wr_num)
2040 atomic_sub(wr->send_wr_num,
2041 &isert_conn->post_send_buf_count);
2042 else
2043 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002044
2045 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002046 }
2047}
2048
2049static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002050isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002051{
2052 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002053 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002054 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2055 struct isert_rdma_wr *wr;
2056 struct iser_tx_desc *t;
2057
2058 while (llnode) {
2059 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2060 llnode = llist_next(llnode);
2061 wr = &t->isert_cmd->rdma_wr;
2062
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002063 /**
2064 * If send_wr_num is 0 this means that we got
2065 * RDMA completion and we cleared it and we should
2066 * simply decrement the response post. else the
2067 * response is incorporated in send_wr_num, just
2068 * sub it.
2069 **/
2070 if (wr->send_wr_num)
2071 atomic_sub(wr->send_wr_num,
2072 &isert_conn->post_send_buf_count);
2073 else
2074 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002075
2076 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002077 }
2078 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002079
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002080 if (!isert_cmd)
2081 isert_unmap_tx_desc(tx_desc, ib_dev);
2082 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002083 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002084}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002085
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002086static void
2087isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2088{
2089 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2090 struct iscsi_conn *conn = isert_conn->conn;
2091
2092 if (isert_conn->post_recv_buf_count)
2093 return;
2094
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002095 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2096
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002097 if (conn->sess) {
2098 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2099 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100 }
2101
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002102 while (atomic_read(&isert_conn->post_send_buf_count))
2103 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002105 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002106 isert_conn_terminate(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002107 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002108
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002109 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2110
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002111 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002112}
2113
2114static void
2115isert_cq_tx_work(struct work_struct *work)
2116{
2117 struct isert_cq_desc *cq_desc = container_of(work,
2118 struct isert_cq_desc, cq_tx_work);
2119 struct isert_device *device = cq_desc->device;
2120 int cq_index = cq_desc->cq_index;
2121 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2122 struct isert_conn *isert_conn;
2123 struct iser_tx_desc *tx_desc;
2124 struct ib_wc wc;
2125
2126 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2127 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2128 isert_conn = wc.qp->qp_context;
2129
2130 if (wc.status == IB_WC_SUCCESS) {
2131 isert_send_completion(tx_desc, isert_conn);
2132 } else {
2133 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2134 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002135 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002136
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002137 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002138 if (tx_desc->llnode_active)
2139 continue;
2140
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002141 atomic_dec(&isert_conn->post_send_buf_count);
2142 isert_cq_tx_comp_err(tx_desc, isert_conn);
2143 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002144 }
2145 }
2146
2147 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2148}
2149
2150static void
2151isert_cq_tx_callback(struct ib_cq *cq, void *context)
2152{
2153 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2154
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002155 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2156}
2157
2158static void
2159isert_cq_rx_work(struct work_struct *work)
2160{
2161 struct isert_cq_desc *cq_desc = container_of(work,
2162 struct isert_cq_desc, cq_rx_work);
2163 struct isert_device *device = cq_desc->device;
2164 int cq_index = cq_desc->cq_index;
2165 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2166 struct isert_conn *isert_conn;
2167 struct iser_rx_desc *rx_desc;
2168 struct ib_wc wc;
2169 unsigned long xfer_len;
2170
2171 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2172 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2173 isert_conn = wc.qp->qp_context;
2174
2175 if (wc.status == IB_WC_SUCCESS) {
2176 xfer_len = (unsigned long)wc.byte_len;
2177 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2178 } else {
2179 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002180 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002181 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002182 pr_debug("RX wc.vendor_err: 0x%08x\n",
2183 wc.vendor_err);
2184 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002185 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002186 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187 }
2188 }
2189
2190 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2191}
2192
2193static void
2194isert_cq_rx_callback(struct ib_cq *cq, void *context)
2195{
2196 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2197
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002198 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2199}
2200
2201static int
2202isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2203{
2204 struct ib_send_wr *wr_failed;
2205 int ret;
2206
2207 atomic_inc(&isert_conn->post_send_buf_count);
2208
2209 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2210 &wr_failed);
2211 if (ret) {
2212 pr_err("ib_post_send failed with %d\n", ret);
2213 atomic_dec(&isert_conn->post_send_buf_count);
2214 return ret;
2215 }
2216 return ret;
2217}
2218
2219static int
2220isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
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;
2225 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2226 &isert_cmd->tx_desc.iscsi_header;
2227
2228 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2229 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2230 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2231 /*
2232 * Attach SENSE DATA payload to iSCSI Response PDU
2233 */
2234 if (cmd->se_cmd.sense_buffer &&
2235 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2236 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2237 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2238 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002239 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002240
2241 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2242 cmd->sense_buffer);
2243 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2244
2245 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2246 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002247 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002248
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002249 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2250 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002251 DMA_TO_DEVICE);
2252
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002253 isert_cmd->pdu_buf_len = pdu_len;
2254 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2255 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002256 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2257 isert_cmd->tx_desc.num_sge = 2;
2258 }
2259
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002260 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002261
2262 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2263
2264 return isert_post_response(isert_conn, isert_cmd);
2265}
2266
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002267static void
2268isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2269{
2270 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2271 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2272 struct isert_device *device = isert_conn->conn_device;
2273
2274 spin_lock_bh(&conn->cmd_lock);
2275 if (!list_empty(&cmd->i_conn_node))
2276 list_del_init(&cmd->i_conn_node);
2277 spin_unlock_bh(&conn->cmd_lock);
2278
2279 if (cmd->data_direction == DMA_TO_DEVICE)
2280 iscsit_stop_dataout_timer(cmd);
2281
2282 device->unreg_rdma_mem(isert_cmd, isert_conn);
2283}
2284
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002285static enum target_prot_op
2286isert_get_sup_prot_ops(struct iscsi_conn *conn)
2287{
2288 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2289 struct isert_device *device = isert_conn->conn_device;
2290
2291 if (device->pi_capable)
2292 return TARGET_PROT_ALL;
2293
2294 return TARGET_PROT_NORMAL;
2295}
2296
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002297static int
2298isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2299 bool nopout_response)
2300{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002301 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002302 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2303 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2304
2305 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2306 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2307 &isert_cmd->tx_desc.iscsi_header,
2308 nopout_response);
2309 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002310 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002311
Masanari Iida8b513d02013-05-21 23:13:12 +09002312 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002313
2314 return isert_post_response(isert_conn, isert_cmd);
2315}
2316
2317static int
2318isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2319{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002320 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002321 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2322 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2323
2324 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2325 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2326 &isert_cmd->tx_desc.iscsi_header);
2327 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002328 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002329
2330 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2331
2332 return isert_post_response(isert_conn, isert_cmd);
2333}
2334
2335static int
2336isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2337{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002338 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002339 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2340 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2341
2342 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2343 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2344 &isert_cmd->tx_desc.iscsi_header);
2345 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002346 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002347
2348 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2349
2350 return isert_post_response(isert_conn, isert_cmd);
2351}
2352
2353static int
2354isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2355{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002356 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2358 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002359 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2360 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2361 struct iscsi_reject *hdr =
2362 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002363
2364 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002365 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002366 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002367
2368 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002369 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002370 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2371 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002372 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2373 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002374 tx_dsg->length = ISCSI_HDR_LEN;
2375 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2376 isert_cmd->tx_desc.num_sge = 2;
2377
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002378 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002379
2380 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2381
2382 return isert_post_response(isert_conn, isert_cmd);
2383}
2384
2385static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002386isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2387{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002388 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002389 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2390 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2391 struct iscsi_text_rsp *hdr =
2392 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2393 u32 txt_rsp_len;
2394 int rc;
2395
2396 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002397 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002398 if (rc < 0)
2399 return rc;
2400
2401 txt_rsp_len = rc;
2402 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2403
2404 if (txt_rsp_len) {
2405 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2406 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2407 void *txt_rsp_buf = cmd->buf_ptr;
2408
2409 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2410 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2411
2412 isert_cmd->pdu_buf_len = txt_rsp_len;
2413 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2414 tx_dsg->length = txt_rsp_len;
2415 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2416 isert_cmd->tx_desc.num_sge = 2;
2417 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002418 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002419
2420 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2421
2422 return isert_post_response(isert_conn, isert_cmd);
2423}
2424
2425static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2427 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2428 u32 data_left, u32 offset)
2429{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002430 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002431 struct scatterlist *sg_start, *tmp_sg;
2432 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2433 u32 sg_off, page_off;
2434 int i = 0, sg_nents;
2435
2436 sg_off = offset / PAGE_SIZE;
2437 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2438 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2439 page_off = offset % PAGE_SIZE;
2440
2441 send_wr->sg_list = ib_sge;
2442 send_wr->num_sge = sg_nents;
2443 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2444 /*
2445 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2446 */
2447 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2448 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2449 (unsigned long long)tmp_sg->dma_address,
2450 tmp_sg->length, page_off);
2451
2452 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2453 ib_sge->length = min_t(u32, data_left,
2454 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2455 ib_sge->lkey = isert_conn->conn_mr->lkey;
2456
Vu Pham90ecc6e2013-08-28 23:23:33 +03002457 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2458 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459 page_off = 0;
2460 data_left -= ib_sge->length;
2461 ib_sge++;
2462 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2463 }
2464
2465 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2466 send_wr->sg_list, send_wr->num_sge);
2467
2468 return sg_nents;
2469}
2470
2471static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002472isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2473 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474{
2475 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002476 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002477 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002478 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002479 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002480 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002481 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2482 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002483
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002484 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002485
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002486 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2487 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2488 se_cmd->t_data_nents, se_cmd->data_length,
2489 offset, wr->iser_ib_op, &wr->data);
2490 if (ret)
2491 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002492
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002493 data_left = data->len;
2494 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002495
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002496 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002497 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002498 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002499 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002500 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002501 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002502 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002503
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002504 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002505 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2506 GFP_KERNEL);
2507 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002508 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002509 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002510 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002511 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002512
2513 wr->isert_cmd = isert_cmd;
2514 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002515
2516 for (i = 0; i < wr->send_wr_num; i++) {
2517 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2518 data_len = min(data_left, rdma_write_max);
2519
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002520 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002521 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2522 send_wr->opcode = IB_WR_RDMA_WRITE;
2523 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2524 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2525 if (i + 1 == wr->send_wr_num)
2526 send_wr->next = &isert_cmd->tx_desc.send_wr;
2527 else
2528 send_wr->next = &wr->send_wr[i + 1];
2529 } else {
2530 send_wr->opcode = IB_WR_RDMA_READ;
2531 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2532 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2533 if (i + 1 == wr->send_wr_num)
2534 send_wr->send_flags = IB_SEND_SIGNALED;
2535 else
2536 send_wr->next = &wr->send_wr[i + 1];
2537 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002538
2539 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2540 send_wr, data_len, offset);
2541 ib_sge += ib_sge_cnt;
2542
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002543 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002544 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002545 data_left -= data_len;
2546 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002547
2548 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002549unmap_cmd:
2550 isert_unmap_data_buf(isert_conn, data);
2551
Vu Pham90ecc6e2013-08-28 23:23:33 +03002552 return ret;
2553}
2554
2555static int
Vu Pham59464ef2013-08-28 23:23:35 +03002556isert_map_fr_pagelist(struct ib_device *ib_dev,
2557 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2558{
2559 u64 start_addr, end_addr, page, chunk_start = 0;
2560 struct scatterlist *tmp_sg;
2561 int i = 0, new_chunk, last_ent, n_pages;
2562
2563 n_pages = 0;
2564 new_chunk = 1;
2565 last_ent = sg_nents - 1;
2566 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2567 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2568 if (new_chunk)
2569 chunk_start = start_addr;
2570 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2571
2572 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2573 i, (unsigned long long)tmp_sg->dma_address,
2574 tmp_sg->length);
2575
2576 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2577 new_chunk = 0;
2578 continue;
2579 }
2580 new_chunk = 1;
2581
2582 page = chunk_start & PAGE_MASK;
2583 do {
2584 fr_pl[n_pages++] = page;
2585 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2586 n_pages - 1, page);
2587 page += PAGE_SIZE;
2588 } while (page < end_addr);
2589 }
2590
2591 return n_pages;
2592}
2593
2594static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002595isert_fast_reg_mr(struct isert_conn *isert_conn,
2596 struct fast_reg_descriptor *fr_desc,
2597 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002598 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002599 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002600{
Vu Pham59464ef2013-08-28 23:23:35 +03002601 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002602 struct ib_mr *mr;
2603 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002604 struct ib_send_wr fr_wr, inv_wr;
2605 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002606 int ret, pagelist_len;
2607 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002608 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002609
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002610 if (mem->dma_nents == 1) {
2611 sge->lkey = isert_conn->conn_mr->lkey;
2612 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2613 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002614 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2615 __func__, __LINE__, sge->addr, sge->length,
2616 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002617 return 0;
2618 }
2619
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002620 if (ind == ISERT_DATA_KEY_VALID) {
2621 /* Registering data buffer */
2622 mr = fr_desc->data_mr;
2623 frpl = fr_desc->data_frpl;
2624 } else {
2625 /* Registering protection buffer */
2626 mr = fr_desc->pi_ctx->prot_mr;
2627 frpl = fr_desc->pi_ctx->prot_frpl;
2628 }
2629
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002630 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002631
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002632 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002633 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002634
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002635 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002636 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002637
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002638 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002639 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002640 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002641 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002642 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002643 wr = &inv_wr;
2644 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002645 key = (u8)(mr->rkey & 0x000000FF);
2646 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002647 }
2648
2649 /* Prepare FASTREG WR */
2650 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002651 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002652 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002653 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2654 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002655 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2656 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002657 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002658 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002659 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2660
2661 if (!wr)
2662 wr = &fr_wr;
2663 else
2664 wr->next = &fr_wr;
2665
2666 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2667 if (ret) {
2668 pr_err("fast registration failed, ret:%d\n", ret);
2669 return ret;
2670 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002671 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002672
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002673 sge->lkey = mr->lkey;
2674 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002675 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002676
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002677 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2678 __func__, __LINE__, sge->addr, sge->length,
2679 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002680
2681 return ret;
2682}
2683
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002684static inline void
2685isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2686 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002687{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002688 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002689 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2690 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2691 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002692 /*
2693 * At the moment we hard code those, but if in the future
2694 * the target core would like to use it, we will take it
2695 * from se_cmd.
2696 */
2697 domain->sig.dif.apptag_check_mask = 0xffff;
2698 domain->sig.dif.app_escape = true;
2699 domain->sig.dif.ref_escape = true;
2700 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2701 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2702 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002703};
2704
2705static int
2706isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2707{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002708 switch (se_cmd->prot_op) {
2709 case TARGET_PROT_DIN_INSERT:
2710 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002711 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002712 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002713 break;
2714 case TARGET_PROT_DOUT_INSERT:
2715 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002716 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002717 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002718 break;
2719 case TARGET_PROT_DIN_PASS:
2720 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002721 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2722 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002723 break;
2724 default:
2725 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2726 return -EINVAL;
2727 }
2728
2729 return 0;
2730}
2731
2732static inline u8
2733isert_set_prot_checks(u8 prot_checks)
2734{
2735 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2736 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2737 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2738}
2739
2740static int
2741isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2742 struct fast_reg_descriptor *fr_desc,
2743 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2744 struct ib_sge *sig_sge)
2745{
2746 struct ib_send_wr sig_wr, inv_wr;
2747 struct ib_send_wr *bad_wr, *wr = NULL;
2748 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2749 struct ib_sig_attrs sig_attrs;
2750 int ret;
2751 u32 key;
2752
2753 memset(&sig_attrs, 0, sizeof(sig_attrs));
2754 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2755 if (ret)
2756 goto err;
2757
2758 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2759
2760 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2761 memset(&inv_wr, 0, sizeof(inv_wr));
2762 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002763 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002764 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2765 wr = &inv_wr;
2766 /* Bump the key */
2767 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2768 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2769 }
2770
2771 memset(&sig_wr, 0, sizeof(sig_wr));
2772 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002773 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002774 sig_wr.sg_list = data_sge;
2775 sig_wr.num_sge = 1;
2776 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2777 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2778 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2779 if (se_cmd->t_prot_sg)
2780 sig_wr.wr.sig_handover.prot = prot_sge;
2781
2782 if (!wr)
2783 wr = &sig_wr;
2784 else
2785 wr->next = &sig_wr;
2786
2787 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2788 if (ret) {
2789 pr_err("fast registration failed, ret:%d\n", ret);
2790 goto err;
2791 }
2792 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2793
2794 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2795 sig_sge->addr = 0;
2796 sig_sge->length = se_cmd->data_length;
2797 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2798 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2799 /*
2800 * We have protection guards on the wire
2801 * so we need to set a larget transfer
2802 */
2803 sig_sge->length += se_cmd->prot_length;
2804
2805 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2806 sig_sge->addr, sig_sge->length,
2807 sig_sge->lkey);
2808err:
2809 return ret;
2810}
2811
Vu Pham59464ef2013-08-28 23:23:35 +03002812static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002813isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2814 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002815{
2816 struct se_cmd *se_cmd = &cmd->se_cmd;
2817 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002818 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002819 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002820 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002821 struct fast_reg_descriptor *fr_desc = NULL;
2822 u32 offset;
2823 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002824 unsigned long flags;
2825
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002826 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2827
2828 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2829 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2830 se_cmd->t_data_nents, se_cmd->data_length,
2831 offset, wr->iser_ib_op, &wr->data);
2832 if (ret)
2833 return ret;
2834
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002835 if (wr->data.dma_nents != 1 ||
2836 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002837 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2838 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2839 struct fast_reg_descriptor, list);
2840 list_del(&fr_desc->list);
2841 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2842 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002843 }
2844
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002845 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2846 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002847 if (ret)
2848 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002849
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002850 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2851 struct ib_sge prot_sge, sig_sge;
2852
2853 if (se_cmd->t_prot_sg) {
2854 ret = isert_map_data_buf(isert_conn, isert_cmd,
2855 se_cmd->t_prot_sg,
2856 se_cmd->t_prot_nents,
2857 se_cmd->prot_length,
2858 0, wr->iser_ib_op, &wr->prot);
2859 if (ret)
2860 goto unmap_cmd;
2861
2862 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2863 ISERT_PROT_KEY_VALID, &prot_sge);
2864 if (ret)
2865 goto unmap_prot_cmd;
2866 }
2867
2868 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2869 &data_sge, &prot_sge, &sig_sge);
2870 if (ret)
2871 goto unmap_prot_cmd;
2872
2873 fr_desc->ind |= ISERT_PROTECTED;
2874 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2875 } else
2876 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2877
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002878 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002879 wr->send_wr_num = 1;
2880 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2881 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002882 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002883
2884 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002885 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002886 send_wr->num_sge = 1;
2887 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2888 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2889 send_wr->opcode = IB_WR_RDMA_WRITE;
2890 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2891 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002892 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2893 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002894 } else {
2895 send_wr->opcode = IB_WR_RDMA_READ;
2896 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2897 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2898 send_wr->send_flags = IB_SEND_SIGNALED;
2899 }
2900
Vu Pham59464ef2013-08-28 23:23:35 +03002901 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002902unmap_prot_cmd:
2903 if (se_cmd->t_prot_sg)
2904 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002905unmap_cmd:
2906 if (fr_desc) {
2907 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2908 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2909 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2910 }
2911 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002912
Vu Pham59464ef2013-08-28 23:23:35 +03002913 return ret;
2914}
2915
2916static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002917isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2918{
2919 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002920 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002921 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2922 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002923 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002924 struct ib_send_wr *wr_failed;
2925 int rc;
2926
2927 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2928 isert_cmd, se_cmd->data_length);
2929 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002930 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002931 if (rc) {
2932 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2933 return rc;
2934 }
2935
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002936 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2937 /*
2938 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2939 */
2940 isert_create_send_desc(isert_conn, isert_cmd,
2941 &isert_cmd->tx_desc);
2942 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2943 &isert_cmd->tx_desc.iscsi_header);
2944 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2945 isert_init_send_wr(isert_conn, isert_cmd,
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002946 &isert_cmd->tx_desc.send_wr, false);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002947 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002948 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002949 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002950
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002951 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002952
2953 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2954 if (rc) {
2955 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002956 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002957 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002958
2959 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2960 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2961 "READ\n", isert_cmd);
2962 else
2963 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2964 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002965
Vu Pham90ecc6e2013-08-28 23:23:33 +03002966 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002967}
2968
2969static int
2970isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2971{
2972 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002973 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002974 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2975 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002976 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002977 struct ib_send_wr *wr_failed;
2978 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002979
Vu Pham90ecc6e2013-08-28 23:23:33 +03002980 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2981 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002982 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002983 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002984 if (rc) {
2985 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2986 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002987 }
2988
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002989 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002990
2991 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2992 if (rc) {
2993 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002994 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002995 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002996 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2997 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002998
Vu Pham90ecc6e2013-08-28 23:23:33 +03002999 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003000}
3001
3002static int
3003isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3004{
3005 int ret;
3006
3007 switch (state) {
3008 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3009 ret = isert_put_nopin(cmd, conn, false);
3010 break;
3011 default:
3012 pr_err("Unknown immediate state: 0x%02x\n", state);
3013 ret = -EINVAL;
3014 break;
3015 }
3016
3017 return ret;
3018}
3019
3020static int
3021isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3022{
3023 int ret;
3024
3025 switch (state) {
3026 case ISTATE_SEND_LOGOUTRSP:
3027 ret = isert_put_logout_rsp(cmd, conn);
3028 if (!ret) {
3029 pr_debug("Returning iSER Logout -EAGAIN\n");
3030 ret = -EAGAIN;
3031 }
3032 break;
3033 case ISTATE_SEND_NOPIN:
3034 ret = isert_put_nopin(cmd, conn, true);
3035 break;
3036 case ISTATE_SEND_TASKMGTRSP:
3037 ret = isert_put_tm_rsp(cmd, conn);
3038 break;
3039 case ISTATE_SEND_REJECT:
3040 ret = isert_put_reject(cmd, conn);
3041 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003042 case ISTATE_SEND_TEXTRSP:
3043 ret = isert_put_text_rsp(cmd, conn);
3044 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003045 case ISTATE_SEND_STATUS:
3046 /*
3047 * Special case for sending non GOOD SCSI status from TX thread
3048 * context during pre se_cmd excecution failure.
3049 */
3050 ret = isert_put_response(conn, cmd);
3051 break;
3052 default:
3053 pr_err("Unknown response state: 0x%02x\n", state);
3054 ret = -EINVAL;
3055 break;
3056 }
3057
3058 return ret;
3059}
3060
3061static int
3062isert_setup_np(struct iscsi_np *np,
3063 struct __kernel_sockaddr_storage *ksockaddr)
3064{
3065 struct isert_np *isert_np;
3066 struct rdma_cm_id *isert_lid;
3067 struct sockaddr *sa;
3068 int ret;
3069
3070 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3071 if (!isert_np) {
3072 pr_err("Unable to allocate struct isert_np\n");
3073 return -ENOMEM;
3074 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003075 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003076 mutex_init(&isert_np->np_accept_mutex);
3077 INIT_LIST_HEAD(&isert_np->np_accept_list);
3078 init_completion(&isert_np->np_login_comp);
3079
3080 sa = (struct sockaddr *)ksockaddr;
3081 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3082 /*
3083 * Setup the np->np_sockaddr from the passed sockaddr setup
3084 * in iscsi_target_configfs.c code..
3085 */
3086 memcpy(&np->np_sockaddr, ksockaddr,
3087 sizeof(struct __kernel_sockaddr_storage));
3088
3089 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3090 IB_QPT_RC);
3091 if (IS_ERR(isert_lid)) {
3092 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3093 PTR_ERR(isert_lid));
3094 ret = PTR_ERR(isert_lid);
3095 goto out;
3096 }
3097
3098 ret = rdma_bind_addr(isert_lid, sa);
3099 if (ret) {
3100 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3101 goto out_lid;
3102 }
3103
3104 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3105 if (ret) {
3106 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3107 goto out_lid;
3108 }
3109
3110 isert_np->np_cm_id = isert_lid;
3111 np->np_context = isert_np;
3112 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3113
3114 return 0;
3115
3116out_lid:
3117 rdma_destroy_id(isert_lid);
3118out:
3119 kfree(isert_np);
3120 return ret;
3121}
3122
3123static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124isert_rdma_accept(struct isert_conn *isert_conn)
3125{
3126 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3127 struct rdma_conn_param cp;
3128 int ret;
3129
3130 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003131 cp.initiator_depth = isert_conn->initiator_depth;
3132 cp.retry_count = 7;
3133 cp.rnr_retry_count = 7;
3134
3135 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3136
3137 ret = rdma_accept(cm_id, &cp);
3138 if (ret) {
3139 pr_err("rdma_accept() failed with: %d\n", ret);
3140 return ret;
3141 }
3142
3143 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3144
3145 return 0;
3146}
3147
3148static int
3149isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3150{
3151 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3152 int ret;
3153
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003154 pr_info("before login_req comp conn: %p\n", isert_conn);
3155 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3156 if (ret) {
3157 pr_err("isert_conn %p interrupted before got login req\n",
3158 isert_conn);
3159 return ret;
3160 }
3161 reinit_completion(&isert_conn->login_req_comp);
3162
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003163 /*
3164 * For login requests after the first PDU, isert_rx_login_req() will
3165 * kick schedule_delayed_work(&conn->login_work) as the packet is
3166 * received, which turns this callback from iscsi_target_do_login_rx()
3167 * into a NOP.
3168 */
3169 if (!login->first_request)
3170 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003171
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003172 isert_rx_login_req(isert_conn);
3173
3174 pr_info("before conn_login_comp conn: %p\n", conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003175 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3176 if (ret)
3177 return ret;
3178
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003179 pr_info("processing login->req: %p\n", login->req);
3180
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003181 return 0;
3182}
3183
3184static void
3185isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3186 struct isert_conn *isert_conn)
3187{
3188 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3189 struct rdma_route *cm_route = &cm_id->route;
3190 struct sockaddr_in *sock_in;
3191 struct sockaddr_in6 *sock_in6;
3192
3193 conn->login_family = np->np_sockaddr.ss_family;
3194
3195 if (np->np_sockaddr.ss_family == AF_INET6) {
3196 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3197 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3198 &sock_in6->sin6_addr.in6_u);
3199 conn->login_port = ntohs(sock_in6->sin6_port);
3200
3201 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3202 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3203 &sock_in6->sin6_addr.in6_u);
3204 conn->local_port = ntohs(sock_in6->sin6_port);
3205 } else {
3206 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3207 sprintf(conn->login_ip, "%pI4",
3208 &sock_in->sin_addr.s_addr);
3209 conn->login_port = ntohs(sock_in->sin_port);
3210
3211 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3212 sprintf(conn->local_ip, "%pI4",
3213 &sock_in->sin_addr.s_addr);
3214 conn->local_port = ntohs(sock_in->sin_port);
3215 }
3216}
3217
3218static int
3219isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3220{
3221 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3222 struct isert_conn *isert_conn;
3223 int max_accept = 0, ret;
3224
3225accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003226 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003227 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003228 return -ENODEV;
3229
3230 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003231 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003232 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003233 pr_debug("np_thread_state %d for isert_accept_np\n",
3234 np->np_thread_state);
3235 /**
3236 * No point in stalling here when np_thread
3237 * is in state RESET/SHUTDOWN/EXIT - bail
3238 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003239 return -ENODEV;
3240 }
3241 spin_unlock_bh(&np->np_thread_lock);
3242
3243 mutex_lock(&isert_np->np_accept_mutex);
3244 if (list_empty(&isert_np->np_accept_list)) {
3245 mutex_unlock(&isert_np->np_accept_mutex);
3246 max_accept++;
3247 goto accept_wait;
3248 }
3249 isert_conn = list_first_entry(&isert_np->np_accept_list,
3250 struct isert_conn, conn_accept_node);
3251 list_del_init(&isert_conn->conn_accept_node);
3252 mutex_unlock(&isert_np->np_accept_mutex);
3253
3254 conn->context = isert_conn;
3255 isert_conn->conn = conn;
3256 max_accept = 0;
3257
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003258 isert_set_conn_info(np, conn, isert_conn);
3259
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003260 pr_debug("Processing isert_conn: %p\n", isert_conn);
3261
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003262 return 0;
3263}
3264
3265static void
3266isert_free_np(struct iscsi_np *np)
3267{
3268 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3269
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003270 if (isert_np->np_cm_id)
3271 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003272
3273 np->np_context = NULL;
3274 kfree(isert_np);
3275}
3276
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003277static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003278{
3279 struct isert_conn *isert_conn = conn->context;
3280
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003281 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003282
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003283 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003284 /*
3285 * Only wait for conn_wait_comp_err if the isert_conn made it
3286 * into full feature phase..
3287 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003288 if (isert_conn->state == ISER_CONN_INIT) {
3289 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003290 return;
3291 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003292 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003293 mutex_unlock(&isert_conn->conn_mutex);
3294
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003295 wait_for_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003296 wait_for_completion(&isert_conn->conn_wait);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003297
3298 mutex_lock(&isert_conn->conn_mutex);
3299 isert_conn->state = ISER_CONN_DOWN;
3300 mutex_unlock(&isert_conn->conn_mutex);
3301
3302 pr_info("Destroying conn %p\n", isert_conn);
Sagi Grimberg0fc4ea72014-07-02 16:19:25 +03003303 isert_put_conn(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003304}
3305
3306static void isert_free_conn(struct iscsi_conn *conn)
3307{
3308 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003309
3310 isert_put_conn(isert_conn);
3311}
3312
3313static struct iscsit_transport iser_target_transport = {
3314 .name = "IB/iSER",
3315 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003316 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003317 .owner = THIS_MODULE,
3318 .iscsit_setup_np = isert_setup_np,
3319 .iscsit_accept_np = isert_accept_np,
3320 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003321 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003322 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003323 .iscsit_get_login_rx = isert_get_login_rx,
3324 .iscsit_put_login_tx = isert_put_login_tx,
3325 .iscsit_immediate_queue = isert_immediate_queue,
3326 .iscsit_response_queue = isert_response_queue,
3327 .iscsit_get_dataout = isert_get_dataout,
3328 .iscsit_queue_data_in = isert_put_datain,
3329 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003330 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003331 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003332};
3333
3334static int __init isert_init(void)
3335{
3336 int ret;
3337
3338 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3339 if (!isert_rx_wq) {
3340 pr_err("Unable to allocate isert_rx_wq\n");
3341 return -ENOMEM;
3342 }
3343
3344 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3345 if (!isert_comp_wq) {
3346 pr_err("Unable to allocate isert_comp_wq\n");
3347 ret = -ENOMEM;
3348 goto destroy_rx_wq;
3349 }
3350
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003351 iscsit_register_transport(&iser_target_transport);
3352 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3353 return 0;
3354
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003355destroy_rx_wq:
3356 destroy_workqueue(isert_rx_wq);
3357 return ret;
3358}
3359
3360static void __exit isert_exit(void)
3361{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003362 flush_scheduled_work();
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003363 destroy_workqueue(isert_comp_wq);
3364 destroy_workqueue(isert_rx_wq);
3365 iscsit_unregister_transport(&iser_target_transport);
3366 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3367}
3368
3369MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3370MODULE_VERSION("0.1");
3371MODULE_AUTHOR("nab@Linux-iSCSI.org");
3372MODULE_LICENSE("GPL");
3373
3374module_init(isert_init);
3375module_exit(isert_exit);