162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This software is available to you under a choice of one of two
662306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
962306a36Sopenharmony_ci * OpenIB.org BSD license below:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1262306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1362306a36Sopenharmony_ci *     conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *	- Redistributions of source code must retain the above
1662306a36Sopenharmony_ci *	  copyright notice, this list of conditions and the following
1762306a36Sopenharmony_ci *	  disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *	- Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *	  copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *	  disclaimer in the documentation and/or other materials
2262306a36Sopenharmony_ci *	  provided with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3162306a36Sopenharmony_ci * SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci#include <linux/kernel.h>
3462306a36Sopenharmony_ci#include <linux/slab.h>
3562306a36Sopenharmony_ci#include <linux/mm.h>
3662306a36Sopenharmony_ci#include <linux/highmem.h>
3762306a36Sopenharmony_ci#include <linux/scatterlist.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include "iscsi_iser.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_civoid iser_reg_comp(struct ib_cq *cq, struct ib_wc *wc)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	iser_err_comp(wc, "memreg");
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic struct iser_fr_desc *iser_reg_desc_get_fr(struct ib_conn *ib_conn)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
4962306a36Sopenharmony_ci	struct iser_fr_desc *desc;
5062306a36Sopenharmony_ci	unsigned long flags;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	spin_lock_irqsave(&fr_pool->lock, flags);
5362306a36Sopenharmony_ci	desc = list_first_entry(&fr_pool->list,
5462306a36Sopenharmony_ci				struct iser_fr_desc, list);
5562306a36Sopenharmony_ci	list_del(&desc->list);
5662306a36Sopenharmony_ci	spin_unlock_irqrestore(&fr_pool->lock, flags);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	return desc;
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistatic void iser_reg_desc_put_fr(struct ib_conn *ib_conn,
6262306a36Sopenharmony_ci				 struct iser_fr_desc *desc)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
6562306a36Sopenharmony_ci	unsigned long flags;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	spin_lock_irqsave(&fr_pool->lock, flags);
6862306a36Sopenharmony_ci	list_add(&desc->list, &fr_pool->list);
6962306a36Sopenharmony_ci	spin_unlock_irqrestore(&fr_pool->lock, flags);
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciint iser_dma_map_task_data(struct iscsi_iser_task *iser_task,
7362306a36Sopenharmony_ci			   enum iser_data_dir iser_dir,
7462306a36Sopenharmony_ci			   enum dma_data_direction dma_dir)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct iser_data_buf *data = &iser_task->data[iser_dir];
7762306a36Sopenharmony_ci	struct ib_device *dev;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	iser_task->dir[iser_dir] = 1;
8062306a36Sopenharmony_ci	dev = iser_task->iser_conn->ib_conn.device->ib_device;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	data->dma_nents = ib_dma_map_sg(dev, data->sg, data->size, dma_dir);
8362306a36Sopenharmony_ci	if (unlikely(data->dma_nents == 0)) {
8462306a36Sopenharmony_ci		iser_err("dma_map_sg failed!!!\n");
8562306a36Sopenharmony_ci		return -EINVAL;
8662306a36Sopenharmony_ci	}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	if (scsi_prot_sg_count(iser_task->sc)) {
8962306a36Sopenharmony_ci		struct iser_data_buf *pdata = &iser_task->prot[iser_dir];
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci		pdata->dma_nents = ib_dma_map_sg(dev, pdata->sg, pdata->size, dma_dir);
9262306a36Sopenharmony_ci		if (unlikely(pdata->dma_nents == 0)) {
9362306a36Sopenharmony_ci			iser_err("protection dma_map_sg failed!!!\n");
9462306a36Sopenharmony_ci			goto out_unmap;
9562306a36Sopenharmony_ci		}
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	return 0;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciout_unmap:
10162306a36Sopenharmony_ci	ib_dma_unmap_sg(dev, data->sg, data->size, dma_dir);
10262306a36Sopenharmony_ci	return -EINVAL;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_civoid iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task,
10762306a36Sopenharmony_ci			      enum iser_data_dir iser_dir,
10862306a36Sopenharmony_ci			      enum dma_data_direction dma_dir)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	struct iser_data_buf *data = &iser_task->data[iser_dir];
11162306a36Sopenharmony_ci	struct ib_device *dev;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	dev = iser_task->iser_conn->ib_conn.device->ib_device;
11462306a36Sopenharmony_ci	ib_dma_unmap_sg(dev, data->sg, data->size, dma_dir);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	if (scsi_prot_sg_count(iser_task->sc)) {
11762306a36Sopenharmony_ci		struct iser_data_buf *pdata = &iser_task->prot[iser_dir];
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci		ib_dma_unmap_sg(dev, pdata->sg, pdata->size, dma_dir);
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic int iser_reg_dma(struct iser_device *device, struct iser_data_buf *mem,
12462306a36Sopenharmony_ci			struct iser_mem_reg *reg)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	struct scatterlist *sg = mem->sg;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	reg->sge.lkey = device->pd->local_dma_lkey;
12962306a36Sopenharmony_ci	/*
13062306a36Sopenharmony_ci	 * FIXME: rework the registration code path to differentiate
13162306a36Sopenharmony_ci	 * rkey/lkey use cases
13262306a36Sopenharmony_ci	 */
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	if (device->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
13562306a36Sopenharmony_ci		reg->rkey = device->pd->unsafe_global_rkey;
13662306a36Sopenharmony_ci	else
13762306a36Sopenharmony_ci		reg->rkey = 0;
13862306a36Sopenharmony_ci	reg->sge.addr = sg_dma_address(&sg[0]);
13962306a36Sopenharmony_ci	reg->sge.length = sg_dma_len(&sg[0]);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
14262306a36Sopenharmony_ci		 " length=0x%x\n", reg->sge.lkey, reg->rkey,
14362306a36Sopenharmony_ci		 reg->sge.addr, reg->sge.length);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	return 0;
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_civoid iser_unreg_mem_fastreg(struct iscsi_iser_task *iser_task,
14962306a36Sopenharmony_ci			    enum iser_data_dir cmd_dir)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
15262306a36Sopenharmony_ci	struct iser_fr_desc *desc;
15362306a36Sopenharmony_ci	struct ib_mr_status mr_status;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	desc = reg->desc;
15662306a36Sopenharmony_ci	if (!desc)
15762306a36Sopenharmony_ci		return;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	/*
16062306a36Sopenharmony_ci	 * The signature MR cannot be invalidated and reused without checking.
16162306a36Sopenharmony_ci	 * libiscsi calls the check_protection transport handler only if
16262306a36Sopenharmony_ci	 * SCSI-Response is received. And the signature MR is not checked if
16362306a36Sopenharmony_ci	 * the task is completed for some other reason like a timeout or error
16462306a36Sopenharmony_ci	 * handling. That's why we must check the signature MR here before
16562306a36Sopenharmony_ci	 * putting it to the free pool.
16662306a36Sopenharmony_ci	 */
16762306a36Sopenharmony_ci	if (unlikely(desc->sig_protected)) {
16862306a36Sopenharmony_ci		desc->sig_protected = false;
16962306a36Sopenharmony_ci		ib_check_mr_status(desc->rsc.sig_mr, IB_MR_CHECK_SIG_STATUS,
17062306a36Sopenharmony_ci				   &mr_status);
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci	iser_reg_desc_put_fr(&iser_task->iser_conn->ib_conn, reg->desc);
17362306a36Sopenharmony_ci	reg->desc = NULL;
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic void iser_set_dif_domain(struct scsi_cmnd *sc,
17762306a36Sopenharmony_ci				struct ib_sig_domain *domain)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	domain->sig_type = IB_SIG_TYPE_T10_DIF;
18062306a36Sopenharmony_ci	domain->sig.dif.pi_interval = scsi_prot_interval(sc);
18162306a36Sopenharmony_ci	domain->sig.dif.ref_tag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
18262306a36Sopenharmony_ci	/*
18362306a36Sopenharmony_ci	 * At the moment we hard code those, but in the future
18462306a36Sopenharmony_ci	 * we will take them from sc.
18562306a36Sopenharmony_ci	 */
18662306a36Sopenharmony_ci	domain->sig.dif.apptag_check_mask = 0xffff;
18762306a36Sopenharmony_ci	domain->sig.dif.app_escape = true;
18862306a36Sopenharmony_ci	domain->sig.dif.ref_escape = true;
18962306a36Sopenharmony_ci	if (sc->prot_flags & SCSI_PROT_REF_INCREMENT)
19062306a36Sopenharmony_ci		domain->sig.dif.ref_remap = true;
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic int iser_set_sig_attrs(struct scsi_cmnd *sc,
19462306a36Sopenharmony_ci			      struct ib_sig_attrs *sig_attrs)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	switch (scsi_get_prot_op(sc)) {
19762306a36Sopenharmony_ci	case SCSI_PROT_WRITE_INSERT:
19862306a36Sopenharmony_ci	case SCSI_PROT_READ_STRIP:
19962306a36Sopenharmony_ci		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
20062306a36Sopenharmony_ci		iser_set_dif_domain(sc, &sig_attrs->wire);
20162306a36Sopenharmony_ci		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
20262306a36Sopenharmony_ci		break;
20362306a36Sopenharmony_ci	case SCSI_PROT_READ_INSERT:
20462306a36Sopenharmony_ci	case SCSI_PROT_WRITE_STRIP:
20562306a36Sopenharmony_ci		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
20662306a36Sopenharmony_ci		iser_set_dif_domain(sc, &sig_attrs->mem);
20762306a36Sopenharmony_ci		sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
20862306a36Sopenharmony_ci						IB_T10DIF_CSUM : IB_T10DIF_CRC;
20962306a36Sopenharmony_ci		break;
21062306a36Sopenharmony_ci	case SCSI_PROT_READ_PASS:
21162306a36Sopenharmony_ci	case SCSI_PROT_WRITE_PASS:
21262306a36Sopenharmony_ci		iser_set_dif_domain(sc, &sig_attrs->wire);
21362306a36Sopenharmony_ci		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
21462306a36Sopenharmony_ci		iser_set_dif_domain(sc, &sig_attrs->mem);
21562306a36Sopenharmony_ci		sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
21662306a36Sopenharmony_ci						IB_T10DIF_CSUM : IB_T10DIF_CRC;
21762306a36Sopenharmony_ci		break;
21862306a36Sopenharmony_ci	default:
21962306a36Sopenharmony_ci		iser_err("Unsupported PI operation %d\n",
22062306a36Sopenharmony_ci			 scsi_get_prot_op(sc));
22162306a36Sopenharmony_ci		return -EINVAL;
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	return 0;
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic inline void iser_set_prot_checks(struct scsi_cmnd *sc, u8 *mask)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	*mask = 0;
23062306a36Sopenharmony_ci	if (sc->prot_flags & SCSI_PROT_REF_CHECK)
23162306a36Sopenharmony_ci		*mask |= IB_SIG_CHECK_REFTAG;
23262306a36Sopenharmony_ci	if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
23362306a36Sopenharmony_ci		*mask |= IB_SIG_CHECK_GUARD;
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic inline void iser_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr,
23762306a36Sopenharmony_ci				 struct ib_cqe *cqe, struct ib_send_wr *next_wr)
23862306a36Sopenharmony_ci{
23962306a36Sopenharmony_ci	inv_wr->opcode = IB_WR_LOCAL_INV;
24062306a36Sopenharmony_ci	inv_wr->wr_cqe = cqe;
24162306a36Sopenharmony_ci	inv_wr->ex.invalidate_rkey = mr->rkey;
24262306a36Sopenharmony_ci	inv_wr->send_flags = 0;
24362306a36Sopenharmony_ci	inv_wr->num_sge = 0;
24462306a36Sopenharmony_ci	inv_wr->next = next_wr;
24562306a36Sopenharmony_ci}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cistatic int iser_reg_sig_mr(struct iscsi_iser_task *iser_task,
24862306a36Sopenharmony_ci			   struct iser_data_buf *mem,
24962306a36Sopenharmony_ci			   struct iser_data_buf *sig_mem,
25062306a36Sopenharmony_ci			   struct iser_reg_resources *rsc,
25162306a36Sopenharmony_ci			   struct iser_mem_reg *sig_reg)
25262306a36Sopenharmony_ci{
25362306a36Sopenharmony_ci	struct iser_tx_desc *tx_desc = &iser_task->desc;
25462306a36Sopenharmony_ci	struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
25562306a36Sopenharmony_ci	struct ib_mr *mr = rsc->sig_mr;
25662306a36Sopenharmony_ci	struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
25762306a36Sopenharmony_ci	struct ib_reg_wr *wr = &tx_desc->reg_wr;
25862306a36Sopenharmony_ci	int ret;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	memset(sig_attrs, 0, sizeof(*sig_attrs));
26162306a36Sopenharmony_ci	ret = iser_set_sig_attrs(iser_task->sc, sig_attrs);
26262306a36Sopenharmony_ci	if (ret)
26362306a36Sopenharmony_ci		goto err;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	iser_set_prot_checks(iser_task->sc, &sig_attrs->check_mask);
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	if (rsc->sig_mr->need_inval)
26862306a36Sopenharmony_ci		iser_inv_rkey(&tx_desc->inv_wr, mr, cqe, &wr->wr);
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	ret = ib_map_mr_sg_pi(mr, mem->sg, mem->dma_nents, NULL,
27362306a36Sopenharmony_ci			      sig_mem->sg, sig_mem->dma_nents, NULL, SZ_4K);
27462306a36Sopenharmony_ci	if (unlikely(ret)) {
27562306a36Sopenharmony_ci		iser_err("failed to map PI sg (%d)\n",
27662306a36Sopenharmony_ci			 mem->dma_nents + sig_mem->dma_nents);
27762306a36Sopenharmony_ci		goto err;
27862306a36Sopenharmony_ci	}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	memset(wr, 0, sizeof(*wr));
28162306a36Sopenharmony_ci	wr->wr.next = &tx_desc->send_wr;
28262306a36Sopenharmony_ci	wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
28362306a36Sopenharmony_ci	wr->wr.wr_cqe = cqe;
28462306a36Sopenharmony_ci	wr->wr.num_sge = 0;
28562306a36Sopenharmony_ci	wr->wr.send_flags = 0;
28662306a36Sopenharmony_ci	wr->mr = mr;
28762306a36Sopenharmony_ci	wr->key = mr->rkey;
28862306a36Sopenharmony_ci	wr->access = IB_ACCESS_LOCAL_WRITE |
28962306a36Sopenharmony_ci		     IB_ACCESS_REMOTE_READ |
29062306a36Sopenharmony_ci		     IB_ACCESS_REMOTE_WRITE;
29162306a36Sopenharmony_ci	rsc->sig_mr->need_inval = true;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	sig_reg->sge.lkey = mr->lkey;
29462306a36Sopenharmony_ci	sig_reg->rkey = mr->rkey;
29562306a36Sopenharmony_ci	sig_reg->sge.addr = mr->iova;
29662306a36Sopenharmony_ci	sig_reg->sge.length = mr->length;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
29962306a36Sopenharmony_ci		 sig_reg->sge.lkey, sig_reg->rkey, sig_reg->sge.addr,
30062306a36Sopenharmony_ci		 sig_reg->sge.length);
30162306a36Sopenharmony_cierr:
30262306a36Sopenharmony_ci	return ret;
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_cistatic int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
30662306a36Sopenharmony_ci			    struct iser_data_buf *mem,
30762306a36Sopenharmony_ci			    struct iser_reg_resources *rsc,
30862306a36Sopenharmony_ci			    struct iser_mem_reg *reg)
30962306a36Sopenharmony_ci{
31062306a36Sopenharmony_ci	struct iser_tx_desc *tx_desc = &iser_task->desc;
31162306a36Sopenharmony_ci	struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
31262306a36Sopenharmony_ci	struct ib_mr *mr = rsc->mr;
31362306a36Sopenharmony_ci	struct ib_reg_wr *wr = &tx_desc->reg_wr;
31462306a36Sopenharmony_ci	int n;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	if (rsc->mr->need_inval)
31762306a36Sopenharmony_ci		iser_inv_rkey(&tx_desc->inv_wr, mr, cqe, &wr->wr);
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	n = ib_map_mr_sg(mr, mem->sg, mem->dma_nents, NULL, SZ_4K);
32262306a36Sopenharmony_ci	if (unlikely(n != mem->dma_nents)) {
32362306a36Sopenharmony_ci		iser_err("failed to map sg (%d/%d)\n",
32462306a36Sopenharmony_ci			 n, mem->dma_nents);
32562306a36Sopenharmony_ci		return n < 0 ? n : -EINVAL;
32662306a36Sopenharmony_ci	}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	wr->wr.next = &tx_desc->send_wr;
32962306a36Sopenharmony_ci	wr->wr.opcode = IB_WR_REG_MR;
33062306a36Sopenharmony_ci	wr->wr.wr_cqe = cqe;
33162306a36Sopenharmony_ci	wr->wr.send_flags = 0;
33262306a36Sopenharmony_ci	wr->wr.num_sge = 0;
33362306a36Sopenharmony_ci	wr->mr = mr;
33462306a36Sopenharmony_ci	wr->key = mr->rkey;
33562306a36Sopenharmony_ci	wr->access = IB_ACCESS_LOCAL_WRITE  |
33662306a36Sopenharmony_ci		     IB_ACCESS_REMOTE_WRITE |
33762306a36Sopenharmony_ci		     IB_ACCESS_REMOTE_READ;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	rsc->mr->need_inval = true;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	reg->sge.lkey = mr->lkey;
34262306a36Sopenharmony_ci	reg->rkey = mr->rkey;
34362306a36Sopenharmony_ci	reg->sge.addr = mr->iova;
34462306a36Sopenharmony_ci	reg->sge.length = mr->length;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
34762306a36Sopenharmony_ci		 reg->sge.lkey, reg->rkey, reg->sge.addr, reg->sge.length);
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	return 0;
35062306a36Sopenharmony_ci}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ciint iser_reg_mem_fastreg(struct iscsi_iser_task *task,
35362306a36Sopenharmony_ci			 enum iser_data_dir dir,
35462306a36Sopenharmony_ci			 bool all_imm)
35562306a36Sopenharmony_ci{
35662306a36Sopenharmony_ci	struct ib_conn *ib_conn = &task->iser_conn->ib_conn;
35762306a36Sopenharmony_ci	struct iser_device *device = ib_conn->device;
35862306a36Sopenharmony_ci	struct iser_data_buf *mem = &task->data[dir];
35962306a36Sopenharmony_ci	struct iser_mem_reg *reg = &task->rdma_reg[dir];
36062306a36Sopenharmony_ci	struct iser_fr_desc *desc;
36162306a36Sopenharmony_ci	bool use_dma_key;
36262306a36Sopenharmony_ci	int err;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	use_dma_key = mem->dma_nents == 1 && (all_imm || !iser_always_reg) &&
36562306a36Sopenharmony_ci		      scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL;
36662306a36Sopenharmony_ci	if (use_dma_key)
36762306a36Sopenharmony_ci		return iser_reg_dma(device, mem, reg);
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	desc = iser_reg_desc_get_fr(ib_conn);
37062306a36Sopenharmony_ci	if (scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL) {
37162306a36Sopenharmony_ci		err = iser_fast_reg_mr(task, mem, &desc->rsc, reg);
37262306a36Sopenharmony_ci		if (unlikely(err))
37362306a36Sopenharmony_ci			goto err_reg;
37462306a36Sopenharmony_ci	} else {
37562306a36Sopenharmony_ci		err = iser_reg_sig_mr(task, mem, &task->prot[dir],
37662306a36Sopenharmony_ci				      &desc->rsc, reg);
37762306a36Sopenharmony_ci		if (unlikely(err))
37862306a36Sopenharmony_ci			goto err_reg;
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci		desc->sig_protected = true;
38162306a36Sopenharmony_ci	}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	reg->desc = desc;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	return 0;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_cierr_reg:
38862306a36Sopenharmony_ci	iser_reg_desc_put_fr(ib_conn, desc);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	return err;
39162306a36Sopenharmony_ci}
392