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