1 /*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
4 * (c) Copyright 2013 Datera, Inc.
5 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19 #include <linux/string.h>
20 #include <linux/module.h>
21 #include <linux/scatterlist.h>
22 #include <linux/socket.h>
23 #include <linux/in.h>
24 #include <linux/in6.h>
25 #include <linux/llist.h>
26 #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>
31 #include <linux/semaphore.h>
32
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
40 static DEFINE_MUTEX(device_list_mutex);
41 static LIST_HEAD(device_list);
42 static struct workqueue_struct *isert_rx_wq;
43 static struct workqueue_struct *isert_comp_wq;
44 static struct workqueue_struct *isert_release_wq;
45
46 static void
47 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
48 static int
49 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
50 struct isert_rdma_wr *wr);
51 static void
52 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
53 static int
54 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
55 struct isert_rdma_wr *wr);
56 static int
57 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
58 static int
59 isert_rdma_post_recvl(struct isert_conn *isert_conn);
60 static int
61 isert_rdma_accept(struct isert_conn *isert_conn);
62 struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
63
64 static void isert_release_work(struct work_struct *work);
65
66 static inline bool
isert_prot_cmd(struct isert_conn * conn,struct se_cmd * cmd)67 isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
68 {
69 return (conn->pi_support &&
70 cmd->prot_op != TARGET_PROT_NORMAL);
71 }
72
73
74 static void
isert_qp_event_callback(struct ib_event * e,void * context)75 isert_qp_event_callback(struct ib_event *e, void *context)
76 {
77 struct isert_conn *isert_conn = (struct isert_conn *)context;
78
79 pr_err("isert_qp_event_callback event: %d\n", e->event);
80 switch (e->event) {
81 case IB_EVENT_COMM_EST:
82 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
83 break;
84 case IB_EVENT_QP_LAST_WQE_REACHED:
85 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
86 break;
87 default:
88 break;
89 }
90 }
91
92 static int
isert_query_device(struct ib_device * ib_dev,struct ib_device_attr * devattr)93 isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
94 {
95 int ret;
96
97 ret = ib_query_device(ib_dev, devattr);
98 if (ret) {
99 pr_err("ib_query_device() failed: %d\n", ret);
100 return ret;
101 }
102 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
103 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
104
105 return 0;
106 }
107
108 static int
isert_conn_setup_qp(struct isert_conn * isert_conn,struct rdma_cm_id * cma_id)109 isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
110 {
111 struct isert_device *device = isert_conn->conn_device;
112 struct ib_qp_init_attr attr;
113 int ret, index, min_index = 0;
114
115 mutex_lock(&device_list_mutex);
116 for (index = 0; index < device->cqs_used; index++)
117 if (device->cq_active_qps[index] <
118 device->cq_active_qps[min_index])
119 min_index = index;
120 device->cq_active_qps[min_index]++;
121 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
122 mutex_unlock(&device_list_mutex);
123
124 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
125 attr.event_handler = isert_qp_event_callback;
126 attr.qp_context = isert_conn;
127 attr.send_cq = device->dev_tx_cq[min_index];
128 attr.recv_cq = device->dev_rx_cq[min_index];
129 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
130 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
131 /*
132 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
133 * work-around for RDMA_READs with ConnectX-2.
134 *
135 * Also, still make sure to have at least two SGEs for
136 * outgoing control PDU responses.
137 */
138 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
139 isert_conn->max_sge = attr.cap.max_send_sge;
140
141 attr.cap.max_recv_sge = 1;
142 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
143 attr.qp_type = IB_QPT_RC;
144 if (device->pi_capable)
145 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
146
147 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
148 cma_id->device);
149 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
150 isert_conn->conn_pd->device);
151
152 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
153 if (ret) {
154 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
155 goto err;
156 }
157 isert_conn->conn_qp = cma_id->qp;
158 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
159
160 return 0;
161 err:
162 mutex_lock(&device_list_mutex);
163 device->cq_active_qps[min_index]--;
164 mutex_unlock(&device_list_mutex);
165
166 return ret;
167 }
168
169 static void
isert_cq_event_callback(struct ib_event * e,void * context)170 isert_cq_event_callback(struct ib_event *e, void *context)
171 {
172 pr_debug("isert_cq_event_callback event: %d\n", e->event);
173 }
174
175 static int
isert_alloc_rx_descriptors(struct isert_conn * isert_conn)176 isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
177 {
178 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
179 struct iser_rx_desc *rx_desc;
180 struct ib_sge *rx_sg;
181 u64 dma_addr;
182 int i, j;
183
184 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
185 sizeof(struct iser_rx_desc), GFP_KERNEL);
186 if (!isert_conn->conn_rx_descs)
187 goto fail;
188
189 rx_desc = isert_conn->conn_rx_descs;
190
191 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
192 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
193 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
194 if (ib_dma_mapping_error(ib_dev, dma_addr))
195 goto dma_map_fail;
196
197 rx_desc->dma_addr = dma_addr;
198
199 rx_sg = &rx_desc->rx_sg;
200 rx_sg->addr = rx_desc->dma_addr;
201 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
202 rx_sg->lkey = isert_conn->conn_mr->lkey;
203 }
204
205 isert_conn->conn_rx_desc_head = 0;
206 return 0;
207
208 dma_map_fail:
209 rx_desc = isert_conn->conn_rx_descs;
210 for (j = 0; j < i; j++, rx_desc++) {
211 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
212 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
213 }
214 kfree(isert_conn->conn_rx_descs);
215 isert_conn->conn_rx_descs = NULL;
216 fail:
217 return -ENOMEM;
218 }
219
220 static void
isert_free_rx_descriptors(struct isert_conn * isert_conn)221 isert_free_rx_descriptors(struct isert_conn *isert_conn)
222 {
223 struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
224 struct iser_rx_desc *rx_desc;
225 int i;
226
227 if (!isert_conn->conn_rx_descs)
228 return;
229
230 rx_desc = isert_conn->conn_rx_descs;
231 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
232 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
233 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
234 }
235
236 kfree(isert_conn->conn_rx_descs);
237 isert_conn->conn_rx_descs = NULL;
238 }
239
240 static void isert_cq_tx_work(struct work_struct *);
241 static void isert_cq_tx_callback(struct ib_cq *, void *);
242 static void isert_cq_rx_work(struct work_struct *);
243 static void isert_cq_rx_callback(struct ib_cq *, void *);
244
245 static int
isert_create_device_ib_res(struct isert_device * device)246 isert_create_device_ib_res(struct isert_device *device)
247 {
248 struct ib_device *ib_dev = device->ib_device;
249 struct isert_cq_desc *cq_desc;
250 struct ib_device_attr *dev_attr;
251 int ret = 0, i, j;
252 int max_rx_cqe, max_tx_cqe;
253
254 dev_attr = &device->dev_attr;
255 ret = isert_query_device(ib_dev, dev_attr);
256 if (ret)
257 return ret;
258
259 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
260 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
261
262 /* asign function handlers */
263 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
264 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
265 device->use_fastreg = 1;
266 device->reg_rdma_mem = isert_reg_rdma;
267 device->unreg_rdma_mem = isert_unreg_rdma;
268 } else {
269 device->use_fastreg = 0;
270 device->reg_rdma_mem = isert_map_rdma;
271 device->unreg_rdma_mem = isert_unmap_cmd;
272 }
273
274 /* Check signature cap */
275 device->pi_capable = dev_attr->device_cap_flags &
276 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
277
278 device->cqs_used = min_t(int, num_online_cpus(),
279 device->ib_device->num_comp_vectors);
280 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
281 pr_debug("Using %d CQs, device %s supports %d vectors support "
282 "Fast registration %d pi_capable %d\n",
283 device->cqs_used, device->ib_device->name,
284 device->ib_device->num_comp_vectors, device->use_fastreg,
285 device->pi_capable);
286 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
287 device->cqs_used, GFP_KERNEL);
288 if (!device->cq_desc) {
289 pr_err("Unable to allocate device->cq_desc\n");
290 return -ENOMEM;
291 }
292 cq_desc = device->cq_desc;
293
294 for (i = 0; i < device->cqs_used; i++) {
295 cq_desc[i].device = device;
296 cq_desc[i].cq_index = i;
297
298 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
299 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
300 isert_cq_rx_callback,
301 isert_cq_event_callback,
302 (void *)&cq_desc[i],
303 max_rx_cqe, i);
304 if (IS_ERR(device->dev_rx_cq[i])) {
305 ret = PTR_ERR(device->dev_rx_cq[i]);
306 device->dev_rx_cq[i] = NULL;
307 goto out_cq;
308 }
309
310 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
311 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
312 isert_cq_tx_callback,
313 isert_cq_event_callback,
314 (void *)&cq_desc[i],
315 max_tx_cqe, i);
316 if (IS_ERR(device->dev_tx_cq[i])) {
317 ret = PTR_ERR(device->dev_tx_cq[i]);
318 device->dev_tx_cq[i] = NULL;
319 goto out_cq;
320 }
321
322 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
323 if (ret)
324 goto out_cq;
325
326 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
327 if (ret)
328 goto out_cq;
329 }
330
331 return 0;
332
333 out_cq:
334 for (j = 0; j < i; j++) {
335 cq_desc = &device->cq_desc[j];
336
337 if (device->dev_rx_cq[j]) {
338 cancel_work_sync(&cq_desc->cq_rx_work);
339 ib_destroy_cq(device->dev_rx_cq[j]);
340 }
341 if (device->dev_tx_cq[j]) {
342 cancel_work_sync(&cq_desc->cq_tx_work);
343 ib_destroy_cq(device->dev_tx_cq[j]);
344 }
345 }
346 kfree(device->cq_desc);
347
348 return ret;
349 }
350
351 static void
isert_free_device_ib_res(struct isert_device * device)352 isert_free_device_ib_res(struct isert_device *device)
353 {
354 struct isert_cq_desc *cq_desc;
355 int i;
356
357 for (i = 0; i < device->cqs_used; i++) {
358 cq_desc = &device->cq_desc[i];
359
360 cancel_work_sync(&cq_desc->cq_rx_work);
361 cancel_work_sync(&cq_desc->cq_tx_work);
362 ib_destroy_cq(device->dev_rx_cq[i]);
363 ib_destroy_cq(device->dev_tx_cq[i]);
364 device->dev_rx_cq[i] = NULL;
365 device->dev_tx_cq[i] = NULL;
366 }
367
368 kfree(device->cq_desc);
369 }
370
371 static void
isert_device_try_release(struct isert_device * device)372 isert_device_try_release(struct isert_device *device)
373 {
374 mutex_lock(&device_list_mutex);
375 device->refcount--;
376 if (!device->refcount) {
377 isert_free_device_ib_res(device);
378 list_del(&device->dev_node);
379 kfree(device);
380 }
381 mutex_unlock(&device_list_mutex);
382 }
383
384 static struct isert_device *
isert_device_find_by_ib_dev(struct rdma_cm_id * cma_id)385 isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
386 {
387 struct isert_device *device;
388 int ret;
389
390 mutex_lock(&device_list_mutex);
391 list_for_each_entry(device, &device_list, dev_node) {
392 if (device->ib_device->node_guid == cma_id->device->node_guid) {
393 device->refcount++;
394 mutex_unlock(&device_list_mutex);
395 return device;
396 }
397 }
398
399 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
400 if (!device) {
401 mutex_unlock(&device_list_mutex);
402 return ERR_PTR(-ENOMEM);
403 }
404
405 INIT_LIST_HEAD(&device->dev_node);
406
407 device->ib_device = cma_id->device;
408 ret = isert_create_device_ib_res(device);
409 if (ret) {
410 kfree(device);
411 mutex_unlock(&device_list_mutex);
412 return ERR_PTR(ret);
413 }
414
415 device->refcount++;
416 list_add_tail(&device->dev_node, &device_list);
417 mutex_unlock(&device_list_mutex);
418
419 return device;
420 }
421
422 static void
isert_conn_free_fastreg_pool(struct isert_conn * isert_conn)423 isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
424 {
425 struct fast_reg_descriptor *fr_desc, *tmp;
426 int i = 0;
427
428 if (list_empty(&isert_conn->conn_fr_pool))
429 return;
430
431 pr_debug("Freeing conn %p fastreg pool", isert_conn);
432
433 list_for_each_entry_safe(fr_desc, tmp,
434 &isert_conn->conn_fr_pool, list) {
435 list_del(&fr_desc->list);
436 ib_free_fast_reg_page_list(fr_desc->data_frpl);
437 ib_dereg_mr(fr_desc->data_mr);
438 if (fr_desc->pi_ctx) {
439 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
440 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
441 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
442 kfree(fr_desc->pi_ctx);
443 }
444 kfree(fr_desc);
445 ++i;
446 }
447
448 if (i < isert_conn->conn_fr_pool_size)
449 pr_warn("Pool still has %d regions registered\n",
450 isert_conn->conn_fr_pool_size - i);
451 }
452
453 static int
isert_create_pi_ctx(struct fast_reg_descriptor * desc,struct ib_device * device,struct ib_pd * pd)454 isert_create_pi_ctx(struct fast_reg_descriptor *desc,
455 struct ib_device *device,
456 struct ib_pd *pd)
457 {
458 struct ib_mr_init_attr mr_init_attr;
459 struct pi_context *pi_ctx;
460 int ret;
461
462 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
463 if (!pi_ctx) {
464 pr_err("Failed to allocate pi context\n");
465 return -ENOMEM;
466 }
467
468 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
469 ISCSI_ISER_SG_TABLESIZE);
470 if (IS_ERR(pi_ctx->prot_frpl)) {
471 pr_err("Failed to allocate prot frpl err=%ld\n",
472 PTR_ERR(pi_ctx->prot_frpl));
473 ret = PTR_ERR(pi_ctx->prot_frpl);
474 goto err_pi_ctx;
475 }
476
477 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
478 if (IS_ERR(pi_ctx->prot_mr)) {
479 pr_err("Failed to allocate prot frmr err=%ld\n",
480 PTR_ERR(pi_ctx->prot_mr));
481 ret = PTR_ERR(pi_ctx->prot_mr);
482 goto err_prot_frpl;
483 }
484 desc->ind |= ISERT_PROT_KEY_VALID;
485
486 memset(&mr_init_attr, 0, sizeof(mr_init_attr));
487 mr_init_attr.max_reg_descriptors = 2;
488 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
489 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
490 if (IS_ERR(pi_ctx->sig_mr)) {
491 pr_err("Failed to allocate signature enabled mr err=%ld\n",
492 PTR_ERR(pi_ctx->sig_mr));
493 ret = PTR_ERR(pi_ctx->sig_mr);
494 goto err_prot_mr;
495 }
496
497 desc->pi_ctx = pi_ctx;
498 desc->ind |= ISERT_SIG_KEY_VALID;
499 desc->ind &= ~ISERT_PROTECTED;
500
501 return 0;
502
503 err_prot_mr:
504 ib_dereg_mr(desc->pi_ctx->prot_mr);
505 err_prot_frpl:
506 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
507 err_pi_ctx:
508 kfree(desc->pi_ctx);
509
510 return ret;
511 }
512
513 static int
isert_create_fr_desc(struct ib_device * ib_device,struct ib_pd * pd,struct fast_reg_descriptor * fr_desc)514 isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
515 struct fast_reg_descriptor *fr_desc)
516 {
517 int ret;
518
519 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
520 ISCSI_ISER_SG_TABLESIZE);
521 if (IS_ERR(fr_desc->data_frpl)) {
522 pr_err("Failed to allocate data frpl err=%ld\n",
523 PTR_ERR(fr_desc->data_frpl));
524 return PTR_ERR(fr_desc->data_frpl);
525 }
526
527 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
528 if (IS_ERR(fr_desc->data_mr)) {
529 pr_err("Failed to allocate data frmr err=%ld\n",
530 PTR_ERR(fr_desc->data_mr));
531 ret = PTR_ERR(fr_desc->data_mr);
532 goto err_data_frpl;
533 }
534 fr_desc->ind |= ISERT_DATA_KEY_VALID;
535
536 pr_debug("Created fr_desc %p\n", fr_desc);
537
538 return 0;
539
540 err_data_frpl:
541 ib_free_fast_reg_page_list(fr_desc->data_frpl);
542
543 return ret;
544 }
545
546 static int
isert_conn_create_fastreg_pool(struct isert_conn * isert_conn)547 isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
548 {
549 struct fast_reg_descriptor *fr_desc;
550 struct isert_device *device = isert_conn->conn_device;
551 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
552 struct se_node_acl *se_nacl = se_sess->se_node_acl;
553 int i, ret, tag_num;
554 /*
555 * Setup the number of FRMRs based upon the number of tags
556 * available to session in iscsi_target_locate_portal().
557 */
558 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
559 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
560
561 isert_conn->conn_fr_pool_size = 0;
562 for (i = 0; i < tag_num; i++) {
563 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
564 if (!fr_desc) {
565 pr_err("Failed to allocate fast_reg descriptor\n");
566 ret = -ENOMEM;
567 goto err;
568 }
569
570 ret = isert_create_fr_desc(device->ib_device,
571 isert_conn->conn_pd, fr_desc);
572 if (ret) {
573 pr_err("Failed to create fastreg descriptor err=%d\n",
574 ret);
575 kfree(fr_desc);
576 goto err;
577 }
578
579 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
580 isert_conn->conn_fr_pool_size++;
581 }
582
583 pr_debug("Creating conn %p fastreg pool size=%d",
584 isert_conn, isert_conn->conn_fr_pool_size);
585
586 return 0;
587
588 err:
589 isert_conn_free_fastreg_pool(isert_conn);
590 return ret;
591 }
592
593 static int
isert_connect_request(struct rdma_cm_id * cma_id,struct rdma_cm_event * event)594 isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
595 {
596 struct isert_np *isert_np = cma_id->context;
597 struct iscsi_np *np = isert_np->np;
598 struct isert_conn *isert_conn;
599 struct isert_device *device;
600 struct ib_device *ib_dev = cma_id->device;
601 int ret = 0;
602
603 spin_lock_bh(&np->np_thread_lock);
604 if (!np->enabled) {
605 spin_unlock_bh(&np->np_thread_lock);
606 pr_debug("iscsi_np is not enabled, reject connect request\n");
607 return rdma_reject(cma_id, NULL, 0);
608 }
609 spin_unlock_bh(&np->np_thread_lock);
610
611 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
612 cma_id, cma_id->context);
613
614 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
615 if (!isert_conn) {
616 pr_err("Unable to allocate isert_conn\n");
617 return -ENOMEM;
618 }
619 isert_conn->state = ISER_CONN_INIT;
620 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
621 init_completion(&isert_conn->conn_login_comp);
622 init_completion(&isert_conn->login_req_comp);
623 init_completion(&isert_conn->conn_wait);
624 init_completion(&isert_conn->conn_wait_comp_err);
625 kref_init(&isert_conn->conn_kref);
626 mutex_init(&isert_conn->conn_mutex);
627 spin_lock_init(&isert_conn->conn_lock);
628 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
629 INIT_WORK(&isert_conn->release_work, isert_release_work);
630
631 isert_conn->conn_cm_id = cma_id;
632
633 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
634 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
635 if (!isert_conn->login_buf) {
636 pr_err("Unable to allocate isert_conn->login_buf\n");
637 ret = -ENOMEM;
638 goto out;
639 }
640
641 isert_conn->login_req_buf = isert_conn->login_buf;
642 isert_conn->login_rsp_buf = isert_conn->login_buf +
643 ISCSI_DEF_MAX_RECV_SEG_LEN;
644 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
645 isert_conn->login_buf, isert_conn->login_req_buf,
646 isert_conn->login_rsp_buf);
647
648 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
649 (void *)isert_conn->login_req_buf,
650 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
651
652 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
653 if (ret) {
654 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
655 ret);
656 isert_conn->login_req_dma = 0;
657 goto out_login_buf;
658 }
659
660 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
661 (void *)isert_conn->login_rsp_buf,
662 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
663
664 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
665 if (ret) {
666 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
667 ret);
668 isert_conn->login_rsp_dma = 0;
669 goto out_req_dma_map;
670 }
671
672 device = isert_device_find_by_ib_dev(cma_id);
673 if (IS_ERR(device)) {
674 ret = PTR_ERR(device);
675 goto out_rsp_dma_map;
676 }
677
678 /* Set max inflight RDMA READ requests */
679 isert_conn->initiator_depth = min_t(u8,
680 event->param.conn.initiator_depth,
681 device->dev_attr.max_qp_init_rd_atom);
682 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
683
684 isert_conn->conn_device = device;
685 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
686 if (IS_ERR(isert_conn->conn_pd)) {
687 ret = PTR_ERR(isert_conn->conn_pd);
688 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
689 isert_conn, ret);
690 goto out_pd;
691 }
692
693 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
694 IB_ACCESS_LOCAL_WRITE);
695 if (IS_ERR(isert_conn->conn_mr)) {
696 ret = PTR_ERR(isert_conn->conn_mr);
697 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
698 isert_conn, ret);
699 goto out_mr;
700 }
701
702 ret = isert_conn_setup_qp(isert_conn, cma_id);
703 if (ret)
704 goto out_conn_dev;
705
706 ret = isert_rdma_post_recvl(isert_conn);
707 if (ret)
708 goto out_conn_dev;
709
710 ret = isert_rdma_accept(isert_conn);
711 if (ret)
712 goto out_conn_dev;
713
714 mutex_lock(&isert_np->np_accept_mutex);
715 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
716 mutex_unlock(&isert_np->np_accept_mutex);
717
718 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
719 up(&isert_np->np_sem);
720 return 0;
721
722 out_conn_dev:
723 ib_dereg_mr(isert_conn->conn_mr);
724 out_mr:
725 ib_dealloc_pd(isert_conn->conn_pd);
726 out_pd:
727 isert_device_try_release(device);
728 out_rsp_dma_map:
729 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
730 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
731 out_req_dma_map:
732 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
733 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
734 out_login_buf:
735 kfree(isert_conn->login_buf);
736 out:
737 kfree(isert_conn);
738 rdma_reject(cma_id, NULL, 0);
739 return ret;
740 }
741
742 static void
isert_connect_release(struct isert_conn * isert_conn)743 isert_connect_release(struct isert_conn *isert_conn)
744 {
745 struct isert_device *device = isert_conn->conn_device;
746 int cq_index;
747 struct ib_device *ib_dev = device->ib_device;
748
749 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
750
751 if (device && device->use_fastreg)
752 isert_conn_free_fastreg_pool(isert_conn);
753
754 isert_free_rx_descriptors(isert_conn);
755 if (isert_conn->conn_cm_id)
756 rdma_destroy_id(isert_conn->conn_cm_id);
757
758 if (isert_conn->conn_qp) {
759 cq_index = ((struct isert_cq_desc *)
760 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
761 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
762 mutex_lock(&device_list_mutex);
763 isert_conn->conn_device->cq_active_qps[cq_index]--;
764 mutex_unlock(&device_list_mutex);
765
766 ib_destroy_qp(isert_conn->conn_qp);
767 }
768
769 ib_dereg_mr(isert_conn->conn_mr);
770 ib_dealloc_pd(isert_conn->conn_pd);
771
772 if (isert_conn->login_buf) {
773 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
774 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
775 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
776 ISCSI_DEF_MAX_RECV_SEG_LEN,
777 DMA_FROM_DEVICE);
778 kfree(isert_conn->login_buf);
779 }
780 kfree(isert_conn);
781
782 if (device)
783 isert_device_try_release(device);
784
785 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
786 }
787
788 static void
isert_connected_handler(struct rdma_cm_id * cma_id)789 isert_connected_handler(struct rdma_cm_id *cma_id)
790 {
791 struct isert_conn *isert_conn = cma_id->qp->qp_context;
792
793 pr_info("conn %p\n", isert_conn);
794
795 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
796 pr_warn("conn %p connect_release is running\n", isert_conn);
797 return;
798 }
799
800 mutex_lock(&isert_conn->conn_mutex);
801 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
802 isert_conn->state = ISER_CONN_UP;
803 mutex_unlock(&isert_conn->conn_mutex);
804 }
805
806 static void
isert_release_conn_kref(struct kref * kref)807 isert_release_conn_kref(struct kref *kref)
808 {
809 struct isert_conn *isert_conn = container_of(kref,
810 struct isert_conn, conn_kref);
811
812 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
813 current->comm, current->pid);
814
815 isert_connect_release(isert_conn);
816 }
817
818 static void
isert_put_conn(struct isert_conn * isert_conn)819 isert_put_conn(struct isert_conn *isert_conn)
820 {
821 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
822 }
823
824 /**
825 * isert_conn_terminate() - Initiate connection termination
826 * @isert_conn: isert connection struct
827 *
828 * Notes:
829 * In case the connection state is FULL_FEATURE, move state
830 * to TEMINATING and start teardown sequence (rdma_disconnect).
831 * In case the connection state is UP, complete flush as well.
832 *
833 * This routine must be called with conn_mutex held. Thus it is
834 * safe to call multiple times.
835 */
836 static void
isert_conn_terminate(struct isert_conn * isert_conn)837 isert_conn_terminate(struct isert_conn *isert_conn)
838 {
839 int err;
840
841 switch (isert_conn->state) {
842 case ISER_CONN_TERMINATING:
843 break;
844 case ISER_CONN_UP:
845 /*
846 * No flush completions will occur as we didn't
847 * get to ISER_CONN_FULL_FEATURE yet, complete
848 * to allow teardown progress.
849 */
850 complete(&isert_conn->conn_wait_comp_err);
851 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
852 pr_info("Terminating conn %p state %d\n",
853 isert_conn, isert_conn->state);
854 isert_conn->state = ISER_CONN_TERMINATING;
855 err = rdma_disconnect(isert_conn->conn_cm_id);
856 if (err)
857 pr_warn("Failed rdma_disconnect isert_conn %p\n",
858 isert_conn);
859 break;
860 default:
861 pr_warn("conn %p teminating in state %d\n",
862 isert_conn, isert_conn->state);
863 }
864 }
865
866 static int
isert_np_cma_handler(struct isert_np * isert_np,enum rdma_cm_event_type event)867 isert_np_cma_handler(struct isert_np *isert_np,
868 enum rdma_cm_event_type event)
869 {
870 pr_debug("isert np %p, handling event %d\n", isert_np, event);
871
872 switch (event) {
873 case RDMA_CM_EVENT_DEVICE_REMOVAL:
874 isert_np->np_cm_id = NULL;
875 break;
876 case RDMA_CM_EVENT_ADDR_CHANGE:
877 isert_np->np_cm_id = isert_setup_id(isert_np);
878 if (IS_ERR(isert_np->np_cm_id)) {
879 pr_err("isert np %p setup id failed: %ld\n",
880 isert_np, PTR_ERR(isert_np->np_cm_id));
881 isert_np->np_cm_id = NULL;
882 }
883 break;
884 default:
885 pr_err("isert np %p Unexpected event %d\n",
886 isert_np, event);
887 }
888
889 return -1;
890 }
891
892 static int
isert_disconnected_handler(struct rdma_cm_id * cma_id,enum rdma_cm_event_type event)893 isert_disconnected_handler(struct rdma_cm_id *cma_id,
894 enum rdma_cm_event_type event)
895 {
896 struct isert_np *isert_np = cma_id->context;
897 struct isert_conn *isert_conn;
898 bool terminating = false;
899
900 if (isert_np->np_cm_id == cma_id)
901 return isert_np_cma_handler(cma_id->context, event);
902
903 isert_conn = cma_id->qp->qp_context;
904
905 mutex_lock(&isert_conn->conn_mutex);
906 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
907 isert_conn_terminate(isert_conn);
908 mutex_unlock(&isert_conn->conn_mutex);
909
910 pr_info("conn %p completing conn_wait\n", isert_conn);
911 complete(&isert_conn->conn_wait);
912
913 if (terminating)
914 goto out;
915
916 mutex_lock(&isert_np->np_accept_mutex);
917 if (!list_empty(&isert_conn->conn_accept_node)) {
918 list_del_init(&isert_conn->conn_accept_node);
919 isert_put_conn(isert_conn);
920 queue_work(isert_release_wq, &isert_conn->release_work);
921 }
922 mutex_unlock(&isert_np->np_accept_mutex);
923
924 out:
925 return 0;
926 }
927
928 static int
isert_connect_error(struct rdma_cm_id * cma_id)929 isert_connect_error(struct rdma_cm_id *cma_id)
930 {
931 struct isert_conn *isert_conn = cma_id->qp->qp_context;
932
933 isert_conn->conn_cm_id = NULL;
934 isert_put_conn(isert_conn);
935
936 return -1;
937 }
938
939 static int
isert_cma_handler(struct rdma_cm_id * cma_id,struct rdma_cm_event * event)940 isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
941 {
942 int ret = 0;
943
944 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
945 event->event, event->status, cma_id->context, cma_id);
946
947 switch (event->event) {
948 case RDMA_CM_EVENT_CONNECT_REQUEST:
949 ret = isert_connect_request(cma_id, event);
950 if (ret)
951 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
952 event->event, ret);
953 break;
954 case RDMA_CM_EVENT_ESTABLISHED:
955 isert_connected_handler(cma_id);
956 break;
957 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
958 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
959 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
960 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
961 ret = isert_disconnected_handler(cma_id, event->event);
962 break;
963 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
964 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
965 case RDMA_CM_EVENT_CONNECT_ERROR:
966 ret = isert_connect_error(cma_id);
967 break;
968 default:
969 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
970 break;
971 }
972
973 return ret;
974 }
975
976 static int
isert_post_recv(struct isert_conn * isert_conn,u32 count)977 isert_post_recv(struct isert_conn *isert_conn, u32 count)
978 {
979 struct ib_recv_wr *rx_wr, *rx_wr_failed;
980 int i, ret;
981 unsigned int rx_head = isert_conn->conn_rx_desc_head;
982 struct iser_rx_desc *rx_desc;
983
984 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
985 rx_desc = &isert_conn->conn_rx_descs[rx_head];
986 rx_wr->wr_id = (unsigned long)rx_desc;
987 rx_wr->sg_list = &rx_desc->rx_sg;
988 rx_wr->num_sge = 1;
989 rx_wr->next = rx_wr + 1;
990 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
991 }
992
993 rx_wr--;
994 rx_wr->next = NULL; /* mark end of work requests list */
995
996 isert_conn->post_recv_buf_count += count;
997 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
998 &rx_wr_failed);
999 if (ret) {
1000 pr_err("ib_post_recv() failed with ret: %d\n", ret);
1001 isert_conn->post_recv_buf_count -= count;
1002 } else {
1003 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
1004 isert_conn->conn_rx_desc_head = rx_head;
1005 }
1006 return ret;
1007 }
1008
1009 static int
isert_post_send(struct isert_conn * isert_conn,struct iser_tx_desc * tx_desc)1010 isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1011 {
1012 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1013 struct ib_send_wr send_wr, *send_wr_failed;
1014 int ret;
1015
1016 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1017 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1018
1019 send_wr.next = NULL;
1020 send_wr.wr_id = (unsigned long)tx_desc;
1021 send_wr.sg_list = tx_desc->tx_sg;
1022 send_wr.num_sge = tx_desc->num_sge;
1023 send_wr.opcode = IB_WR_SEND;
1024 send_wr.send_flags = IB_SEND_SIGNALED;
1025
1026 atomic_inc(&isert_conn->post_send_buf_count);
1027
1028 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1029 if (ret) {
1030 pr_err("ib_post_send() failed, ret: %d\n", ret);
1031 atomic_dec(&isert_conn->post_send_buf_count);
1032 }
1033
1034 return ret;
1035 }
1036
1037 static void
isert_create_send_desc(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct iser_tx_desc * tx_desc)1038 isert_create_send_desc(struct isert_conn *isert_conn,
1039 struct isert_cmd *isert_cmd,
1040 struct iser_tx_desc *tx_desc)
1041 {
1042 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1043
1044 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1045 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1046
1047 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1048 tx_desc->iser_header.flags = ISER_VER;
1049
1050 tx_desc->num_sge = 1;
1051 tx_desc->isert_cmd = isert_cmd;
1052
1053 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1054 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1055 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1056 }
1057 }
1058
1059 static int
isert_init_tx_hdrs(struct isert_conn * isert_conn,struct iser_tx_desc * tx_desc)1060 isert_init_tx_hdrs(struct isert_conn *isert_conn,
1061 struct iser_tx_desc *tx_desc)
1062 {
1063 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1064 u64 dma_addr;
1065
1066 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1067 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1068 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1069 pr_err("ib_dma_mapping_error() failed\n");
1070 return -ENOMEM;
1071 }
1072
1073 tx_desc->dma_addr = dma_addr;
1074 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1075 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1076 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1077
1078 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1079 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1080 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1081
1082 return 0;
1083 }
1084
1085 static void
isert_init_send_wr(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct ib_send_wr * send_wr,bool coalesce)1086 isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1087 struct ib_send_wr *send_wr, bool coalesce)
1088 {
1089 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1090
1091 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1092 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1093 send_wr->opcode = IB_WR_SEND;
1094 send_wr->sg_list = &tx_desc->tx_sg[0];
1095 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
1096 /*
1097 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
1098 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
1099 */
1100 mutex_lock(&isert_conn->conn_mutex);
1101 if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
1102 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
1103 tx_desc->llnode_active = true;
1104 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
1105 mutex_unlock(&isert_conn->conn_mutex);
1106 return;
1107 }
1108 isert_conn->conn_comp_batch = 0;
1109 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
1110 mutex_unlock(&isert_conn->conn_mutex);
1111
1112 send_wr->send_flags = IB_SEND_SIGNALED;
1113 }
1114
1115 static int
isert_rdma_post_recvl(struct isert_conn * isert_conn)1116 isert_rdma_post_recvl(struct isert_conn *isert_conn)
1117 {
1118 struct ib_recv_wr rx_wr, *rx_wr_fail;
1119 struct ib_sge sge;
1120 int ret;
1121
1122 memset(&sge, 0, sizeof(struct ib_sge));
1123 sge.addr = isert_conn->login_req_dma;
1124 sge.length = ISER_RX_LOGIN_SIZE;
1125 sge.lkey = isert_conn->conn_mr->lkey;
1126
1127 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1128 sge.addr, sge.length, sge.lkey);
1129
1130 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1131 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1132 rx_wr.sg_list = &sge;
1133 rx_wr.num_sge = 1;
1134
1135 isert_conn->post_recv_buf_count++;
1136 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1137 if (ret) {
1138 pr_err("ib_post_recv() failed: %d\n", ret);
1139 isert_conn->post_recv_buf_count--;
1140 }
1141
1142 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1143 return ret;
1144 }
1145
1146 static int
isert_put_login_tx(struct iscsi_conn * conn,struct iscsi_login * login,u32 length)1147 isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1148 u32 length)
1149 {
1150 struct isert_conn *isert_conn = conn->context;
1151 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1152 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1153 int ret;
1154
1155 isert_create_send_desc(isert_conn, NULL, tx_desc);
1156
1157 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1158 sizeof(struct iscsi_hdr));
1159
1160 isert_init_tx_hdrs(isert_conn, tx_desc);
1161
1162 if (length > 0) {
1163 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1164
1165 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1166 length, DMA_TO_DEVICE);
1167
1168 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1169
1170 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1171 length, DMA_TO_DEVICE);
1172
1173 tx_dsg->addr = isert_conn->login_rsp_dma;
1174 tx_dsg->length = length;
1175 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1176 tx_desc->num_sge = 2;
1177 }
1178 if (!login->login_failed) {
1179 if (login->login_complete) {
1180 if (!conn->sess->sess_ops->SessionType &&
1181 isert_conn->conn_device->use_fastreg) {
1182 ret = isert_conn_create_fastreg_pool(isert_conn);
1183 if (ret) {
1184 pr_err("Conn: %p failed to create"
1185 " fastreg pool\n", isert_conn);
1186 return ret;
1187 }
1188 }
1189
1190 ret = isert_alloc_rx_descriptors(isert_conn);
1191 if (ret)
1192 return ret;
1193
1194 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1195 if (ret)
1196 return ret;
1197
1198 /* Now we are in FULL_FEATURE phase */
1199 mutex_lock(&isert_conn->conn_mutex);
1200 isert_conn->state = ISER_CONN_FULL_FEATURE;
1201 mutex_unlock(&isert_conn->conn_mutex);
1202 goto post_send;
1203 }
1204
1205 ret = isert_rdma_post_recvl(isert_conn);
1206 if (ret)
1207 return ret;
1208 }
1209 post_send:
1210 ret = isert_post_send(isert_conn, tx_desc);
1211 if (ret)
1212 return ret;
1213
1214 return 0;
1215 }
1216
1217 static void
isert_rx_login_req(struct isert_conn * isert_conn)1218 isert_rx_login_req(struct isert_conn *isert_conn)
1219 {
1220 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1221 int rx_buflen = isert_conn->login_req_len;
1222 struct iscsi_conn *conn = isert_conn->conn;
1223 struct iscsi_login *login = conn->conn_login;
1224 int size;
1225
1226 pr_info("conn %p\n", isert_conn);
1227
1228 WARN_ON_ONCE(!login);
1229
1230 if (login->first_request) {
1231 struct iscsi_login_req *login_req =
1232 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1233 /*
1234 * Setup the initial iscsi_login values from the leading
1235 * login request PDU.
1236 */
1237 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1238 login->current_stage =
1239 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1240 >> 2;
1241 login->version_min = login_req->min_version;
1242 login->version_max = login_req->max_version;
1243 memcpy(login->isid, login_req->isid, 6);
1244 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1245 login->init_task_tag = login_req->itt;
1246 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1247 login->cid = be16_to_cpu(login_req->cid);
1248 login->tsih = be16_to_cpu(login_req->tsih);
1249 }
1250
1251 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1252
1253 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1254 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1255 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1256 memcpy(login->req_buf, &rx_desc->data[0], size);
1257
1258 if (login->first_request) {
1259 complete(&isert_conn->conn_login_comp);
1260 return;
1261 }
1262 schedule_delayed_work(&conn->login_work, 0);
1263 }
1264
1265 static struct iscsi_cmd
isert_allocate_cmd(struct iscsi_conn * conn)1266 *isert_allocate_cmd(struct iscsi_conn *conn)
1267 {
1268 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1269 struct isert_cmd *isert_cmd;
1270 struct iscsi_cmd *cmd;
1271
1272 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1273 if (!cmd) {
1274 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1275 return NULL;
1276 }
1277 isert_cmd = iscsit_priv_cmd(cmd);
1278 isert_cmd->conn = isert_conn;
1279 isert_cmd->iscsi_cmd = cmd;
1280
1281 return cmd;
1282 }
1283
1284 static int
isert_handle_scsi_cmd(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct iscsi_cmd * cmd,struct iser_rx_desc * rx_desc,unsigned char * buf)1285 isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1286 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1287 struct iser_rx_desc *rx_desc, unsigned char *buf)
1288 {
1289 struct iscsi_conn *conn = isert_conn->conn;
1290 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1291 struct scatterlist *sg;
1292 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1293 bool dump_payload = false;
1294
1295 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1296 if (rc < 0)
1297 return rc;
1298
1299 imm_data = cmd->immediate_data;
1300 imm_data_len = cmd->first_burst_len;
1301 unsol_data = cmd->unsolicited_data;
1302
1303 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1304 if (rc < 0) {
1305 return 0;
1306 } else if (rc > 0) {
1307 dump_payload = true;
1308 goto sequence_cmd;
1309 }
1310
1311 if (!imm_data)
1312 return 0;
1313
1314 sg = &cmd->se_cmd.t_data_sg[0];
1315 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1316
1317 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1318 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1319
1320 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1321
1322 cmd->write_data_done += imm_data_len;
1323
1324 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1325 spin_lock_bh(&cmd->istate_lock);
1326 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1327 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1328 spin_unlock_bh(&cmd->istate_lock);
1329 }
1330
1331 sequence_cmd:
1332 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1333
1334 if (!rc && dump_payload == false && unsol_data)
1335 iscsit_set_unsoliticed_dataout(cmd);
1336 else if (dump_payload && imm_data)
1337 target_put_sess_cmd(&cmd->se_cmd);
1338
1339 return 0;
1340 }
1341
1342 static int
isert_handle_iscsi_dataout(struct isert_conn * isert_conn,struct iser_rx_desc * rx_desc,unsigned char * buf)1343 isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1344 struct iser_rx_desc *rx_desc, unsigned char *buf)
1345 {
1346 struct scatterlist *sg_start;
1347 struct iscsi_conn *conn = isert_conn->conn;
1348 struct iscsi_cmd *cmd = NULL;
1349 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1350 u32 unsol_data_len = ntoh24(hdr->dlength);
1351 int rc, sg_nents, sg_off, page_off;
1352
1353 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1354 if (rc < 0)
1355 return rc;
1356 else if (!cmd)
1357 return 0;
1358 /*
1359 * FIXME: Unexpected unsolicited_data out
1360 */
1361 if (!cmd->unsolicited_data) {
1362 pr_err("Received unexpected solicited data payload\n");
1363 dump_stack();
1364 return -1;
1365 }
1366
1367 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1368 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1369
1370 sg_off = cmd->write_data_done / PAGE_SIZE;
1371 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1372 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1373 page_off = cmd->write_data_done % PAGE_SIZE;
1374 /*
1375 * FIXME: Non page-aligned unsolicited_data out
1376 */
1377 if (page_off) {
1378 pr_err("Received unexpected non-page aligned data payload\n");
1379 dump_stack();
1380 return -1;
1381 }
1382 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1383 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1384
1385 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1386 unsol_data_len);
1387
1388 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1389 if (rc < 0)
1390 return rc;
1391
1392 return 0;
1393 }
1394
1395 static int
isert_handle_nop_out(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct iscsi_cmd * cmd,struct iser_rx_desc * rx_desc,unsigned char * buf)1396 isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1397 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1398 unsigned char *buf)
1399 {
1400 struct iscsi_conn *conn = isert_conn->conn;
1401 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1402 int rc;
1403
1404 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1405 if (rc < 0)
1406 return rc;
1407 /*
1408 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1409 */
1410
1411 return iscsit_process_nop_out(conn, cmd, hdr);
1412 }
1413
1414 static int
isert_handle_text_cmd(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct iscsi_cmd * cmd,struct iser_rx_desc * rx_desc,struct iscsi_text * hdr)1415 isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1416 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1417 struct iscsi_text *hdr)
1418 {
1419 struct iscsi_conn *conn = isert_conn->conn;
1420 u32 payload_length = ntoh24(hdr->dlength);
1421 int rc;
1422 unsigned char *text_in;
1423
1424 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1425 if (rc < 0)
1426 return rc;
1427
1428 text_in = kzalloc(payload_length, GFP_KERNEL);
1429 if (!text_in) {
1430 pr_err("Unable to allocate text_in of payload_length: %u\n",
1431 payload_length);
1432 return -ENOMEM;
1433 }
1434 cmd->text_in_ptr = text_in;
1435
1436 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1437
1438 return iscsit_process_text_cmd(conn, cmd, hdr);
1439 }
1440
1441 static int
isert_rx_opcode(struct isert_conn * isert_conn,struct iser_rx_desc * rx_desc,uint32_t read_stag,uint64_t read_va,uint32_t write_stag,uint64_t write_va)1442 isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1443 uint32_t read_stag, uint64_t read_va,
1444 uint32_t write_stag, uint64_t write_va)
1445 {
1446 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1447 struct iscsi_conn *conn = isert_conn->conn;
1448 struct iscsi_session *sess = conn->sess;
1449 struct iscsi_cmd *cmd;
1450 struct isert_cmd *isert_cmd;
1451 int ret = -EINVAL;
1452 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1453
1454 if (sess->sess_ops->SessionType &&
1455 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1456 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1457 " ignoring\n", opcode);
1458 return 0;
1459 }
1460
1461 switch (opcode) {
1462 case ISCSI_OP_SCSI_CMD:
1463 cmd = isert_allocate_cmd(conn);
1464 if (!cmd)
1465 break;
1466
1467 isert_cmd = iscsit_priv_cmd(cmd);
1468 isert_cmd->read_stag = read_stag;
1469 isert_cmd->read_va = read_va;
1470 isert_cmd->write_stag = write_stag;
1471 isert_cmd->write_va = write_va;
1472
1473 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1474 rx_desc, (unsigned char *)hdr);
1475 break;
1476 case ISCSI_OP_NOOP_OUT:
1477 cmd = isert_allocate_cmd(conn);
1478 if (!cmd)
1479 break;
1480
1481 isert_cmd = iscsit_priv_cmd(cmd);
1482 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1483 rx_desc, (unsigned char *)hdr);
1484 break;
1485 case ISCSI_OP_SCSI_DATA_OUT:
1486 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1487 (unsigned char *)hdr);
1488 break;
1489 case ISCSI_OP_SCSI_TMFUNC:
1490 cmd = isert_allocate_cmd(conn);
1491 if (!cmd)
1492 break;
1493
1494 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1495 (unsigned char *)hdr);
1496 break;
1497 case ISCSI_OP_LOGOUT:
1498 cmd = isert_allocate_cmd(conn);
1499 if (!cmd)
1500 break;
1501
1502 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1503 if (ret > 0)
1504 wait_for_completion_timeout(&conn->conn_logout_comp,
1505 SECONDS_FOR_LOGOUT_COMP *
1506 HZ);
1507 break;
1508 case ISCSI_OP_TEXT:
1509 cmd = isert_allocate_cmd(conn);
1510 if (!cmd)
1511 break;
1512
1513 isert_cmd = iscsit_priv_cmd(cmd);
1514 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1515 rx_desc, (struct iscsi_text *)hdr);
1516 break;
1517 default:
1518 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1519 dump_stack();
1520 break;
1521 }
1522
1523 return ret;
1524 }
1525
1526 static void
isert_rx_do_work(struct iser_rx_desc * rx_desc,struct isert_conn * isert_conn)1527 isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1528 {
1529 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1530 uint64_t read_va = 0, write_va = 0;
1531 uint32_t read_stag = 0, write_stag = 0;
1532 int rc;
1533
1534 switch (iser_hdr->flags & 0xF0) {
1535 case ISCSI_CTRL:
1536 if (iser_hdr->flags & ISER_RSV) {
1537 read_stag = be32_to_cpu(iser_hdr->read_stag);
1538 read_va = be64_to_cpu(iser_hdr->read_va);
1539 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1540 read_stag, (unsigned long long)read_va);
1541 }
1542 if (iser_hdr->flags & ISER_WSV) {
1543 write_stag = be32_to_cpu(iser_hdr->write_stag);
1544 write_va = be64_to_cpu(iser_hdr->write_va);
1545 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1546 write_stag, (unsigned long long)write_va);
1547 }
1548
1549 pr_debug("ISER ISCSI_CTRL PDU\n");
1550 break;
1551 case ISER_HELLO:
1552 pr_err("iSER Hello message\n");
1553 break;
1554 default:
1555 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1556 break;
1557 }
1558
1559 rc = isert_rx_opcode(isert_conn, rx_desc,
1560 read_stag, read_va, write_stag, write_va);
1561 }
1562
1563 static void
isert_rx_completion(struct iser_rx_desc * desc,struct isert_conn * isert_conn,unsigned long xfer_len)1564 isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1565 unsigned long xfer_len)
1566 {
1567 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1568 struct iscsi_hdr *hdr;
1569 u64 rx_dma;
1570 int rx_buflen, outstanding;
1571
1572 if ((char *)desc == isert_conn->login_req_buf) {
1573 rx_dma = isert_conn->login_req_dma;
1574 rx_buflen = ISER_RX_LOGIN_SIZE;
1575 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1576 rx_dma, rx_buflen);
1577 } else {
1578 rx_dma = desc->dma_addr;
1579 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1580 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1581 rx_dma, rx_buflen);
1582 }
1583
1584 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1585
1586 hdr = &desc->iscsi_header;
1587 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1588 hdr->opcode, hdr->itt, hdr->flags,
1589 (int)(xfer_len - ISER_HEADERS_LEN));
1590
1591 if ((char *)desc == isert_conn->login_req_buf) {
1592 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1593 if (isert_conn->conn) {
1594 struct iscsi_login *login = isert_conn->conn->conn_login;
1595
1596 if (login && !login->first_request)
1597 isert_rx_login_req(isert_conn);
1598 }
1599 mutex_lock(&isert_conn->conn_mutex);
1600 complete(&isert_conn->login_req_comp);
1601 mutex_unlock(&isert_conn->conn_mutex);
1602 } else {
1603 isert_rx_do_work(desc, isert_conn);
1604 }
1605
1606 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1607 DMA_FROM_DEVICE);
1608
1609 isert_conn->post_recv_buf_count--;
1610 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1611 isert_conn->post_recv_buf_count);
1612
1613 if ((char *)desc == isert_conn->login_req_buf)
1614 return;
1615
1616 outstanding = isert_conn->post_recv_buf_count;
1617 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1618 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1619 ISERT_MIN_POSTED_RX);
1620 err = isert_post_recv(isert_conn, count);
1621 if (err) {
1622 pr_err("isert_post_recv() count: %d failed, %d\n",
1623 count, err);
1624 }
1625 }
1626 }
1627
1628 static int
isert_map_data_buf(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct scatterlist * sg,u32 nents,u32 length,u32 offset,enum iser_ib_op_code op,struct isert_data_buf * data)1629 isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1630 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1631 enum iser_ib_op_code op, struct isert_data_buf *data)
1632 {
1633 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1634
1635 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1636 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1637
1638 data->len = length - offset;
1639 data->offset = offset;
1640 data->sg_off = data->offset / PAGE_SIZE;
1641
1642 data->sg = &sg[data->sg_off];
1643 data->nents = min_t(unsigned int, nents - data->sg_off,
1644 ISCSI_ISER_SG_TABLESIZE);
1645 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1646 PAGE_SIZE);
1647
1648 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1649 data->dma_dir);
1650 if (unlikely(!data->dma_nents)) {
1651 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1652 return -EINVAL;
1653 }
1654
1655 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1656 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1657
1658 return 0;
1659 }
1660
1661 static void
isert_unmap_data_buf(struct isert_conn * isert_conn,struct isert_data_buf * data)1662 isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1663 {
1664 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1665
1666 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1667 memset(data, 0, sizeof(*data));
1668 }
1669
1670
1671
1672 static void
isert_unmap_cmd(struct isert_cmd * isert_cmd,struct isert_conn * isert_conn)1673 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1674 {
1675 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1676
1677 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1678
1679 if (wr->data.sg) {
1680 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1681 isert_unmap_data_buf(isert_conn, &wr->data);
1682 }
1683
1684 if (wr->send_wr) {
1685 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1686 kfree(wr->send_wr);
1687 wr->send_wr = NULL;
1688 }
1689
1690 if (wr->ib_sge) {
1691 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1692 kfree(wr->ib_sge);
1693 wr->ib_sge = NULL;
1694 }
1695 }
1696
1697 static void
isert_unreg_rdma(struct isert_cmd * isert_cmd,struct isert_conn * isert_conn)1698 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1699 {
1700 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1701 LIST_HEAD(unmap_list);
1702
1703 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
1704
1705 if (wr->fr_desc) {
1706 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
1707 isert_cmd, wr->fr_desc);
1708 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1709 isert_unmap_data_buf(isert_conn, &wr->prot);
1710 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1711 }
1712 spin_lock_bh(&isert_conn->conn_lock);
1713 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
1714 spin_unlock_bh(&isert_conn->conn_lock);
1715 wr->fr_desc = NULL;
1716 }
1717
1718 if (wr->data.sg) {
1719 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1720 isert_unmap_data_buf(isert_conn, &wr->data);
1721 }
1722
1723 wr->ib_sge = NULL;
1724 wr->send_wr = NULL;
1725 }
1726
1727 static void
isert_put_cmd(struct isert_cmd * isert_cmd,bool comp_err)1728 isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1729 {
1730 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1731 struct isert_conn *isert_conn = isert_cmd->conn;
1732 struct iscsi_conn *conn = isert_conn->conn;
1733 struct isert_device *device = isert_conn->conn_device;
1734
1735 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1736
1737 switch (cmd->iscsi_opcode) {
1738 case ISCSI_OP_SCSI_CMD:
1739 spin_lock_bh(&conn->cmd_lock);
1740 if (!list_empty(&cmd->i_conn_node))
1741 list_del_init(&cmd->i_conn_node);
1742 spin_unlock_bh(&conn->cmd_lock);
1743
1744 if (cmd->data_direction == DMA_TO_DEVICE) {
1745 iscsit_stop_dataout_timer(cmd);
1746 /*
1747 * Check for special case during comp_err where
1748 * WRITE_PENDING has been handed off from core,
1749 * but requires an extra target_put_sess_cmd()
1750 * before transport_generic_free_cmd() below.
1751 */
1752 if (comp_err &&
1753 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1754 struct se_cmd *se_cmd = &cmd->se_cmd;
1755
1756 target_put_sess_cmd(se_cmd);
1757 }
1758 }
1759
1760 device->unreg_rdma_mem(isert_cmd, isert_conn);
1761 transport_generic_free_cmd(&cmd->se_cmd, 0);
1762 break;
1763 case ISCSI_OP_SCSI_TMFUNC:
1764 spin_lock_bh(&conn->cmd_lock);
1765 if (!list_empty(&cmd->i_conn_node))
1766 list_del_init(&cmd->i_conn_node);
1767 spin_unlock_bh(&conn->cmd_lock);
1768
1769 transport_generic_free_cmd(&cmd->se_cmd, 0);
1770 break;
1771 case ISCSI_OP_REJECT:
1772 case ISCSI_OP_NOOP_OUT:
1773 case ISCSI_OP_TEXT:
1774 spin_lock_bh(&conn->cmd_lock);
1775 if (!list_empty(&cmd->i_conn_node))
1776 list_del_init(&cmd->i_conn_node);
1777 spin_unlock_bh(&conn->cmd_lock);
1778
1779 /*
1780 * Handle special case for REJECT when iscsi_add_reject*() has
1781 * overwritten the original iscsi_opcode assignment, and the
1782 * associated cmd->se_cmd needs to be released.
1783 */
1784 if (cmd->se_cmd.se_tfo != NULL) {
1785 pr_debug("Calling transport_generic_free_cmd from"
1786 " isert_put_cmd for 0x%02x\n",
1787 cmd->iscsi_opcode);
1788 transport_generic_free_cmd(&cmd->se_cmd, 0);
1789 break;
1790 }
1791 /*
1792 * Fall-through
1793 */
1794 default:
1795 iscsit_release_cmd(cmd);
1796 break;
1797 }
1798 }
1799
1800 static void
isert_unmap_tx_desc(struct iser_tx_desc * tx_desc,struct ib_device * ib_dev)1801 isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1802 {
1803 if (tx_desc->dma_addr != 0) {
1804 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1805 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1806 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1807 tx_desc->dma_addr = 0;
1808 }
1809 }
1810
1811 static void
isert_completion_put(struct iser_tx_desc * tx_desc,struct isert_cmd * isert_cmd,struct ib_device * ib_dev,bool comp_err)1812 isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1813 struct ib_device *ib_dev, bool comp_err)
1814 {
1815 if (isert_cmd->pdu_buf_dma != 0) {
1816 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1817 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1818 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1819 isert_cmd->pdu_buf_dma = 0;
1820 }
1821
1822 isert_unmap_tx_desc(tx_desc, ib_dev);
1823 isert_put_cmd(isert_cmd, comp_err);
1824 }
1825
1826 static int
isert_check_pi_status(struct se_cmd * se_cmd,struct ib_mr * sig_mr)1827 isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1828 {
1829 struct ib_mr_status mr_status;
1830 int ret;
1831
1832 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1833 if (ret) {
1834 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1835 goto fail_mr_status;
1836 }
1837
1838 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1839 u64 sec_offset_err;
1840 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1841
1842 switch (mr_status.sig_err.err_type) {
1843 case IB_SIG_BAD_GUARD:
1844 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1845 break;
1846 case IB_SIG_BAD_REFTAG:
1847 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1848 break;
1849 case IB_SIG_BAD_APPTAG:
1850 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1851 break;
1852 }
1853 sec_offset_err = mr_status.sig_err.sig_err_offset;
1854 do_div(sec_offset_err, block_size);
1855 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1856
1857 pr_err("isert: PI error found type %d at sector 0x%llx "
1858 "expected 0x%x vs actual 0x%x\n",
1859 mr_status.sig_err.err_type,
1860 (unsigned long long)se_cmd->bad_sector,
1861 mr_status.sig_err.expected,
1862 mr_status.sig_err.actual);
1863 ret = 1;
1864 }
1865
1866 fail_mr_status:
1867 return ret;
1868 }
1869
1870 static void
isert_completion_rdma_write(struct iser_tx_desc * tx_desc,struct isert_cmd * isert_cmd)1871 isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1872 struct isert_cmd *isert_cmd)
1873 {
1874 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1875 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1876 struct se_cmd *se_cmd = &cmd->se_cmd;
1877 struct isert_conn *isert_conn = isert_cmd->conn;
1878 struct isert_device *device = isert_conn->conn_device;
1879 int ret = 0;
1880
1881 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
1882 ret = isert_check_pi_status(se_cmd,
1883 wr->fr_desc->pi_ctx->sig_mr);
1884 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1885 }
1886
1887 device->unreg_rdma_mem(isert_cmd, isert_conn);
1888 wr->send_wr_num = 0;
1889 if (ret)
1890 transport_send_check_condition_and_sense(se_cmd,
1891 se_cmd->pi_err, 0);
1892 else
1893 isert_put_response(isert_conn->conn, cmd);
1894 }
1895
1896 static void
isert_completion_rdma_read(struct iser_tx_desc * tx_desc,struct isert_cmd * isert_cmd)1897 isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1898 struct isert_cmd *isert_cmd)
1899 {
1900 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1901 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1902 struct se_cmd *se_cmd = &cmd->se_cmd;
1903 struct isert_conn *isert_conn = isert_cmd->conn;
1904 struct isert_device *device = isert_conn->conn_device;
1905 int ret = 0;
1906
1907 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
1908 ret = isert_check_pi_status(se_cmd,
1909 wr->fr_desc->pi_ctx->sig_mr);
1910 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1911 }
1912
1913 iscsit_stop_dataout_timer(cmd);
1914 device->unreg_rdma_mem(isert_cmd, isert_conn);
1915 cmd->write_data_done = wr->data.len;
1916 wr->send_wr_num = 0;
1917
1918 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1919 spin_lock_bh(&cmd->istate_lock);
1920 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1921 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1922 spin_unlock_bh(&cmd->istate_lock);
1923
1924 if (ret) {
1925 target_put_sess_cmd(se_cmd);
1926 transport_send_check_condition_and_sense(se_cmd,
1927 se_cmd->pi_err, 0);
1928 } else {
1929 target_execute_cmd(se_cmd);
1930 }
1931 }
1932
1933 static void
isert_do_control_comp(struct work_struct * work)1934 isert_do_control_comp(struct work_struct *work)
1935 {
1936 struct isert_cmd *isert_cmd = container_of(work,
1937 struct isert_cmd, comp_work);
1938 struct isert_conn *isert_conn = isert_cmd->conn;
1939 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1940 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1941
1942 switch (cmd->i_state) {
1943 case ISTATE_SEND_TASKMGTRSP:
1944 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1945
1946 atomic_dec(&isert_conn->post_send_buf_count);
1947 iscsit_tmr_post_handler(cmd, cmd->conn);
1948
1949 cmd->i_state = ISTATE_SENT_STATUS;
1950 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1951 break;
1952 case ISTATE_SEND_REJECT:
1953 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1954 atomic_dec(&isert_conn->post_send_buf_count);
1955
1956 cmd->i_state = ISTATE_SENT_STATUS;
1957 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1958 break;
1959 case ISTATE_SEND_LOGOUTRSP:
1960 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1961
1962 atomic_dec(&isert_conn->post_send_buf_count);
1963 iscsit_logout_post_handler(cmd, cmd->conn);
1964 break;
1965 case ISTATE_SEND_TEXTRSP:
1966 atomic_dec(&isert_conn->post_send_buf_count);
1967 cmd->i_state = ISTATE_SENT_STATUS;
1968 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1969 break;
1970 default:
1971 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1972 dump_stack();
1973 break;
1974 }
1975 }
1976
1977 static void
isert_response_completion(struct iser_tx_desc * tx_desc,struct isert_cmd * isert_cmd,struct isert_conn * isert_conn,struct ib_device * ib_dev)1978 isert_response_completion(struct iser_tx_desc *tx_desc,
1979 struct isert_cmd *isert_cmd,
1980 struct isert_conn *isert_conn,
1981 struct ib_device *ib_dev)
1982 {
1983 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1984 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1985
1986 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
1987 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1988 cmd->i_state == ISTATE_SEND_REJECT ||
1989 cmd->i_state == ISTATE_SEND_TEXTRSP) {
1990 isert_unmap_tx_desc(tx_desc, ib_dev);
1991
1992 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1993 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1994 return;
1995 }
1996
1997 /**
1998 * If send_wr_num is 0 this means that we got
1999 * RDMA completion and we cleared it and we should
2000 * simply decrement the response post. else the
2001 * response is incorporated in send_wr_num, just
2002 * sub it.
2003 **/
2004 if (wr->send_wr_num)
2005 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2006 else
2007 atomic_dec(&isert_conn->post_send_buf_count);
2008
2009 cmd->i_state = ISTATE_SENT_STATUS;
2010 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
2011 }
2012
2013 static void
__isert_send_completion(struct iser_tx_desc * tx_desc,struct isert_conn * isert_conn)2014 __isert_send_completion(struct iser_tx_desc *tx_desc,
2015 struct isert_conn *isert_conn)
2016 {
2017 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2018 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2019 struct isert_rdma_wr *wr;
2020
2021 if (!isert_cmd) {
2022 atomic_dec(&isert_conn->post_send_buf_count);
2023 isert_unmap_tx_desc(tx_desc, ib_dev);
2024 return;
2025 }
2026 wr = &isert_cmd->rdma_wr;
2027
2028 switch (wr->iser_ib_op) {
2029 case ISER_IB_RECV:
2030 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
2031 dump_stack();
2032 break;
2033 case ISER_IB_SEND:
2034 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
2035 isert_response_completion(tx_desc, isert_cmd,
2036 isert_conn, ib_dev);
2037 break;
2038 case ISER_IB_RDMA_WRITE:
2039 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
2040 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2041 isert_completion_rdma_write(tx_desc, isert_cmd);
2042 break;
2043 case ISER_IB_RDMA_READ:
2044 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
2045
2046 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2047 isert_completion_rdma_read(tx_desc, isert_cmd);
2048 break;
2049 default:
2050 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
2051 dump_stack();
2052 break;
2053 }
2054 }
2055
2056 static void
isert_send_completion(struct iser_tx_desc * tx_desc,struct isert_conn * isert_conn)2057 isert_send_completion(struct iser_tx_desc *tx_desc,
2058 struct isert_conn *isert_conn)
2059 {
2060 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2061 struct iser_tx_desc *t;
2062 /*
2063 * Drain coalesced completion llist starting from comp_llnode_batch
2064 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
2065 */
2066 while (llnode) {
2067 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2068 llnode = llist_next(llnode);
2069 __isert_send_completion(t, isert_conn);
2070 }
2071 __isert_send_completion(tx_desc, isert_conn);
2072 }
2073
2074 static void
isert_cq_drain_comp_llist(struct isert_conn * isert_conn,struct ib_device * ib_dev)2075 isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
2076 {
2077 struct llist_node *llnode;
2078 struct isert_rdma_wr *wr;
2079 struct iser_tx_desc *t;
2080
2081 mutex_lock(&isert_conn->conn_mutex);
2082 llnode = llist_del_all(&isert_conn->conn_comp_llist);
2083 isert_conn->conn_comp_batch = 0;
2084 mutex_unlock(&isert_conn->conn_mutex);
2085
2086 while (llnode) {
2087 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2088 llnode = llist_next(llnode);
2089 wr = &t->isert_cmd->rdma_wr;
2090
2091 /**
2092 * If send_wr_num is 0 this means that we got
2093 * RDMA completion and we cleared it and we should
2094 * simply decrement the response post. else the
2095 * response is incorporated in send_wr_num, just
2096 * sub it.
2097 **/
2098 if (wr->send_wr_num)
2099 atomic_sub(wr->send_wr_num,
2100 &isert_conn->post_send_buf_count);
2101 else
2102 atomic_dec(&isert_conn->post_send_buf_count);
2103
2104 isert_completion_put(t, t->isert_cmd, ib_dev, true);
2105 }
2106 }
2107
2108 static void
isert_cq_tx_comp_err(struct iser_tx_desc * tx_desc,struct isert_conn * isert_conn)2109 isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
2110 {
2111 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2112 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2113 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2114 struct isert_rdma_wr *wr;
2115 struct iser_tx_desc *t;
2116
2117 while (llnode) {
2118 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2119 llnode = llist_next(llnode);
2120 wr = &t->isert_cmd->rdma_wr;
2121
2122 /**
2123 * If send_wr_num is 0 this means that we got
2124 * RDMA completion and we cleared it and we should
2125 * simply decrement the response post. else the
2126 * response is incorporated in send_wr_num, just
2127 * sub it.
2128 **/
2129 if (wr->send_wr_num)
2130 atomic_sub(wr->send_wr_num,
2131 &isert_conn->post_send_buf_count);
2132 else
2133 atomic_dec(&isert_conn->post_send_buf_count);
2134
2135 isert_completion_put(t, t->isert_cmd, ib_dev, true);
2136 }
2137 tx_desc->comp_llnode_batch = NULL;
2138
2139 if (!isert_cmd)
2140 isert_unmap_tx_desc(tx_desc, ib_dev);
2141 else
2142 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2143 }
2144
2145 static void
isert_cq_rx_comp_err(struct isert_conn * isert_conn)2146 isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2147 {
2148 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2149 struct iscsi_conn *conn = isert_conn->conn;
2150
2151 if (isert_conn->post_recv_buf_count)
2152 return;
2153
2154 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2155
2156 if (conn->sess) {
2157 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2158 target_wait_for_sess_cmds(conn->sess->se_sess);
2159 }
2160
2161 while (atomic_read(&isert_conn->post_send_buf_count))
2162 msleep(3000);
2163
2164 mutex_lock(&isert_conn->conn_mutex);
2165 isert_conn_terminate(isert_conn);
2166 mutex_unlock(&isert_conn->conn_mutex);
2167
2168 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2169
2170 complete(&isert_conn->conn_wait_comp_err);
2171 }
2172
2173 static void
isert_cq_tx_work(struct work_struct * work)2174 isert_cq_tx_work(struct work_struct *work)
2175 {
2176 struct isert_cq_desc *cq_desc = container_of(work,
2177 struct isert_cq_desc, cq_tx_work);
2178 struct isert_device *device = cq_desc->device;
2179 int cq_index = cq_desc->cq_index;
2180 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2181 struct isert_conn *isert_conn;
2182 struct iser_tx_desc *tx_desc;
2183 struct ib_wc wc;
2184
2185 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2186 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2187 isert_conn = wc.qp->qp_context;
2188
2189 if (wc.status == IB_WC_SUCCESS) {
2190 isert_send_completion(tx_desc, isert_conn);
2191 } else {
2192 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2193 pr_debug("TX wc.status: 0x%08x\n", wc.status);
2194 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2195
2196 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2197 if (tx_desc->llnode_active)
2198 continue;
2199
2200 atomic_dec(&isert_conn->post_send_buf_count);
2201 isert_cq_tx_comp_err(tx_desc, isert_conn);
2202 }
2203 }
2204 }
2205
2206 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2207 }
2208
2209 static void
isert_cq_tx_callback(struct ib_cq * cq,void * context)2210 isert_cq_tx_callback(struct ib_cq *cq, void *context)
2211 {
2212 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2213
2214 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2215 }
2216
2217 static void
isert_cq_rx_work(struct work_struct * work)2218 isert_cq_rx_work(struct work_struct *work)
2219 {
2220 struct isert_cq_desc *cq_desc = container_of(work,
2221 struct isert_cq_desc, cq_rx_work);
2222 struct isert_device *device = cq_desc->device;
2223 int cq_index = cq_desc->cq_index;
2224 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2225 struct isert_conn *isert_conn;
2226 struct iser_rx_desc *rx_desc;
2227 struct ib_wc wc;
2228 unsigned long xfer_len;
2229
2230 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2231 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2232 isert_conn = wc.qp->qp_context;
2233
2234 if (wc.status == IB_WC_SUCCESS) {
2235 xfer_len = (unsigned long)wc.byte_len;
2236 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2237 } else {
2238 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2239 if (wc.status != IB_WC_WR_FLUSH_ERR) {
2240 pr_debug("RX wc.status: 0x%08x\n", wc.status);
2241 pr_debug("RX wc.vendor_err: 0x%08x\n",
2242 wc.vendor_err);
2243 }
2244 isert_conn->post_recv_buf_count--;
2245 isert_cq_rx_comp_err(isert_conn);
2246 }
2247 }
2248
2249 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2250 }
2251
2252 static void
isert_cq_rx_callback(struct ib_cq * cq,void * context)2253 isert_cq_rx_callback(struct ib_cq *cq, void *context)
2254 {
2255 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2256
2257 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2258 }
2259
2260 static int
isert_post_response(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd)2261 isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2262 {
2263 struct ib_send_wr *wr_failed;
2264 int ret;
2265
2266 atomic_inc(&isert_conn->post_send_buf_count);
2267
2268 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2269 &wr_failed);
2270 if (ret) {
2271 pr_err("ib_post_send failed with %d\n", ret);
2272 atomic_dec(&isert_conn->post_send_buf_count);
2273 return ret;
2274 }
2275 return ret;
2276 }
2277
2278 static int
isert_put_response(struct iscsi_conn * conn,struct iscsi_cmd * cmd)2279 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2280 {
2281 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2282 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2283 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2284 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2285 &isert_cmd->tx_desc.iscsi_header;
2286
2287 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2288 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2289 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2290 /*
2291 * Attach SENSE DATA payload to iSCSI Response PDU
2292 */
2293 if (cmd->se_cmd.sense_buffer &&
2294 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2295 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2296 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2297 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2298 u32 padding, pdu_len;
2299
2300 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2301 cmd->sense_buffer);
2302 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2303
2304 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2305 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2306 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2307
2308 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2309 (void *)cmd->sense_buffer, pdu_len,
2310 DMA_TO_DEVICE);
2311
2312 isert_cmd->pdu_buf_len = pdu_len;
2313 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2314 tx_dsg->length = pdu_len;
2315 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2316 isert_cmd->tx_desc.num_sge = 2;
2317 }
2318
2319 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2320
2321 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2322
2323 return isert_post_response(isert_conn, isert_cmd);
2324 }
2325
2326 static void
isert_aborted_task(struct iscsi_conn * conn,struct iscsi_cmd * cmd)2327 isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2328 {
2329 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2330 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2331 struct isert_device *device = isert_conn->conn_device;
2332
2333 spin_lock_bh(&conn->cmd_lock);
2334 if (!list_empty(&cmd->i_conn_node))
2335 list_del_init(&cmd->i_conn_node);
2336 spin_unlock_bh(&conn->cmd_lock);
2337
2338 if (cmd->data_direction == DMA_TO_DEVICE)
2339 iscsit_stop_dataout_timer(cmd);
2340
2341 device->unreg_rdma_mem(isert_cmd, isert_conn);
2342 }
2343
2344 static enum target_prot_op
isert_get_sup_prot_ops(struct iscsi_conn * conn)2345 isert_get_sup_prot_ops(struct iscsi_conn *conn)
2346 {
2347 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2348 struct isert_device *device = isert_conn->conn_device;
2349
2350 if (conn->tpg->tpg_attrib.t10_pi) {
2351 if (device->pi_capable) {
2352 pr_info("conn %p PI offload enabled\n", isert_conn);
2353 isert_conn->pi_support = true;
2354 return TARGET_PROT_ALL;
2355 }
2356 }
2357
2358 pr_info("conn %p PI offload disabled\n", isert_conn);
2359 isert_conn->pi_support = false;
2360
2361 return TARGET_PROT_NORMAL;
2362 }
2363
2364 static int
isert_put_nopin(struct iscsi_cmd * cmd,struct iscsi_conn * conn,bool nopout_response)2365 isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2366 bool nopout_response)
2367 {
2368 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2369 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2370 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2371
2372 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2373 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2374 &isert_cmd->tx_desc.iscsi_header,
2375 nopout_response);
2376 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2377 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2378
2379 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2380
2381 return isert_post_response(isert_conn, isert_cmd);
2382 }
2383
2384 static int
isert_put_logout_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2385 isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2386 {
2387 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2388 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2389 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2390
2391 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2392 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2393 &isert_cmd->tx_desc.iscsi_header);
2394 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2395 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2396
2397 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2398
2399 return isert_post_response(isert_conn, isert_cmd);
2400 }
2401
2402 static int
isert_put_tm_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2403 isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2404 {
2405 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2406 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2407 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2408
2409 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2410 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2411 &isert_cmd->tx_desc.iscsi_header);
2412 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2413 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2414
2415 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2416
2417 return isert_post_response(isert_conn, isert_cmd);
2418 }
2419
2420 static int
isert_put_reject(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2421 isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2422 {
2423 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2424 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2425 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2426 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2427 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2428 struct iscsi_reject *hdr =
2429 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2430
2431 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2432 iscsit_build_reject(cmd, conn, hdr);
2433 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2434
2435 hton24(hdr->dlength, ISCSI_HDR_LEN);
2436 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2437 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2438 DMA_TO_DEVICE);
2439 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2440 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2441 tx_dsg->length = ISCSI_HDR_LEN;
2442 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2443 isert_cmd->tx_desc.num_sge = 2;
2444
2445 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2446
2447 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2448
2449 return isert_post_response(isert_conn, isert_cmd);
2450 }
2451
2452 static int
isert_put_text_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2453 isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2454 {
2455 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2456 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2457 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2458 struct iscsi_text_rsp *hdr =
2459 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2460 u32 txt_rsp_len;
2461 int rc;
2462
2463 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2464 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2465 if (rc < 0)
2466 return rc;
2467
2468 txt_rsp_len = rc;
2469 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2470
2471 if (txt_rsp_len) {
2472 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2473 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2474 void *txt_rsp_buf = cmd->buf_ptr;
2475
2476 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2477 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2478
2479 isert_cmd->pdu_buf_len = txt_rsp_len;
2480 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2481 tx_dsg->length = txt_rsp_len;
2482 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2483 isert_cmd->tx_desc.num_sge = 2;
2484 }
2485 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2486
2487 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2488
2489 return isert_post_response(isert_conn, isert_cmd);
2490 }
2491
2492 static int
isert_build_rdma_wr(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct ib_sge * ib_sge,struct ib_send_wr * send_wr,u32 data_left,u32 offset)2493 isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2494 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2495 u32 data_left, u32 offset)
2496 {
2497 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2498 struct scatterlist *sg_start, *tmp_sg;
2499 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2500 u32 sg_off, page_off;
2501 int i = 0, sg_nents;
2502
2503 sg_off = offset / PAGE_SIZE;
2504 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2505 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2506 page_off = offset % PAGE_SIZE;
2507
2508 send_wr->sg_list = ib_sge;
2509 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2510 /*
2511 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2512 */
2513 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2514 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2515 (unsigned long long)tmp_sg->dma_address,
2516 tmp_sg->length, page_off);
2517
2518 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2519 ib_sge->length = min_t(u32, data_left,
2520 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2521 ib_sge->lkey = isert_conn->conn_mr->lkey;
2522
2523 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2524 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2525 page_off = 0;
2526 data_left -= ib_sge->length;
2527 if (!data_left)
2528 break;
2529 ib_sge++;
2530 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2531 }
2532
2533 send_wr->num_sge = ++i;
2534 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2535 send_wr->sg_list, send_wr->num_sge);
2536
2537 return send_wr->num_sge;
2538 }
2539
2540 static int
isert_map_rdma(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct isert_rdma_wr * wr)2541 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2542 struct isert_rdma_wr *wr)
2543 {
2544 struct se_cmd *se_cmd = &cmd->se_cmd;
2545 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2546 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2547 struct isert_data_buf *data = &wr->data;
2548 struct ib_send_wr *send_wr;
2549 struct ib_sge *ib_sge;
2550 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2551 int ret = 0, i, ib_sge_cnt;
2552
2553 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2554
2555 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2556 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2557 se_cmd->t_data_nents, se_cmd->data_length,
2558 offset, wr->iser_ib_op, &wr->data);
2559 if (ret)
2560 return ret;
2561
2562 data_left = data->len;
2563 offset = data->offset;
2564
2565 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2566 if (!ib_sge) {
2567 pr_warn("Unable to allocate ib_sge\n");
2568 ret = -ENOMEM;
2569 goto unmap_cmd;
2570 }
2571 wr->ib_sge = ib_sge;
2572
2573 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2574 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2575 GFP_KERNEL);
2576 if (!wr->send_wr) {
2577 pr_debug("Unable to allocate wr->send_wr\n");
2578 ret = -ENOMEM;
2579 goto unmap_cmd;
2580 }
2581
2582 wr->isert_cmd = isert_cmd;
2583 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2584
2585 for (i = 0; i < wr->send_wr_num; i++) {
2586 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2587 data_len = min(data_left, rdma_write_max);
2588
2589 send_wr->send_flags = 0;
2590 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2591 send_wr->opcode = IB_WR_RDMA_WRITE;
2592 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2593 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2594 if (i + 1 == wr->send_wr_num)
2595 send_wr->next = &isert_cmd->tx_desc.send_wr;
2596 else
2597 send_wr->next = &wr->send_wr[i + 1];
2598 } else {
2599 send_wr->opcode = IB_WR_RDMA_READ;
2600 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2601 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2602 if (i + 1 == wr->send_wr_num)
2603 send_wr->send_flags = IB_SEND_SIGNALED;
2604 else
2605 send_wr->next = &wr->send_wr[i + 1];
2606 }
2607
2608 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2609 send_wr, data_len, offset);
2610 ib_sge += ib_sge_cnt;
2611
2612 offset += data_len;
2613 va_offset += data_len;
2614 data_left -= data_len;
2615 }
2616
2617 return 0;
2618 unmap_cmd:
2619 isert_unmap_data_buf(isert_conn, data);
2620
2621 return ret;
2622 }
2623
2624 static int
isert_map_fr_pagelist(struct ib_device * ib_dev,struct scatterlist * sg_start,int sg_nents,u64 * fr_pl)2625 isert_map_fr_pagelist(struct ib_device *ib_dev,
2626 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2627 {
2628 u64 start_addr, end_addr, page, chunk_start = 0;
2629 struct scatterlist *tmp_sg;
2630 int i = 0, new_chunk, last_ent, n_pages;
2631
2632 n_pages = 0;
2633 new_chunk = 1;
2634 last_ent = sg_nents - 1;
2635 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2636 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2637 if (new_chunk)
2638 chunk_start = start_addr;
2639 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2640
2641 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2642 i, (unsigned long long)tmp_sg->dma_address,
2643 tmp_sg->length);
2644
2645 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2646 new_chunk = 0;
2647 continue;
2648 }
2649 new_chunk = 1;
2650
2651 page = chunk_start & PAGE_MASK;
2652 do {
2653 fr_pl[n_pages++] = page;
2654 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2655 n_pages - 1, page);
2656 page += PAGE_SIZE;
2657 } while (page < end_addr);
2658 }
2659
2660 return n_pages;
2661 }
2662
2663 static int
isert_fast_reg_mr(struct isert_conn * isert_conn,struct fast_reg_descriptor * fr_desc,struct isert_data_buf * mem,enum isert_indicator ind,struct ib_sge * sge)2664 isert_fast_reg_mr(struct isert_conn *isert_conn,
2665 struct fast_reg_descriptor *fr_desc,
2666 struct isert_data_buf *mem,
2667 enum isert_indicator ind,
2668 struct ib_sge *sge)
2669 {
2670 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2671 struct ib_mr *mr;
2672 struct ib_fast_reg_page_list *frpl;
2673 struct ib_send_wr fr_wr, inv_wr;
2674 struct ib_send_wr *bad_wr, *wr = NULL;
2675 int ret, pagelist_len;
2676 u32 page_off;
2677 u8 key;
2678
2679 if (mem->dma_nents == 1) {
2680 sge->lkey = isert_conn->conn_mr->lkey;
2681 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2682 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
2683 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2684 __func__, __LINE__, sge->addr, sge->length,
2685 sge->lkey);
2686 return 0;
2687 }
2688
2689 if (ind == ISERT_DATA_KEY_VALID) {
2690 /* Registering data buffer */
2691 mr = fr_desc->data_mr;
2692 frpl = fr_desc->data_frpl;
2693 } else {
2694 /* Registering protection buffer */
2695 mr = fr_desc->pi_ctx->prot_mr;
2696 frpl = fr_desc->pi_ctx->prot_frpl;
2697 }
2698
2699 page_off = mem->offset % PAGE_SIZE;
2700
2701 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2702 fr_desc, mem->nents, mem->offset);
2703
2704 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
2705 &frpl->page_list[0]);
2706
2707 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
2708 memset(&inv_wr, 0, sizeof(inv_wr));
2709 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
2710 inv_wr.opcode = IB_WR_LOCAL_INV;
2711 inv_wr.ex.invalidate_rkey = mr->rkey;
2712 wr = &inv_wr;
2713 /* Bump the key */
2714 key = (u8)(mr->rkey & 0x000000FF);
2715 ib_update_fast_reg_key(mr, ++key);
2716 }
2717
2718 /* Prepare FASTREG WR */
2719 memset(&fr_wr, 0, sizeof(fr_wr));
2720 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
2721 fr_wr.opcode = IB_WR_FAST_REG_MR;
2722 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2723 fr_wr.wr.fast_reg.page_list = frpl;
2724 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2725 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2726 fr_wr.wr.fast_reg.length = mem->len;
2727 fr_wr.wr.fast_reg.rkey = mr->rkey;
2728 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2729
2730 if (!wr)
2731 wr = &fr_wr;
2732 else
2733 wr->next = &fr_wr;
2734
2735 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2736 if (ret) {
2737 pr_err("fast registration failed, ret:%d\n", ret);
2738 return ret;
2739 }
2740 fr_desc->ind &= ~ind;
2741
2742 sge->lkey = mr->lkey;
2743 sge->addr = frpl->page_list[0] + page_off;
2744 sge->length = mem->len;
2745
2746 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2747 __func__, __LINE__, sge->addr, sge->length,
2748 sge->lkey);
2749
2750 return ret;
2751 }
2752
2753 static inline void
isert_set_dif_domain(struct se_cmd * se_cmd,struct ib_sig_attrs * sig_attrs,struct ib_sig_domain * domain)2754 isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2755 struct ib_sig_domain *domain)
2756 {
2757 domain->sig_type = IB_SIG_TYPE_T10_DIF;
2758 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2759 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2760 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
2761 /*
2762 * At the moment we hard code those, but if in the future
2763 * the target core would like to use it, we will take it
2764 * from se_cmd.
2765 */
2766 domain->sig.dif.apptag_check_mask = 0xffff;
2767 domain->sig.dif.app_escape = true;
2768 domain->sig.dif.ref_escape = true;
2769 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2770 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2771 domain->sig.dif.ref_remap = true;
2772 };
2773
2774 static int
isert_set_sig_attrs(struct se_cmd * se_cmd,struct ib_sig_attrs * sig_attrs)2775 isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2776 {
2777 switch (se_cmd->prot_op) {
2778 case TARGET_PROT_DIN_INSERT:
2779 case TARGET_PROT_DOUT_STRIP:
2780 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
2781 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2782 break;
2783 case TARGET_PROT_DOUT_INSERT:
2784 case TARGET_PROT_DIN_STRIP:
2785 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
2786 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
2787 break;
2788 case TARGET_PROT_DIN_PASS:
2789 case TARGET_PROT_DOUT_PASS:
2790 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2791 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
2792 break;
2793 default:
2794 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2795 return -EINVAL;
2796 }
2797
2798 return 0;
2799 }
2800
2801 static inline u8
isert_set_prot_checks(u8 prot_checks)2802 isert_set_prot_checks(u8 prot_checks)
2803 {
2804 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2805 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2806 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2807 }
2808
2809 static int
isert_reg_sig_mr(struct isert_conn * isert_conn,struct se_cmd * se_cmd,struct isert_rdma_wr * rdma_wr,struct fast_reg_descriptor * fr_desc)2810 isert_reg_sig_mr(struct isert_conn *isert_conn,
2811 struct se_cmd *se_cmd,
2812 struct isert_rdma_wr *rdma_wr,
2813 struct fast_reg_descriptor *fr_desc)
2814 {
2815 struct ib_send_wr sig_wr, inv_wr;
2816 struct ib_send_wr *bad_wr, *wr = NULL;
2817 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2818 struct ib_sig_attrs sig_attrs;
2819 int ret;
2820 u32 key;
2821
2822 memset(&sig_attrs, 0, sizeof(sig_attrs));
2823 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2824 if (ret)
2825 goto err;
2826
2827 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2828
2829 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2830 memset(&inv_wr, 0, sizeof(inv_wr));
2831 inv_wr.opcode = IB_WR_LOCAL_INV;
2832 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
2833 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2834 wr = &inv_wr;
2835 /* Bump the key */
2836 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2837 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2838 }
2839
2840 memset(&sig_wr, 0, sizeof(sig_wr));
2841 sig_wr.opcode = IB_WR_REG_SIG_MR;
2842 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2843 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
2844 sig_wr.num_sge = 1;
2845 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2846 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2847 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2848 if (se_cmd->t_prot_sg)
2849 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
2850
2851 if (!wr)
2852 wr = &sig_wr;
2853 else
2854 wr->next = &sig_wr;
2855
2856 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2857 if (ret) {
2858 pr_err("fast registration failed, ret:%d\n", ret);
2859 goto err;
2860 }
2861 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2862
2863 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2864 rdma_wr->ib_sg[SIG].addr = 0;
2865 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
2866 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2867 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2868 /*
2869 * We have protection guards on the wire
2870 * so we need to set a larget transfer
2871 */
2872 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
2873
2874 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2875 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2876 rdma_wr->ib_sg[SIG].lkey);
2877 err:
2878 return ret;
2879 }
2880
2881 static int
isert_handle_prot_cmd(struct isert_conn * isert_conn,struct isert_cmd * isert_cmd,struct isert_rdma_wr * wr)2882 isert_handle_prot_cmd(struct isert_conn *isert_conn,
2883 struct isert_cmd *isert_cmd,
2884 struct isert_rdma_wr *wr)
2885 {
2886 struct isert_device *device = isert_conn->conn_device;
2887 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2888 int ret;
2889
2890 if (!wr->fr_desc->pi_ctx) {
2891 ret = isert_create_pi_ctx(wr->fr_desc,
2892 device->ib_device,
2893 isert_conn->conn_pd);
2894 if (ret) {
2895 pr_err("conn %p failed to allocate pi_ctx\n",
2896 isert_conn);
2897 return ret;
2898 }
2899 }
2900
2901 if (se_cmd->t_prot_sg) {
2902 ret = isert_map_data_buf(isert_conn, isert_cmd,
2903 se_cmd->t_prot_sg,
2904 se_cmd->t_prot_nents,
2905 se_cmd->prot_length,
2906 0, wr->iser_ib_op, &wr->prot);
2907 if (ret) {
2908 pr_err("conn %p failed to map protection buffer\n",
2909 isert_conn);
2910 return ret;
2911 }
2912
2913 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2914 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2915 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2916 if (ret) {
2917 pr_err("conn %p failed to fast reg mr\n",
2918 isert_conn);
2919 goto unmap_prot_cmd;
2920 }
2921 }
2922
2923 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2924 if (ret) {
2925 pr_err("conn %p failed to fast reg mr\n",
2926 isert_conn);
2927 goto unmap_prot_cmd;
2928 }
2929 wr->fr_desc->ind |= ISERT_PROTECTED;
2930
2931 return 0;
2932
2933 unmap_prot_cmd:
2934 if (se_cmd->t_prot_sg)
2935 isert_unmap_data_buf(isert_conn, &wr->prot);
2936
2937 return ret;
2938 }
2939
2940 static int
isert_reg_rdma(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct isert_rdma_wr * wr)2941 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2942 struct isert_rdma_wr *wr)
2943 {
2944 struct se_cmd *se_cmd = &cmd->se_cmd;
2945 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2946 struct isert_conn *isert_conn = conn->context;
2947 struct fast_reg_descriptor *fr_desc = NULL;
2948 struct ib_send_wr *send_wr;
2949 struct ib_sge *ib_sg;
2950 u32 offset;
2951 int ret = 0;
2952 unsigned long flags;
2953
2954 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2955
2956 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2957 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2958 se_cmd->t_data_nents, se_cmd->data_length,
2959 offset, wr->iser_ib_op, &wr->data);
2960 if (ret)
2961 return ret;
2962
2963 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2964 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2965 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2966 struct fast_reg_descriptor, list);
2967 list_del(&fr_desc->list);
2968 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2969 wr->fr_desc = fr_desc;
2970 }
2971
2972 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2973 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2974 if (ret)
2975 goto unmap_cmd;
2976
2977 if (isert_prot_cmd(isert_conn, se_cmd)) {
2978 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
2979 if (ret)
2980 goto unmap_cmd;
2981
2982 ib_sg = &wr->ib_sg[SIG];
2983 } else {
2984 ib_sg = &wr->ib_sg[DATA];
2985 }
2986
2987 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2988 wr->ib_sge = &wr->s_ib_sge;
2989 wr->send_wr_num = 1;
2990 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2991 wr->send_wr = &wr->s_send_wr;
2992 wr->isert_cmd = isert_cmd;
2993
2994 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2995 send_wr->sg_list = &wr->s_ib_sge;
2996 send_wr->num_sge = 1;
2997 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2998 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2999 send_wr->opcode = IB_WR_RDMA_WRITE;
3000 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
3001 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
3002 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
3003 0 : IB_SEND_SIGNALED;
3004 } else {
3005 send_wr->opcode = IB_WR_RDMA_READ;
3006 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
3007 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
3008 send_wr->send_flags = IB_SEND_SIGNALED;
3009 }
3010
3011 return 0;
3012
3013 unmap_cmd:
3014 if (fr_desc) {
3015 spin_lock_irqsave(&isert_conn->conn_lock, flags);
3016 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
3017 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
3018 }
3019 isert_unmap_data_buf(isert_conn, &wr->data);
3020
3021 return ret;
3022 }
3023
3024 static int
isert_put_datain(struct iscsi_conn * conn,struct iscsi_cmd * cmd)3025 isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
3026 {
3027 struct se_cmd *se_cmd = &cmd->se_cmd;
3028 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3029 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
3030 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3031 struct isert_device *device = isert_conn->conn_device;
3032 struct ib_send_wr *wr_failed;
3033 int rc;
3034
3035 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
3036 isert_cmd, se_cmd->data_length);
3037 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
3038 rc = device->reg_rdma_mem(conn, cmd, wr);
3039 if (rc) {
3040 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
3041 return rc;
3042 }
3043
3044 if (!isert_prot_cmd(isert_conn, se_cmd)) {
3045 /*
3046 * Build isert_conn->tx_desc for iSCSI response PDU and attach
3047 */
3048 isert_create_send_desc(isert_conn, isert_cmd,
3049 &isert_cmd->tx_desc);
3050 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
3051 &isert_cmd->tx_desc.iscsi_header);
3052 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
3053 isert_init_send_wr(isert_conn, isert_cmd,
3054 &isert_cmd->tx_desc.send_wr, false);
3055 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
3056 wr->send_wr_num += 1;
3057 }
3058
3059 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3060
3061 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3062 if (rc) {
3063 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
3064 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3065 }
3066
3067 if (!isert_prot_cmd(isert_conn, se_cmd))
3068 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
3069 "READ\n", isert_cmd);
3070 else
3071 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
3072 isert_cmd);
3073
3074 return 1;
3075 }
3076
3077 static int
isert_get_dataout(struct iscsi_conn * conn,struct iscsi_cmd * cmd,bool recovery)3078 isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
3079 {
3080 struct se_cmd *se_cmd = &cmd->se_cmd;
3081 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3082 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
3083 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3084 struct isert_device *device = isert_conn->conn_device;
3085 struct ib_send_wr *wr_failed;
3086 int rc;
3087
3088 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
3089 isert_cmd, se_cmd->data_length, cmd->write_data_done);
3090 wr->iser_ib_op = ISER_IB_RDMA_READ;
3091 rc = device->reg_rdma_mem(conn, cmd, wr);
3092 if (rc) {
3093 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
3094 return rc;
3095 }
3096
3097 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3098
3099 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3100 if (rc) {
3101 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
3102 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3103 }
3104 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
3105 isert_cmd);
3106
3107 return 0;
3108 }
3109
3110 static int
isert_immediate_queue(struct iscsi_conn * conn,struct iscsi_cmd * cmd,int state)3111 isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3112 {
3113 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3114 int ret = 0;
3115
3116 switch (state) {
3117 case ISTATE_REMOVE:
3118 spin_lock_bh(&conn->cmd_lock);
3119 list_del_init(&cmd->i_conn_node);
3120 spin_unlock_bh(&conn->cmd_lock);
3121 isert_put_cmd(isert_cmd, true);
3122 break;
3123 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3124 ret = isert_put_nopin(cmd, conn, false);
3125 break;
3126 default:
3127 pr_err("Unknown immediate state: 0x%02x\n", state);
3128 ret = -EINVAL;
3129 break;
3130 }
3131
3132 return ret;
3133 }
3134
3135 static int
isert_response_queue(struct iscsi_conn * conn,struct iscsi_cmd * cmd,int state)3136 isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3137 {
3138 int ret;
3139
3140 switch (state) {
3141 case ISTATE_SEND_LOGOUTRSP:
3142 ret = isert_put_logout_rsp(cmd, conn);
3143 if (!ret) {
3144 pr_debug("Returning iSER Logout -EAGAIN\n");
3145 ret = -EAGAIN;
3146 }
3147 break;
3148 case ISTATE_SEND_NOPIN:
3149 ret = isert_put_nopin(cmd, conn, true);
3150 break;
3151 case ISTATE_SEND_TASKMGTRSP:
3152 ret = isert_put_tm_rsp(cmd, conn);
3153 break;
3154 case ISTATE_SEND_REJECT:
3155 ret = isert_put_reject(cmd, conn);
3156 break;
3157 case ISTATE_SEND_TEXTRSP:
3158 ret = isert_put_text_rsp(cmd, conn);
3159 break;
3160 case ISTATE_SEND_STATUS:
3161 /*
3162 * Special case for sending non GOOD SCSI status from TX thread
3163 * context during pre se_cmd excecution failure.
3164 */
3165 ret = isert_put_response(conn, cmd);
3166 break;
3167 default:
3168 pr_err("Unknown response state: 0x%02x\n", state);
3169 ret = -EINVAL;
3170 break;
3171 }
3172
3173 return ret;
3174 }
3175
3176 struct rdma_cm_id *
isert_setup_id(struct isert_np * isert_np)3177 isert_setup_id(struct isert_np *isert_np)
3178 {
3179 struct iscsi_np *np = isert_np->np;
3180 struct rdma_cm_id *id;
3181 struct sockaddr *sa;
3182 int ret;
3183
3184 sa = (struct sockaddr *)&np->np_sockaddr;
3185 pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3186
3187 id = rdma_create_id(isert_cma_handler, isert_np,
3188 RDMA_PS_TCP, IB_QPT_RC);
3189 if (IS_ERR(id)) {
3190 pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3191 ret = PTR_ERR(id);
3192 goto out;
3193 }
3194 pr_debug("id %p context %p\n", id, id->context);
3195
3196 ret = rdma_bind_addr(id, sa);
3197 if (ret) {
3198 pr_err("rdma_bind_addr() failed: %d\n", ret);
3199 goto out_id;
3200 }
3201
3202 ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3203 if (ret) {
3204 pr_err("rdma_listen() failed: %d\n", ret);
3205 goto out_id;
3206 }
3207
3208 return id;
3209 out_id:
3210 rdma_destroy_id(id);
3211 out:
3212 return ERR_PTR(ret);
3213 }
3214
3215 static int
isert_setup_np(struct iscsi_np * np,struct __kernel_sockaddr_storage * ksockaddr)3216 isert_setup_np(struct iscsi_np *np,
3217 struct __kernel_sockaddr_storage *ksockaddr)
3218 {
3219 struct isert_np *isert_np;
3220 struct rdma_cm_id *isert_lid;
3221 int ret;
3222
3223 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3224 if (!isert_np) {
3225 pr_err("Unable to allocate struct isert_np\n");
3226 return -ENOMEM;
3227 }
3228 sema_init(&isert_np->np_sem, 0);
3229 mutex_init(&isert_np->np_accept_mutex);
3230 INIT_LIST_HEAD(&isert_np->np_accept_list);
3231 init_completion(&isert_np->np_login_comp);
3232 isert_np->np = np;
3233
3234 /*
3235 * Setup the np->np_sockaddr from the passed sockaddr setup
3236 * in iscsi_target_configfs.c code..
3237 */
3238 memcpy(&np->np_sockaddr, ksockaddr,
3239 sizeof(struct __kernel_sockaddr_storage));
3240
3241 isert_lid = isert_setup_id(isert_np);
3242 if (IS_ERR(isert_lid)) {
3243 ret = PTR_ERR(isert_lid);
3244 goto out;
3245 }
3246
3247 isert_np->np_cm_id = isert_lid;
3248 np->np_context = isert_np;
3249
3250 return 0;
3251
3252 out:
3253 kfree(isert_np);
3254
3255 return ret;
3256 }
3257
3258 static int
isert_rdma_accept(struct isert_conn * isert_conn)3259 isert_rdma_accept(struct isert_conn *isert_conn)
3260 {
3261 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3262 struct rdma_conn_param cp;
3263 int ret;
3264
3265 memset(&cp, 0, sizeof(struct rdma_conn_param));
3266 cp.initiator_depth = isert_conn->initiator_depth;
3267 cp.retry_count = 7;
3268 cp.rnr_retry_count = 7;
3269
3270 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3271
3272 ret = rdma_accept(cm_id, &cp);
3273 if (ret) {
3274 pr_err("rdma_accept() failed with: %d\n", ret);
3275 return ret;
3276 }
3277
3278 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3279
3280 return 0;
3281 }
3282
3283 static int
isert_get_login_rx(struct iscsi_conn * conn,struct iscsi_login * login)3284 isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3285 {
3286 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3287 int ret;
3288
3289 pr_info("before login_req comp conn: %p\n", isert_conn);
3290 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3291 if (ret) {
3292 pr_err("isert_conn %p interrupted before got login req\n",
3293 isert_conn);
3294 return ret;
3295 }
3296 reinit_completion(&isert_conn->login_req_comp);
3297
3298 /*
3299 * For login requests after the first PDU, isert_rx_login_req() will
3300 * kick schedule_delayed_work(&conn->login_work) as the packet is
3301 * received, which turns this callback from iscsi_target_do_login_rx()
3302 * into a NOP.
3303 */
3304 if (!login->first_request)
3305 return 0;
3306
3307 isert_rx_login_req(isert_conn);
3308
3309 pr_info("before conn_login_comp conn: %p\n", conn);
3310 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3311 if (ret)
3312 return ret;
3313
3314 pr_info("processing login->req: %p\n", login->req);
3315
3316 return 0;
3317 }
3318
3319 static void
isert_set_conn_info(struct iscsi_np * np,struct iscsi_conn * conn,struct isert_conn * isert_conn)3320 isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3321 struct isert_conn *isert_conn)
3322 {
3323 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3324 struct rdma_route *cm_route = &cm_id->route;
3325 struct sockaddr_in *sock_in;
3326 struct sockaddr_in6 *sock_in6;
3327
3328 conn->login_family = np->np_sockaddr.ss_family;
3329
3330 if (np->np_sockaddr.ss_family == AF_INET6) {
3331 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3332 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3333 &sock_in6->sin6_addr.in6_u);
3334 conn->login_port = ntohs(sock_in6->sin6_port);
3335
3336 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3337 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3338 &sock_in6->sin6_addr.in6_u);
3339 conn->local_port = ntohs(sock_in6->sin6_port);
3340 } else {
3341 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3342 sprintf(conn->login_ip, "%pI4",
3343 &sock_in->sin_addr.s_addr);
3344 conn->login_port = ntohs(sock_in->sin_port);
3345
3346 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3347 sprintf(conn->local_ip, "%pI4",
3348 &sock_in->sin_addr.s_addr);
3349 conn->local_port = ntohs(sock_in->sin_port);
3350 }
3351 }
3352
3353 static int
isert_accept_np(struct iscsi_np * np,struct iscsi_conn * conn)3354 isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3355 {
3356 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3357 struct isert_conn *isert_conn;
3358 int max_accept = 0, ret;
3359
3360 accept_wait:
3361 ret = down_interruptible(&isert_np->np_sem);
3362 if (ret || max_accept > 5)
3363 return -ENODEV;
3364
3365 spin_lock_bh(&np->np_thread_lock);
3366 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3367 spin_unlock_bh(&np->np_thread_lock);
3368 pr_debug("np_thread_state %d for isert_accept_np\n",
3369 np->np_thread_state);
3370 /**
3371 * No point in stalling here when np_thread
3372 * is in state RESET/SHUTDOWN/EXIT - bail
3373 **/
3374 return -ENODEV;
3375 }
3376 spin_unlock_bh(&np->np_thread_lock);
3377
3378 mutex_lock(&isert_np->np_accept_mutex);
3379 if (list_empty(&isert_np->np_accept_list)) {
3380 mutex_unlock(&isert_np->np_accept_mutex);
3381 max_accept++;
3382 goto accept_wait;
3383 }
3384 isert_conn = list_first_entry(&isert_np->np_accept_list,
3385 struct isert_conn, conn_accept_node);
3386 list_del_init(&isert_conn->conn_accept_node);
3387 mutex_unlock(&isert_np->np_accept_mutex);
3388
3389 conn->context = isert_conn;
3390 isert_conn->conn = conn;
3391 max_accept = 0;
3392
3393 isert_set_conn_info(np, conn, isert_conn);
3394
3395 pr_debug("Processing isert_conn: %p\n", isert_conn);
3396
3397 return 0;
3398 }
3399
3400 static void
isert_free_np(struct iscsi_np * np)3401 isert_free_np(struct iscsi_np *np)
3402 {
3403 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3404
3405 if (isert_np->np_cm_id)
3406 rdma_destroy_id(isert_np->np_cm_id);
3407
3408 np->np_context = NULL;
3409 kfree(isert_np);
3410 }
3411
isert_release_work(struct work_struct * work)3412 static void isert_release_work(struct work_struct *work)
3413 {
3414 struct isert_conn *isert_conn = container_of(work,
3415 struct isert_conn,
3416 release_work);
3417
3418 pr_info("Starting release conn %p\n", isert_conn);
3419
3420 wait_for_completion(&isert_conn->conn_wait);
3421
3422 mutex_lock(&isert_conn->conn_mutex);
3423 isert_conn->state = ISER_CONN_DOWN;
3424 mutex_unlock(&isert_conn->conn_mutex);
3425
3426 pr_info("Destroying conn %p\n", isert_conn);
3427 isert_put_conn(isert_conn);
3428 }
3429
isert_wait_conn(struct iscsi_conn * conn)3430 static void isert_wait_conn(struct iscsi_conn *conn)
3431 {
3432 struct isert_conn *isert_conn = conn->context;
3433
3434 pr_debug("isert_wait_conn: Starting \n");
3435
3436 mutex_lock(&isert_conn->conn_mutex);
3437 /*
3438 * Only wait for conn_wait_comp_err if the isert_conn made it
3439 * into full feature phase..
3440 */
3441 if (isert_conn->state == ISER_CONN_INIT) {
3442 mutex_unlock(&isert_conn->conn_mutex);
3443 return;
3444 }
3445 isert_conn_terminate(isert_conn);
3446 mutex_unlock(&isert_conn->conn_mutex);
3447
3448 wait_for_completion(&isert_conn->conn_wait_comp_err);
3449
3450 queue_work(isert_release_wq, &isert_conn->release_work);
3451 }
3452
isert_free_conn(struct iscsi_conn * conn)3453 static void isert_free_conn(struct iscsi_conn *conn)
3454 {
3455 struct isert_conn *isert_conn = conn->context;
3456
3457 isert_put_conn(isert_conn);
3458 }
3459
3460 static struct iscsit_transport iser_target_transport = {
3461 .name = "IB/iSER",
3462 .transport_type = ISCSI_INFINIBAND,
3463 .priv_size = sizeof(struct isert_cmd),
3464 .owner = THIS_MODULE,
3465 .iscsit_setup_np = isert_setup_np,
3466 .iscsit_accept_np = isert_accept_np,
3467 .iscsit_free_np = isert_free_np,
3468 .iscsit_wait_conn = isert_wait_conn,
3469 .iscsit_free_conn = isert_free_conn,
3470 .iscsit_get_login_rx = isert_get_login_rx,
3471 .iscsit_put_login_tx = isert_put_login_tx,
3472 .iscsit_immediate_queue = isert_immediate_queue,
3473 .iscsit_response_queue = isert_response_queue,
3474 .iscsit_get_dataout = isert_get_dataout,
3475 .iscsit_queue_data_in = isert_put_datain,
3476 .iscsit_queue_status = isert_put_response,
3477 .iscsit_aborted_task = isert_aborted_task,
3478 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3479 };
3480
isert_init(void)3481 static int __init isert_init(void)
3482 {
3483 int ret;
3484
3485 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3486 if (!isert_rx_wq) {
3487 pr_err("Unable to allocate isert_rx_wq\n");
3488 return -ENOMEM;
3489 }
3490
3491 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3492 if (!isert_comp_wq) {
3493 pr_err("Unable to allocate isert_comp_wq\n");
3494 ret = -ENOMEM;
3495 goto destroy_rx_wq;
3496 }
3497
3498 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3499 WQ_UNBOUND_MAX_ACTIVE);
3500 if (!isert_release_wq) {
3501 pr_err("Unable to allocate isert_release_wq\n");
3502 ret = -ENOMEM;
3503 goto destroy_comp_wq;
3504 }
3505
3506 iscsit_register_transport(&iser_target_transport);
3507 pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3508
3509 return 0;
3510
3511 destroy_comp_wq:
3512 destroy_workqueue(isert_comp_wq);
3513 destroy_rx_wq:
3514 destroy_workqueue(isert_rx_wq);
3515 return ret;
3516 }
3517
isert_exit(void)3518 static void __exit isert_exit(void)
3519 {
3520 flush_scheduled_work();
3521 destroy_workqueue(isert_release_wq);
3522 destroy_workqueue(isert_comp_wq);
3523 destroy_workqueue(isert_rx_wq);
3524 iscsit_unregister_transport(&iser_target_transport);
3525 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3526 }
3527
3528 MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3529 MODULE_VERSION("0.1");
3530 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3531 MODULE_LICENSE("GPL");
3532
3533 module_init(isert_init);
3534 module_exit(isert_exit);
3535