Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3673771imm; Tue, 29 May 2018 11:20:19 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoDlNKevziX7vFXIoG0PNcAljWeFLK7rgNzrDTz8cAq7onzJa+TaIZMzBgKoqgJDKOo1H1y X-Received: by 2002:a17:902:2805:: with SMTP id e5-v6mr18922300plb.55.1527618019287; Tue, 29 May 2018 11:20:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527618019; cv=none; d=google.com; s=arc-20160816; b=L9JEhZMdnTeGJ7T3GJ4Af43wy0NeVcMJnwEg6HXg4vlpKvFunmB4Bdoy49hP9N+q/i WJqVwcqlwuhfxInvNtRGv7XstmJtC1GQsWfGYZ9ORvQ385IoVzTR3mpBDjwnjBDysB5m eMGpM9YyogRa3IqigDbCLKr8enQ1Hx5iZk/2Uq50R4/uwCl96YKwnEYaJtk1vFzY7ha7 06l+Jcesnr+OJiyg+1XUzL0jq2EE6r29z8BbnwhFWiq4rVrsqddVBdL2nTp91ZJyhxVx 0foa7pxMFsPmfbejIj2/qnEO32HSUDj6RA8vdZHCD1mhczz6WebH1njBrX1rLFAF/HKx XUxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=xGc/URFRPxMvy71S2vPJMNRAuNvk/D0jUEHotBruZeE=; b=KMx5B3qicjVVBH2Jm2x4CzYpkblpOlZUNfsOEYB02e62V/zjVRCZKrHwmVqNb7HAwm hlkeX7h2k9Usf6iVSFisfdUr38E6eG+aNkrOLfLFbTFfHR7mQU2PzuXG2CQFl5dZLqwt fONOch/ugFV0QKmTF7SrfDtOh3rsM7cWLgfpXrYPyn1ZfnKHPuq0MJYcMpH8ePOE66cb rmvV9CKG5Zv0pofGiE3OteMCJo2r81AwdD5xzKBnhXas6mxxjtO9PFZXU4+ku9f9ecSY NCrcDoN2ts6pobPiXFsVVlx5daebqu9yuJOUfXoWZYgsj5thHXJeImaSDCVqCnNSHFyy +DTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PWwlgnlb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9-v6si33881585pfk.228.2018.05.29.11.20.05; Tue, 29 May 2018 11:20:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PWwlgnlb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966098AbeE2STO (ORCPT + 99 others); Tue, 29 May 2018 14:19:14 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:35315 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S937033AbeE2SSU (ORCPT ); Tue, 29 May 2018 14:18:20 -0400 Received: by mail-pf0-f196.google.com with SMTP id x9-v6so7677179pfm.2 for ; Tue, 29 May 2018 11:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xGc/URFRPxMvy71S2vPJMNRAuNvk/D0jUEHotBruZeE=; b=PWwlgnlbeblUXsWZ4bnTi3lCHjgvmgFTBq8NeE23/ZgsF/4hRG2unpKmhgWAXcc9HO cD+Ps/ZI+oq7tOKbxe8DVhe8UUVCsLx3AdfsZqW5+EsfFRxwV+T+Ics7ECy42eYMx4nq KqGcMWLxPJs7CoXYOTMRbrKB/5WHfR0L3PQpg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xGc/URFRPxMvy71S2vPJMNRAuNvk/D0jUEHotBruZeE=; b=AVXJF1GhBTgPUhhQpHBhhrMPq2zqV7m3oq6Gjh9cgNmfYjITXseL/kqlUhPE9GCsv1 m7WCxXEONYtpAoEFktAJXyJOBvJ5psPaTWj2cB4eAjbpi4Frq2sboK/e5UiH/+Yx3ndY kY0ZRAADZuLRP4693pheS1DgHzeB/MaQv7M8NnNA7a3fdxI0f9eX0pSf9jMoWm5cpG3D D9mfeGpfQGs6Z6RFTu+i4ahqIopUO5j5wK2W0oK0gfZvp+Hc8yfu+sGVes6Srz+rKghn iSywyIF7j4sxIbGT8MXUj4EXSCgrPW9658Rfi5lN3epVblL1kl84kZ1NtLQuy17Wj5W7 fKUQ== X-Gm-Message-State: ALKqPwfrFXlGx1quYCj74hT8Oaqs4XBdftYiEhhaC5Imnw+klRDobvIg xs7DixtqO56JOp0pkFmlRSppcg== X-Received: by 2002:a65:4348:: with SMTP id k8-v6mr4022778pgq.341.1527617899840; Tue, 29 May 2018 11:18:19 -0700 (PDT) Received: from evgreen2.mtv.corp.google.com ([2620:0:1000:1511:116f:8bf3:133b:f7fd]) by smtp.gmail.com with ESMTPSA id b74-v6sm18255704pfl.138.2018.05.29.11.18.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 29 May 2018 11:18:18 -0700 (PDT) From: Evan Green To: Vinayak Holikatti , "James E.J. Bottomley" , "Martin K. Petersen" , Stanislav Nijnikov , linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Cc: Gwendal Grignou , Evan Green Subject: [PATCH 5/7] scsi: ufs: Refactor descriptor read for write Date: Tue, 29 May 2018 11:17:38 -0700 Message-Id: <20180529181740.195362-6-evgreen@chromium.org> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20180529181740.195362-1-evgreen@chromium.org> References: <20180529181740.195362-1-evgreen@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This change refactors the ufshcd_read_desc_param function into one that can both read and write descriptors. Most of the low-level plumbing for writing to descriptors was already there, this change simply enables that code path, and manages the incoming data buffer appropriately. Signed-off-by: Evan Green --- drivers/scsi/ufs/ufs-sysfs.c | 4 +- drivers/scsi/ufs/ufshcd.c | 89 ++++++++++++++++++++++++++++++++------------ drivers/scsi/ufs/ufshcd.h | 13 ++++--- 3 files changed, 74 insertions(+), 32 deletions(-) diff --git a/drivers/scsi/ufs/ufs-sysfs.c b/drivers/scsi/ufs/ufs-sysfs.c index 51bf54e6b47a..623c56074572 100644 --- a/drivers/scsi/ufs/ufs-sysfs.c +++ b/drivers/scsi/ufs/ufs-sysfs.c @@ -227,8 +227,8 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba, if (param_size > 8) return -EINVAL; - ret = ufshcd_read_desc_param(hba, desc_id, desc_index, - param_offset, desc_buf, param_size); + ret = ufshcd_rw_desc_param(hba, UPIU_QUERY_OPCODE_READ_DESC, desc_id, + desc_index, param_offset, desc_buf, param_size); if (ret) return -EINVAL; switch (param_size) { diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index 00e79057f870..6a5939fb5da3 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -3004,22 +3004,24 @@ int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); /** - * ufshcd_read_desc_param - read the specified descriptor parameter + * ufshcd_rw_desc_param - read or write the specified descriptor parameter * @hba: Pointer to adapter instance + * @opcode: indicates whether to read or write * @desc_id: descriptor idn value * @desc_index: descriptor index - * @param_offset: offset of the parameter to read - * @param_read_buf: pointer to buffer where parameter would be read - * @param_size: sizeof(param_read_buf) + * @param_offset: offset of the parameter to read or write + * @param_buf: pointer to buffer to be read or written + * @param_size: sizeof(param_buf) * * Return 0 in case of success, non-zero otherwise */ -int ufshcd_read_desc_param(struct ufs_hba *hba, - enum desc_idn desc_id, - int desc_index, - u8 param_offset, - u8 *param_read_buf, - u8 param_size) +int ufshcd_rw_desc_param(struct ufs_hba *hba, + enum query_opcode opcode, + enum desc_idn desc_id, + int desc_index, + u8 param_offset, + u8 *param_buf, + u8 param_size) { int ret; u8 *desc_buf; @@ -3042,24 +3044,57 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, return ret; } + if (param_offset > buff_len) + return 0; + /* Check whether we need temp memory */ if (param_offset != 0 || param_size < buff_len) { - desc_buf = kmalloc(buff_len, GFP_KERNEL); + desc_buf = kzalloc(buff_len, GFP_KERNEL); if (!desc_buf) return -ENOMEM; + + /* If it's a write, first read the complete descriptor, then + * copy in the parts being changed. + */ + if (opcode == UPIU_QUERY_OPCODE_WRITE_DESC) { + if ((int)param_offset + (int)param_size > buff_len) { + ret = -EINVAL; + goto out; + } + + ret = ufshcd_query_descriptor_retry(hba, + UPIU_QUERY_OPCODE_READ_DESC, + desc_id, desc_index, 0, + desc_buf, &buff_len); + + if (ret) { + dev_err(hba->dev, + "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", + __func__, desc_id, desc_index, + param_offset, ret); + + goto out; + } + + memcpy(desc_buf + param_offset, param_buf, param_size); + } + } else { - desc_buf = param_read_buf; + desc_buf = param_buf; is_kmalloc = false; } - /* Request for full descriptor */ - ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, + /* Read or write the entire descriptor. */ + ret = ufshcd_query_descriptor_retry(hba, opcode, desc_id, desc_index, 0, desc_buf, &buff_len); if (ret) { - dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", - __func__, desc_id, desc_index, param_offset, ret); + dev_err(hba->dev, "%s: Failed %s descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", + __func__, + opcode == UPIU_QUERY_OPCODE_READ_DESC ? + "reading" : "writing", + desc_id, desc_index, param_offset, ret); goto out; } @@ -3071,12 +3106,16 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, goto out; } - /* Check wherher we will not copy more data, than available */ - if (is_kmalloc && param_size > buff_len) - param_size = buff_len; + /* Copy data to the output. The offset is already validated to be + * within the buffer. + */ + if (is_kmalloc && (opcode == UPIU_QUERY_OPCODE_READ_DESC)) { + if ((int)param_offset + (int)param_size > buff_len) + param_size = buff_len - param_offset; + + memcpy(param_buf, &desc_buf[param_offset], param_size); + } - if (is_kmalloc) - memcpy(param_read_buf, &desc_buf[param_offset], param_size); out: if (is_kmalloc) kfree(desc_buf); @@ -3089,7 +3128,8 @@ static inline int ufshcd_read_desc(struct ufs_hba *hba, u8 *buf, u32 size) { - return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); + return ufshcd_rw_desc_param(hba, UPIU_QUERY_OPCODE_READ_DESC, desc_id, + desc_index, 0, buf, size); } static inline int ufshcd_read_power_desc(struct ufs_hba *hba, @@ -3195,8 +3235,9 @@ static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, if (!ufs_is_valid_unit_desc_lun(lun)) return -EOPNOTSUPP; - return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, - param_offset, param_read_buf, param_size); + return ufshcd_rw_desc_param(hba, UPIU_QUERY_OPCODE_READ_DESC, + QUERY_DESC_IDN_UNIT, lun, param_offset, + param_read_buf, param_size); } /** diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h index fb6dcc490f21..ebbd9d580c4b 100644 --- a/drivers/scsi/ufs/ufshcd.h +++ b/drivers/scsi/ufs/ufshcd.h @@ -853,12 +853,13 @@ int ufshcd_query_descriptor_retry(struct ufs_hba *hba, enum desc_idn idn, u8 index, u8 selector, u8 *desc_buf, int *buf_len); -int ufshcd_read_desc_param(struct ufs_hba *hba, - enum desc_idn desc_id, - int desc_index, - u8 param_offset, - u8 *param_read_buf, - u8 param_size); +int ufshcd_rw_desc_param(struct ufs_hba *hba, + enum query_opcode opcode, + enum desc_idn desc_id, + int desc_index, + u8 param_offset, + u8 *param_read_buf, + u8 param_size); int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, u32 *attr_val); int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, -- 2.13.5