All of lore.kernel.org
 help / color / mirror / Atom feed
From: Varun Prakash <varun@chelsio.com>
To: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Cc: nab@linux-iscsi.org, roland@kernel.org,
	swise@opengridcomputing.com, indranil@chelsio.com,
	kxie@chelsio.com, hariprasad@chelsio.com, varun@chelsio.com
Subject: [RFC 32/34] cxgbit: add cxgbit_ddp.c
Date: Sun, 14 Feb 2016 23:15:39 +0530	[thread overview]
Message-ID: <092f672bc73ded67450869f5383352b38c79ae29.1455467089.git.varun@chelsio.com> (raw)
In-Reply-To: <19973d4150937cdaffec583caa35b5d8c9a64fbb.1455467089.git.varun@chelsio.com>
In-Reply-To: <cover.1455467089.git.varun@chelsio.com>

This file contains code for
Direct Data Placement.

Signed-off-by: Varun Prakash <varun@chelsio.com>
---
 drivers/target/iscsi/cxgbit/cxgbit_ddp.c | 374 +++++++++++++++++++++++++++++++
 1 file changed, 374 insertions(+)
 create mode 100644 drivers/target/iscsi/cxgbit/cxgbit_ddp.c

diff --git a/drivers/target/iscsi/cxgbit/cxgbit_ddp.c b/drivers/target/iscsi/cxgbit/cxgbit_ddp.c
new file mode 100644
index 0000000..07e2bc8
--- /dev/null
+++ b/drivers/target/iscsi/cxgbit/cxgbit_ddp.c
@@ -0,0 +1,374 @@
+/*
+ * Copyright (c) 2016 Chelsio Communications, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include "cxgbit.h"
+
+/*
+ * functions to program the pagepod in h/w
+ */
+static void ulp_mem_io_set_hdr(struct cxgbit_device *cdev,
+			       struct ulp_mem_io *req,
+			       unsigned int wr_len,
+			       unsigned int dlen,
+			       unsigned int pm_addr,
+			       int tid)
+{
+	struct ulptx_idata *idata = (struct ulptx_idata *)(req + 1);
+
+	INIT_ULPTX_WR(req, wr_len, 0, tid);
+	req->wr.wr_hi = htonl(FW_WR_OP_V(FW_ULPTX_WR) |
+		FW_WR_ATOMIC_V(0));
+	req->cmd = htonl(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
+		ULP_MEMIO_ORDER_V(0) |
+		T5_ULP_MEMIO_IMM_V(1));
+	req->dlen = htonl(ULP_MEMIO_DATA_LEN_V(dlen >> 5));
+	req->lock_addr = htonl(ULP_MEMIO_ADDR_V(pm_addr >> 5));
+	req->len16 = htonl(DIV_ROUND_UP(wr_len - sizeof(req->wr), 16));
+
+	idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
+	idata->len = htonl(dlen);
+}
+
+static void cxgbit_set_one_ppod(struct cxgbi_pagepod *ppod,
+				struct cxgbi_task_tag_info *ttinfo,
+				struct scatterlist **sg_pp,
+				unsigned int *sg_off)
+{
+	struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
+	unsigned int offset = sg_off ? *sg_off : 0;
+	dma_addr_t addr = 0UL;
+	unsigned int len = 0;
+	int i;
+
+	memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
+
+	if (sg) {
+		addr = sg_dma_address(sg);
+		len = sg_dma_len(sg);
+	}
+
+	for (i = 0; i < PPOD_PAGES_MAX; i++) {
+		if (sg) {
+			ppod->addr[i] = cpu_to_be64(addr + offset);
+			offset += PAGE_SIZE;
+			if (offset == (len + sg->offset)) {
+				offset = 0;
+				sg = sg_next(sg);
+				if (sg) {
+					addr = sg_dma_address(sg);
+					len = sg_dma_len(sg);
+				}
+			}
+		} else {
+			ppod->addr[i] = 0ULL;
+		}
+	}
+
+	/*
+	 * the fifth address needs to be repeated in the next ppod, so do
+	 * not move sg
+	 */
+	if (sg_pp) {
+		*sg_pp = sg;
+		*sg_off = offset;
+	}
+
+	if (offset == len) {
+		offset = 0;
+		sg = sg_next(sg);
+		if (sg) {
+			addr = sg_dma_address(sg);
+			len = sg_dma_len(sg);
+		}
+	}
+	ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
+}
+
+static struct sk_buff *cxgbit_ppod_init_idata(struct cxgbit_device *cdev,
+					      struct cxgbi_ppm *ppm,
+					      unsigned int idx,
+					      unsigned int npods,
+					      unsigned int tid)
+{
+	unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ppm->llimit;
+	unsigned int dlen = npods << PPOD_SIZE_SHIFT;
+	unsigned int wr_len = roundup(sizeof(struct ulp_mem_io) +
+				sizeof(struct ulptx_idata) + dlen, 16);
+	struct sk_buff *skb = alloc_skb(wr_len, GFP_KERNEL);
+
+	if (!skb)
+		return NULL;
+
+	__skb_put(skb, wr_len);
+	ulp_mem_io_set_hdr(cdev, (struct ulp_mem_io *)skb->data, wr_len, dlen,
+			   pm_addr, tid);
+
+	return skb;
+}
+
+static int cxgbit_ppod_write_idata(struct cxgbi_ppm *ppm,
+				   struct cxgbit_sock *csk,
+				   struct cxgbi_task_tag_info *ttinfo,
+				   unsigned int idx, unsigned int npods,
+				   struct scatterlist **sg_pp,
+				   unsigned int *sg_off)
+{
+	struct cxgbit_device *cdev = csk->com.cdev;
+	struct sk_buff *skb = cxgbit_ppod_init_idata(cdev, ppm, idx, npods,
+						csk->tid);
+	struct ulp_mem_io *req;
+	struct ulptx_idata *idata;
+	struct cxgbi_pagepod *ppod;
+	int i;
+
+	if (!skb)
+		return -ENOMEM;
+
+	req = (struct ulp_mem_io *)skb->data;
+	idata = (struct ulptx_idata *)(req + 1);
+	ppod = (struct cxgbi_pagepod *)(idata + 1);
+
+	for (i = 0; i < npods; i++, ppod++)
+		cxgbit_set_one_ppod(ppod, ttinfo, sg_pp, sg_off);
+
+	__skb_queue_tail(&csk->ppodq, skb);
+
+	return 0;
+}
+
+static int cxgbit_ddp_set_map(struct cxgbi_ppm *ppm, struct cxgbit_sock *csk,
+			      struct cxgbi_task_tag_info *ttinfo)
+{
+	unsigned int pidx = ttinfo->idx;
+	unsigned int npods = ttinfo->npods;
+	unsigned int i, cnt;
+	int ret = 0;
+	struct scatterlist *sg = ttinfo->sgl;
+	unsigned int offset = 0;
+
+	ttinfo->cid = csk->port_id;
+
+	for (i = 0; i < npods; i += cnt, pidx += cnt) {
+		cnt = npods - i;
+
+		if (cnt > ULPMEM_IDATA_MAX_NPPODS)
+			cnt = ULPMEM_IDATA_MAX_NPPODS;
+
+		ret = cxgbit_ppod_write_idata(ppm, csk, ttinfo, pidx, cnt,
+					      &sg, &offset);
+		if (ret < 0)
+			break;
+	}
+
+	return ret;
+}
+
+static void
+cxgbit_dump_sgl(const char *cap, struct scatterlist *sgl, int nents)
+{
+	struct scatterlist *sg;
+	int i;
+
+	if (cap)
+		pr_info("%s: sgl 0x%p, nents %u.\n", cap, sgl, nents);
+	for_each_sg(sgl, sg, nents, i)
+		pr_info("\t%d/%u, 0x%p: len %u, off %u, pg 0x%p, dma 0x%llx, %u\n",
+			i, nents, sg, sg->length, sg->offset, sg_page(sg),
+			sg_dma_address(sg), sg_dma_len(sg));
+}
+
+static int cxgbit_ddp_sgl_check(struct scatterlist *sgl, int nents)
+{
+	int i;
+	int last_sgidx = nents - 1;
+	struct scatterlist *sg = sgl;
+
+	for (i = 1, sg = sg_next(sgl); i < nents; i++, sg = sg_next(sg)) {
+		if ((i && sg->offset) ||
+		    ((i != last_sgidx) &&
+		     ((sg->length + sg->offset) & ((1 << PAGE_SHIFT) - 1)))) {
+			pr_info("%s: sg %u/%u, %u,%u, not page aligned.\n",
+				__func__, i, nents, sg->offset, sg->length);
+			cxgbit_dump_sgl(NULL, sgl, nents);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+static int cxgbit_ddp_reserve(struct cxgbit_sock *csk,
+			      struct cxgbi_task_tag_info *ttinfo,
+			      unsigned int xferlen)
+{
+	struct cxgbit_device *cdev = csk->com.cdev;
+	struct cxgbi_ppm *ppm = cdev2ppm(cdev);
+	struct scatterlist *sgl = ttinfo->sgl;
+	unsigned int sgcnt = ttinfo->nents;
+	unsigned int sg_offset = sgl->offset;
+	int ret;
+
+	if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
+	    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
+		pr_debug("ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
+			 ppm, ppm ? ppm->tformat.pgsz_idx_dflt :
+			 DDP_PGIDX_MAX,
+			 xferlen, ttinfo->nents);
+		return -EINVAL;
+	}
+
+	/* make sure the buffer is suitable for ddp */
+	if (cxgbit_ddp_sgl_check(sgl, sgcnt) < 0)
+		return -EINVAL;
+
+	ttinfo->nr_pages = (xferlen + sgl->offset +
+			    (1 << PAGE_SHIFT) - 1) >> PAGE_SHIFT;
+
+	/*
+	 * the ddp tag will be used for the ttt in the outgoing r2t pdu
+	 */
+	ret = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
+				      &ttinfo->tag, 0);
+	if (ret < 0)
+		return ret;
+	ttinfo->npods = ret;
+
+	 /* setup dma from scsi command sgl */
+	sgl->offset = 0;
+	ret = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
+	sgl->offset = sg_offset;
+	if (!ret) {
+		pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
+			__func__, 0, xferlen, sgcnt);
+		goto rel_ppods;
+	}
+	if (ret != ttinfo->nr_pages) {
+		pr_info("%s: 0x%x, xfer %u, sgl %u, dma count %d.\n",
+			__func__, 0, xferlen, sgcnt, ret);
+		cxgbit_dump_sgl(__func__, sgl, sgcnt);
+	}
+
+	ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
+	ttinfo->cid = csk->port_id;
+
+	cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
+				xferlen, &ttinfo->hdr);
+
+	ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
+	cxgbit_ddp_set_map(ppm, csk, ttinfo);
+
+	return 0;
+
+rel_ppods:
+	cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
+
+	if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
+		ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
+		dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
+	}
+	return -EINVAL;
+}
+
+int cxgbit_reserve_ttt(struct cxgbit_sock *csk, struct iscsi_cmd *cmd)
+{
+	struct cxgbit_device *cdev = csk->com.cdev;
+	struct cxgbit_cmd *ccmd = iscsit_priv_cmd(cmd);
+	struct cxgbi_task_tag_info *ttinfo = &ccmd->ttinfo;
+	int ret = -EINVAL;
+
+	ttinfo->sgl = cmd->se_cmd.t_data_sg;
+	ttinfo->nents = cmd->se_cmd.t_data_nents;
+
+	ret = cxgbit_ddp_reserve(csk, ttinfo, cmd->se_cmd.data_length);
+	if (ret < 0) {
+		pr_info("csk 0x%p, cmd 0x%p, xfer len %u, sgcnt %u no ddp.\n",
+			csk, cmd, cmd->se_cmd.data_length, ttinfo->nents);
+
+		ttinfo->sgl = NULL;
+		ttinfo->nents = 0;
+
+		return ret;
+	}
+
+	ccmd->release = true;
+
+	pr_debug("cdev 0x%p, cmd 0x%p, tag 0x%x\n", cdev, cmd, ttinfo->tag);
+
+	return 0;
+}
+
+void cxgbit_release_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
+{
+	struct cxgbit_cmd *ccmd = iscsit_priv_cmd(cmd);
+
+	if (ccmd->release) {
+		struct cxgbi_task_tag_info *ttinfo = &ccmd->ttinfo;
+
+		if (ttinfo->sgl) {
+			struct cxgbit_sock *csk = conn->context;
+			struct cxgbit_device *cdev = csk->com.cdev;
+			struct cxgbi_ppm *ppm = cdev2ppm(cdev);
+
+			cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
+
+			dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl,
+				     ttinfo->nents, DMA_FROM_DEVICE);
+		} else {
+			put_page(sg_page(&ccmd->sg));
+		}
+
+		ccmd->release = false;
+	}
+}
+
+static void cxgbit_ddp_ppm_setup(void **ppm_pp, struct cxgbit_device *cdev,
+				 struct cxgbi_tag_format *tformat,
+				 unsigned int ppmax,
+				 unsigned int llimit,
+				 unsigned int start)
+{
+	int ret = cxgbi_ppm_init(ppm_pp, cdev->lldi.ports[0], cdev->lldi.pdev,
+				 &cdev->lldi, tformat, ppmax, llimit, start,
+				 2);
+
+	if (ret >= 0) {
+		struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
+
+		if (ppm->ppmax < 1024 ||
+		    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
+			return;
+
+		set_bit(CDEV_DDP_ENABLE, &cdev->flags);
+	}
+}
+
+int cxgbit_ddp_init(struct cxgbit_device *cdev)
+{
+	struct cxgb4_lld_info *lldi = &cdev->lldi;
+	struct net_device *ndev = cdev->lldi.ports[0];
+	struct cxgbi_tag_format tformat;
+	unsigned int ppmax;
+	int i;
+
+	if (!lldi->vr->iscsi.size) {
+		pr_warn("%s, iscsi NOT enabled, check config!\n", ndev->name);
+		return -EACCES;
+	}
+
+	ppmax = lldi->vr->iscsi.size >> PPOD_SIZE_SHIFT;
+
+	memset(&tformat, 0, sizeof(struct cxgbi_tag_format));
+	for (i = 0; i < 4; i++)
+		tformat.pgsz_order[i] = (lldi->iscsi_pgsz_order >> (i << 3))
+					 & 0xF;
+	cxgbi_tagmask_check(lldi->iscsi_tagmask, &tformat);
+
+	cxgbit_ddp_ppm_setup(lldi->iscsi_ppm, cdev, &tformat, ppmax,
+			     lldi->iscsi_llimit, lldi->vr->iscsi.start);
+	return 0;
+}
-- 
2.0.2

  parent reply	other threads:[~2016-02-14 17:45 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-14 17:30 [RFC 00/34] Chelsio iSCSI target offload driver Varun Prakash
2016-02-14 17:32 ` [RFC 01/34] cxgb4: add new ULD type CXGB4_ULD_ISCSIT Varun Prakash
2016-02-14 17:32 ` [RFC 02/34] cxgb4: allocate resources for CXGB4_ULD_ISCSIT Varun Prakash
2016-02-14 17:32 ` [RFC 03/34] cxgb4: large receive offload support Varun Prakash
2016-02-14 17:34 ` [RFC 04/34] cxgb4, iw_cxgb4: move definitions to common header file Varun Prakash
2016-02-14 17:34 ` [RFC 05/34] cxgb4, iw_cxgb4, cxgb4i: remove duplicate definitions Varun Prakash
2016-02-14 17:37 ` [RFC 06/34] cxgb4, cxgb4i: move struct cpl_rx_data_ddp definition Varun Prakash
2016-02-14 17:37 ` [RFC 07/34] cxgb4: add definitions for iSCSI target ULD Varun Prakash
2016-02-14 17:37 ` [RFC 08/34] cxgb4: update struct cxgb4_lld_info definition Varun Prakash
2016-02-14 17:37 ` [RFC 09/34] cxgb4: move VLAN_NONE macro definition Varun Prakash
2016-02-14 17:38 ` [RFC 10/34] cxgb4, iw_cxgb4: move delayed ack macro definitions Varun Prakash
2016-02-14 17:39 ` [RFC 11/34] cxgb4: add iSCSI DDP page pod manager Varun Prakash
2016-02-14 17:39 ` [RFC 12/34] cxgb4: update Kconfig and Makefile Varun Prakash
2016-03-01 14:47   ` Christoph Hellwig
2016-03-02 10:56     ` Varun Prakash
2016-02-14 17:42 ` [RFC 13/34] iscsi-target: add new transport type Varun Prakash
2016-03-01 14:48   ` Christoph Hellwig
2016-03-02 11:52     ` Varun Prakash
2016-03-05 21:28     ` Nicholas A. Bellinger
2016-03-07 14:55       ` Varun Prakash
2016-03-07 20:30         ` Nicholas A. Bellinger
2016-02-14 17:42 ` [RFC 14/34] iscsi-target: export symbols Varun Prakash
2016-03-01 14:49   ` Christoph Hellwig
2016-03-02 12:00     ` Varun Prakash
2016-03-05 21:54       ` Nicholas A. Bellinger
2016-03-07 23:22         ` Nicholas A. Bellinger
2016-03-12  6:28           ` Nicholas A. Bellinger
2016-03-13 12:13             ` Varun Prakash
2016-04-08  7:16               ` Nicholas A. Bellinger
2016-04-09 12:09                 ` Varun Prakash
2016-04-10  8:56                 ` Sagi Grimberg
2016-02-14 17:42 ` [RFC 15/34] iscsi-target: export symbols from iscsi_target.c Varun Prakash
2016-03-01 14:49   ` Christoph Hellwig
2016-03-02 12:07     ` Varun Prakash
2016-02-14 17:42 ` [RFC 16/34] iscsi-target: split iscsit_send_r2t() Varun Prakash
2016-02-14 17:42 ` [RFC 17/34] iscsi-target: split iscsit_send_conn_drop_async_message() Varun Prakash
2016-02-14 17:42 ` [RFC 18/34] iscsi-target: call complete on conn_logout_comp Varun Prakash
2016-02-15 17:07   ` Sagi Grimberg
2016-03-01 14:52     ` Christoph Hellwig
2016-03-05 21:02       ` Nicholas A. Bellinger
2016-02-14 17:42 ` [RFC 19/34] iscsi-target: clear tx_thread_active Varun Prakash
2016-02-15 17:07   ` Sagi Grimberg
2016-03-01 14:59   ` Christoph Hellwig
2016-02-14 17:42 ` [RFC 20/34] iscsi-target: update struct iscsit_transport definition Varun Prakash
2016-02-15 17:09   ` Sagi Grimberg
2016-02-18 12:36     ` Varun Prakash
2016-02-14 17:42 ` [RFC 21/34] iscsi-target: release transport driver resources Varun Prakash
2016-03-01 14:59   ` Christoph Hellwig
2016-03-02 12:15     ` Varun Prakash
2016-02-14 17:45 ` [RFC 22/34] iscsi-target: call Rx thread function Varun Prakash
2016-02-15 17:16   ` Sagi Grimberg
2016-03-01 15:01   ` Christoph Hellwig
2016-03-05 23:16     ` Nicholas A. Bellinger
2016-02-14 17:45 ` [RFC 23/34] iscsi-target: split iscsi_target_rx_thread() Varun Prakash
2016-03-01 15:02   ` Christoph Hellwig
2016-02-14 17:45 ` [RFC 24/34] iscsi-target: validate conn operational parameters Varun Prakash
2016-03-01 15:03   ` Christoph Hellwig
2016-03-02 12:18     ` Varun Prakash
2016-02-14 17:45 ` [RFC 25/34] iscsi-target: move iscsit_thread_check_cpumask() Varun Prakash
2016-02-14 17:45 ` [RFC 26/34] iscsi-target: fix seq_end_offset calculation Varun Prakash
2016-02-14 17:45 ` [RFC 27/34] cxgbit: add cxgbit.h Varun Prakash
2016-02-14 17:45 ` [RFC 28/34] cxgbit: add cxgbit_lro.h Varun Prakash
2016-02-14 17:45 ` [RFC 29/34] cxgbit: add cxgbit_main.c Varun Prakash
2016-02-14 17:45 ` [RFC 30/34] cxgbit: add cxgbit_cm.c Varun Prakash
2016-02-14 17:45 ` [RFC 31/34] cxgbit: add cxgbit_target.c Varun Prakash
2016-02-14 17:45 ` Varun Prakash [this message]
2016-02-14 17:45 ` [RFC 33/34] cxgbit: add Kconfig and Makefile Varun Prakash
2016-02-14 17:45 ` [RFC 34/34] iscsi-target: update " Varun Prakash
2016-02-26  7:29 ` [RFC 00/34] Chelsio iSCSI target offload driver Nicholas A. Bellinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=092f672bc73ded67450869f5383352b38c79ae29.1455467089.git.varun@chelsio.com \
    --to=varun@chelsio.com \
    --cc=hariprasad@chelsio.com \
    --cc=indranil@chelsio.com \
    --cc=kxie@chelsio.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=nab@linux-iscsi.org \
    --cc=roland@kernel.org \
    --cc=swise@opengridcomputing.com \
    --cc=target-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.