From patchwork Sat Jan 30 07:05:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 8169881 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EB141BEEE5 for ; Sat, 30 Jan 2016 07:09:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E8B5420395 for ; Sat, 30 Jan 2016 07:09:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C89A020397 for ; Sat, 30 Jan 2016 07:09:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756523AbcA3HJF (ORCPT ); Sat, 30 Jan 2016 02:09:05 -0500 Received: from mail-oi0-f52.google.com ([209.85.218.52]:36861 "EHLO mail-oi0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756496AbcA3HJB (ORCPT ); Sat, 30 Jan 2016 02:09:01 -0500 Received: by mail-oi0-f52.google.com with SMTP id o124so60979831oia.3 for ; Fri, 29 Jan 2016 23:09:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daterainc-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bWA21DKzO9ZUScz/HVl3JUxqcJ4Nf/0fCdbqDrJ0wXs=; b=wyG0SAx2V5mK1IgJAZ4SMVAVRwofkBVWLNR1qlc2HUp6V2ZfMCWINZTNshs+2K9wlq cYiThcOlKosV9iHdeae1q2lErmmAd+SeE8Bkh04CnQQi3C4vRuyUkieOd5d448VT7zuk 3CgKN12pIPb8rnm0IB7Q6RDsP5z6Pa2PG4mO0r2tUrmDTud5ZxksnwHZz7YHspJ0isb3 25m8YoVd5Xb/V1vviFuM824vaeoVFLmBHoI9agh9dmWYuqtvRYQoSoK/okiUs4JJqshT Vbu2uTqqmjcty+yz19wWhnb8zsp+TULLPTxroCyDB8S65LGQlyMU5sFFcow9DYVX6gUa Y9cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bWA21DKzO9ZUScz/HVl3JUxqcJ4Nf/0fCdbqDrJ0wXs=; b=g428HDKo92C81PYAoFevCXPF2A6IRrUz1kq/6OWc44UJV9VCmkQhzI+RDozWyzfL23 VVVXYhWUhiFVzMOXsOWTTj5LunkFwWY9LHw1a9JfDtMtOK04GwbVGFon2DneyX6bYy6f kR5R2Xb7R+zACa5nYF+p3QBtFhRkfyYyZMkQ1eF7WK1tUuQ15CeRWd8tiCY4GWEvItZ8 fJfq2j64qtLoZ8ZOjHFxi0slxq8Tc/7tDlAYnwvOjF4PWJ0b9GIY/vYyYYTEcn9tmDG7 uWrQnpAAHu3Yyo+TFdCvkJVjjYuM3oeu1PaghGwnM16AnAR3+YP/b1OkPZCsf/D3A1+k 4lHA== X-Gm-Message-State: AG10YORhj4+HJA4OOPiRN+PMWkW++HxUOvKhz0v/FvziELxi/0vFrb1dAQXoJxcCFMY3pA== X-Received: by 10.202.223.132 with SMTP id w126mr9614544oig.98.1454137740536; Fri, 29 Jan 2016 23:09:00 -0800 (PST) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by smtp.gmail.com with ESMTPSA id f8sm8655678obs.3.2016.01.29.23.08.59 (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 29 Jan 2016 23:09:00 -0800 (PST) From: "Nicholas A. Bellinger" To: target-devel Cc: linux-scsi , Christoph Hellwig , Hannes Reinecke , Mike Christie , Sagi Grimberg , Andy Grover , Sebastian Andrzej Siewior , Juergen Gross , Andrzej Pietrasiewicz , Chris Boot , Nicholas Bellinger Subject: [PATCH-v3 09/14] usb-gadget/tcm: Conversion to percpu_ida tag pre-allocation Date: Sat, 30 Jan 2016 07:05:53 +0000 Message-Id: <1454137558-30396-10-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1454137558-30396-1-git-send-email-nab@daterainc.com> References: <1454137558-30396-1-git-send-email-nab@daterainc.com> Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Nicholas Bellinger Cc: Sebastian Andrzej Siewior Cc: Andrzej Pietrasiewicz Signed-off-by: Nicholas Bellinger --- drivers/usb/gadget/function/f_tcm.c | 95 ++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c index 2e8b91d..8773572 100644 --- a/drivers/usb/gadget/function/f_tcm.c +++ b/drivers/usb/gadget/function/f_tcm.c @@ -1073,39 +1073,65 @@ out: usbg_cleanup_cmd(cmd); } +static struct usbg_cmd *usbg_get_cmd(struct f_uas *fu, + struct tcm_usbg_nexus *tv_nexus, u32 scsi_tag) +{ + struct se_session *se_sess = tv_nexus->tvn_se_sess; + struct usbg_cmd *cmd; + int tag; + + tag = percpu_ida_alloc(&se_sess->sess_tag_pool, GFP_ATOMIC); + if (tag < 0) + return ERR_PTR(-ENOMEM); + + cmd = &((struct usbg_cmd *)se_sess->sess_cmd_map)[tag]; + cmd->se_cmd.map_tag = tag; + cmd->se_cmd.tag = cmd->tag = scsi_tag; + cmd->fu = fu; + + return cmd; +} + +static void usbg_release_cmd(struct se_cmd *); + static int usbg_submit_command(struct f_uas *fu, void *cmdbuf, unsigned int len) { struct command_iu *cmd_iu = cmdbuf; struct usbg_cmd *cmd; - struct usbg_tpg *tpg; - struct tcm_usbg_nexus *tv_nexus; + struct usbg_tpg *tpg = fu->tpg; + struct tcm_usbg_nexus *tv_nexus = tpg->tpg_nexus; u32 cmd_len; + u16 scsi_tag; if (cmd_iu->iu_id != IU_ID_COMMAND) { pr_err("Unsupported type %d\n", cmd_iu->iu_id); return -EINVAL; } - cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); - if (!cmd) - return -ENOMEM; + tv_nexus = tpg->tpg_nexus; + if (!tv_nexus) { + pr_err("Missing nexus, ignoring command\n"); + return -EINVAL; + } - cmd->fu = fu; + cmd_len = (cmd_iu->len & ~0x3) + 16; + if (cmd_len > USBG_MAX_CMD) + return -EINVAL; + + scsi_tag = be16_to_cpup(&cmd_iu->tag); + cmd = usbg_get_cmd(fu, tv_nexus, scsi_tag); + if (IS_ERR(cmd)) { + pr_err("usbg_get_cmd failed\n"); + return -ENOMEM; + } /* XXX until I figure out why I can't free in on complete */ kref_init(&cmd->ref); kref_get(&cmd->ref); - tpg = fu->tpg; - cmd_len = (cmd_iu->len & ~0x3) + 16; - if (cmd_len > USBG_MAX_CMD) - goto err; - memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len); - cmd->tag = be16_to_cpup(&cmd_iu->tag); - cmd->se_cmd.tag = cmd->tag; if (fu->flags & USBG_USE_STREAMS) { if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS) goto err; @@ -1117,12 +1143,6 @@ static int usbg_submit_command(struct f_uas *fu, cmd->stream = &fu->stream[0]; } - tv_nexus = tpg->tpg_nexus; - if (!tv_nexus) { - pr_err("Missing nexus, ignoring command\n"); - goto err; - } - switch (cmd_iu->prio_attr & 0x7) { case UAS_HEAD_TAG: cmd->prio_attr = TCM_HEAD_TAG; @@ -1148,7 +1168,7 @@ static int usbg_submit_command(struct f_uas *fu, return 0; err: - kfree(cmd); + usbg_release_cmd(&cmd->se_cmd); return -EINVAL; } @@ -1190,7 +1210,7 @@ static int bot_submit_command(struct f_uas *fu, { struct bulk_cb_wrap *cbw = cmdbuf; struct usbg_cmd *cmd; - struct usbg_tpg *tpg; + struct usbg_tpg *tpg = fu->tpg; struct tcm_usbg_nexus *tv_nexus; u32 cmd_len; @@ -1207,28 +1227,25 @@ static int bot_submit_command(struct f_uas *fu, if (cmd_len < 1 || cmd_len > 16) return -EINVAL; - cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); - if (!cmd) - return -ENOMEM; + tv_nexus = tpg->tpg_nexus; + if (!tv_nexus) { + pr_err("Missing nexus, ignoring command\n"); + return -ENODEV; + } - cmd->fu = fu; + cmd = usbg_get_cmd(fu, tv_nexus, cbw->Tag); + if (IS_ERR(cmd)) { + pr_err("usbg_get_cmd failed\n"); + return -ENOMEM; + } /* XXX until I figure out why I can't free in on complete */ kref_init(&cmd->ref); kref_get(&cmd->ref); - tpg = fu->tpg; - memcpy(cmd->cmd_buf, cbw->CDB, cmd_len); cmd->bot_tag = cbw->Tag; - - tv_nexus = tpg->tpg_nexus; - if (!tv_nexus) { - pr_err("Missing nexus, ignoring command\n"); - goto err; - } - cmd->prio_attr = TCM_SIMPLE_TAG; cmd->unpacked_lun = cbw->Lun; cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0; @@ -1239,9 +1256,6 @@ static int bot_submit_command(struct f_uas *fu, queue_work(tpg->workqueue, &cmd->work); return 0; -err: - kfree(cmd); - return -EINVAL; } /* Start fabric.c code */ @@ -1294,8 +1308,10 @@ static void usbg_release_cmd(struct se_cmd *se_cmd) { struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, se_cmd); + struct se_session *se_sess = se_cmd->se_sess; + kfree(cmd->data_buf); - kfree(cmd); + percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag); } static int usbg_shutdown_session(struct se_session *se_sess) @@ -1597,7 +1613,8 @@ static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name) goto out_unlock; } - tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg, 0, 0, + tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg, 128, + sizeof(struct usbg_cmd), TARGET_PROT_NORMAL, name, tv_nexus, NULL); if (IS_ERR(tv_nexus->tvn_se_sess)) {