From nobody Thu Apr 9 10:36:20 2026 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87D993E8C46 for ; Tue, 10 Mar 2026 07:22:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773127365; cv=none; b=LfMBYagIWm2g0ubSZiXuamF6R3Mz7yfxjLw5/qOAqd6sbvhqAfJS1sglL3Xaqd3ylk9PFJ3bCYbjnLxhgQJiYHWlmjYYMWYxdAP8gIZIJxkR5blu+w9iObNQKi02iXdxIdXfEIfWLL06H9XMY9soLlizgm9b14NFganC/tOYvXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773127365; c=relaxed/simple; bh=P1IzXcVjmKfRaow4rEqobZUMjX/s+CE8w3S3MJsIlfk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fLprmUcYT5SvwFbtYK+IRK5ox8C2N29KiiU/UGPQA0MgH05/sXFaDADJoUulspLcJDTRHmXhOXk+jqff3VSCa5Zfjx+SR7VQDoy+KS5Rx6Wo1HJnxJUg8WBCwXwYks7lpyJBZ/A7BFGl5GLjlVgZ01E0ae5tAvpXQQ2ChGDlGX0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=AdYYoLpE; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=dYC7vMqx; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="AdYYoLpE"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="dYC7vMqx" Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 62A5lFLS568670 for ; Tue, 10 Mar 2026 07:22:42 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= SK5UC/KQfXR1hLzmHDReuvjdixzY1PbWgrbCpHiLeF0=; b=AdYYoLpEaO87vlnT G1bci/o9FUcaBCKnY+Nhm9aLjz3ufnJ2mQpyFGrTf4JP08ULENwxTkP4inhqSV2s v56poNpfZaxqdOM/XLrGR52ymm5zwCDmjMI6kaJ1fDH8n0GWbCEpbnESwPtuGQfm 3Ycl6erLa754REHAWsW0ncJAB955Mku2neUu0CigG00Zw8tuhb8YDXcGH5A7NYid 7wNkGkgwQ1Gi9kwikP3MiSKBuJIL23ZW3a5xxML9l5r9Qw/+w9k4wmJPAwcnrQVr N0Yzn0etegBhJreMCWkKDUQ6laPN2syKGVPxL4Z+J7IzmcjrnfZIc3I8hYNpXA2P o7YdQg== Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4ctdf8gaa5-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 10 Mar 2026 07:22:42 +0000 (GMT) Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2ae3badc00dso106572885ad.3 for ; Tue, 10 Mar 2026 00:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1773127361; x=1773732161; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=SK5UC/KQfXR1hLzmHDReuvjdixzY1PbWgrbCpHiLeF0=; b=dYC7vMqx9J6JSP14uZJI86FF+wbdlySSYP9Md2OdCDV0ADwIEkRnD5OU+mSfQw6iYf S1OzfsOMmanafXTsuF8m2TvMW/0JTX7L1RyOg9pj8KtdOyEl5v/VH9QJiv5fOkO4RQM4 AaUZeQTx+sy83o9MMFHTR0PEUtXHh+AcH+Q74zdMJnmQ+cUlBDKCzlA72vBxmDttrepB pwc9WPk8iAeCjOu1ieC/6OzjbcR4EeAMljJA8WloZROhSiuxxm7dSP+6/U/p1uYhvF6E VTljA6ePz2Oyj1q8nUqXW9SE04oMsM1OeQumn+S4IgonQZUjQmExwRICh+YPXfEL3mek HF6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773127361; x=1773732161; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=SK5UC/KQfXR1hLzmHDReuvjdixzY1PbWgrbCpHiLeF0=; b=IRf1APlXYJWaIq+dV6DjRn1Aw+cmVLC0M7FLrjOSFguqqUZ/TgsHmZ/PqV5ghdE6Z9 FtBHe5WdNClR0QCWAdG+KDWupgpki61yAmU+tUm8TrJSjJ1hmFgpG3CPY46qShYpeNuv U+6Kd5vW+5JFSpDbnHfQn5Q6GToivjw7f5abPr7/X19zj4dsZ9EJ6Hvcc+YhoVNrGOOy Gb2KbRHZ3rWqiLYalP7Pk9O3YfhHhv0AnFawiy3e45Nk5/vFlbIurhT+dOaiyt69iBdG +aszPEB7vr6Vd8m0lEkzj9UYwejACTq4ckB9971Ho0b1z/lWva9P4lc7y9exeZCmYybD mlVQ== X-Forwarded-Encrypted: i=1; AJvYcCVsk3v71OI3/12KlLopd8kgIPI03mvCng37dsj50Sl6M7kgxeDNGDQXILxe304sm7tBYcEpu0hOn2ybc7M=@vger.kernel.org X-Gm-Message-State: AOJu0YyPepSo/KeDDCnQhKkedb+FCiDU5OTxhs8Db2JIamXatQoSluS3 DPXCF/BE6NL5OISXCV4e4wGaz9CkqJOyLadmKaxfjAv5u6ZT79YEe+7FAglecnRCkU6XXuq1Gfd bkji0xu4yap6WKpM2cWzFDYhyzEHWwpEWABiB/7NFQjGCeMwiUg+8R/Bvr4RjXVccdYY= X-Gm-Gg: ATEYQzxwcs5qvK/TLVNx9mNF0zW/knlZcyjF+suqmFkCEjPCI4BcHJcqOyL19jlX5L5 1nwmkl3qVsKudoAFItQlBXKbk2TMU9R2qLO2Hr/lm1nqfD/PY3trd19szWsUNBqhsuwXm9B1ayF u0G4BHfmn/+amDqHQ7aGIhCXPDsAIalpN9i/fz+PkBlOH5E93lkdZ+ZXvoiYOH0TqcBzG053ZoW KEB32VwzUQxsoku79bAE6wsYipI5gXFzPa7rMQjFpV6buC49HoNUcu6DOHLQk0Zy/2zUxopmq9v zdXo+RXUANv+yUOME6qYSKy6EH+TF2xXSztSridBPDBQ5TFbGSLe3XaHwu4zPYVzqHsZ9amFk/y hOP0pCsw0XhUkopOix8jlQF0q1GLmjB7KMp/YFww//RIzEFMVjto= X-Received: by 2002:a17:902:e890:b0:2ae:5163:c2aa with SMTP id d9443c01a7336-2ae823a1852mr132415175ad.20.1773127361211; Tue, 10 Mar 2026 00:22:41 -0700 (PDT) X-Received: by 2002:a17:902:e890:b0:2ae:5163:c2aa with SMTP id d9443c01a7336-2ae823a1852mr132414985ad.20.1773127360712; Tue, 10 Mar 2026 00:22:40 -0700 (PDT) Received: from hu-batta-hyd.qualcomm.com ([202.46.23.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ae840b2e9dsm185200325ad.85.2026.03.10.00.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2026 00:22:40 -0700 (PDT) From: Kishore Batta Date: Tue, 10 Mar 2026 12:52:04 +0530 Subject: [PATCH v3 7/9] bus: mhi: Capture DDR training data using command mode Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260310-sahara_protocol_new_v2-v3-7-994ea4b0d5ad@oss.qualcomm.com> References: <20260310-sahara_protocol_new_v2-v3-0-994ea4b0d5ad@oss.qualcomm.com> In-Reply-To: <20260310-sahara_protocol_new_v2-v3-0-994ea4b0d5ad@oss.qualcomm.com> To: Jonathan Corbet , Shuah Khan , Jeff Hugo , Carl Vanderlip , Oded Gabbay , Manivannan Sadhasivam Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, mhi@lists.linux.dev, Kishore Batta X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1773127328; l=14768; i=kishore.batta@oss.qualcomm.com; s=20260206; h=from:subject:message-id; bh=P1IzXcVjmKfRaow4rEqobZUMjX/s+CE8w3S3MJsIlfk=; b=trgfPbRYn+QD4CE/IxijzPKE6ZWQQgvq2wEEbFsvFmN6sZOW6RDIzQwRRQSWiyGq8tw7Wl+6A wu9SMnmnkeMBmblMYzEpfWPxqThoTKbiYPXkIWBv1n1Ikw5nSGJ5jG9 X-Developer-Key: i=kishore.batta@oss.qualcomm.com; a=ed25519; pk=vJo8RvTf+HZpRLK2oOIljmbn9l3zFkibCGh+blaqZCw= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzEwMDA2MSBTYWx0ZWRfX64TH4p8Kp5N7 WWN1pB3Sn9HhSST967BQmPXMqBQDv62BSjTO3pgLeVKA+l93QWGuSycDdNLyREj+LH/MZlX5J3i 9iuOxNg/H3GsXjzOu8TvGKLV/9SwgL3HXM7bdbF85Cg6tgSZga7Vs6MbleN2tPgO2obrVtuLeb1 hCj3ilIo8EKchLPNK4UTIbNCWTJ9Tfqbh2Hpf5vAWZSyc37XVDEE9/jJGu2FmVjMgtJ4zwuY+Ex V9LDb6ujEYRMR/rxz/BGpcNClq9A8NIjCXvK1f/ScN3V1/Us9tjia7iVbUQvQ7ez28iSp/2cDSK GutCPo46qRxH9QIeHb6S48O07yeKCZHtTTj1iPht5vKzHLQ0j7UCzOTp44xNuWJ1FIGGZlnpc4+ 8ITiQTWI1PpQ0/nESL5QzIG08xRH2gKLDzqQo+DLZUgXiYUNZqijX7IA9TKIPAjxwfBJGCopNkY W/oWLT5A0atv5c0TKag== X-Proofpoint-ORIG-GUID: FwenjpvgpRv8Kq1kOfHiMg6-Cc2LLrk_ X-Proofpoint-GUID: FwenjpvgpRv8Kq1kOfHiMg6-Cc2LLrk_ X-Authority-Analysis: v=2.4 cv=b+W/I9Gx c=1 sm=1 tr=0 ts=69afc6c2 cx=c_pps a=MTSHoo12Qbhz2p7MsH1ifg==:117 a=ZePRamnt/+rB5gQjfz0u9A==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=_glEPmIy2e8OvE2BGh3C:22 a=EUspDBNiAAAA:8 a=_u4x-iUupSARUiuVX_kA:9 a=QEXdDO2ut3YA:10 a=GvdueXVYPmCkWapjIL-Q:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-03-10_01,2026-03-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 adultscore=0 suspectscore=0 phishscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 malwarescore=0 lowpriorityscore=0 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2602130000 definitions=main-2603100061 During early boot, devices may perform DDR training and produce training data that can be reused on subsequent boots to reduce initialization time. The sahara protocol provides a command mode flow to transfer this training data to the host, but the driver currently does not handle command mode and drops the training payload. Add Sahara command mode support to retrieve DDR training data from the device. When the device enters command mode and sends CMD_READY, query the support command list and request DDR training data using EXECUTE and EXECUTE_DATA. Allocate receive buffers based on the reported response size and copy the raw payload directly from the MHI DL completion callback. Store the captured training data in controller-scoped memory using devres, so it remains available after sahara channel teardown. Also distinguish raw payload completion from control packets in the DL callback, avoiding misinterpretation of training data as protocol messages, and requeue the RX buffer after switching back to IMAGE_TX_PENDING to allow the boot flow to continue. Signed-off-by: Kishore Batta --- drivers/bus/mhi/sahara/sahara.c | 328 ++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 320 insertions(+), 8 deletions(-) diff --git a/drivers/bus/mhi/sahara/sahara.c b/drivers/bus/mhi/sahara/sahar= a.c index 0a0f578aaa47ab2c4ca0765666b392fb9936ddd5..c88f1220199ac4373d355216787= 0c19a0d5f23b9 100644 --- a/drivers/bus/mhi/sahara/sahara.c +++ b/drivers/bus/mhi/sahara/sahara.c @@ -5,11 +5,14 @@ */ =20 #include +#include +#include #include #include #include #include #include +#include #include #include #include @@ -60,8 +63,16 @@ #define SAHARA_RESET_LENGTH 0x8 #define SAHARA_MEM_DEBUG64_LENGTH 0x18 #define SAHARA_MEM_READ64_LENGTH 0x18 - +#define SAHARA_COMMAND_READY_LENGTH 0x8 +#define SAHARA_COMMAND_EXEC_RESP_LENGTH 0x10 +#define SAHARA_COMMAND_EXECUTE_LENGTH 0xc +#define SAHARA_COMMAND_EXEC_DATA_LENGTH 0xc +#define SAHARA_SWITCH_MODE_LENGTH 0xc + +#define SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST 0x8 +#define SAHARA_EXEC_CMD_GET_TRAINING_DATA 0x9 #define SAHARA_DDR_TRAINING_IMG_ID 34 +#define SAHARA_NUM_CMD_BUF SAHARA_NUM_TX_BUF =20 struct sahara_packet { __le32 cmd; @@ -97,6 +108,19 @@ struct sahara_packet { __le64 memory_address; __le64 memory_length; } memory_read64; + struct { + __le32 client_command; + } command_execute; + struct { + __le32 client_command; + __le32 response_length; + } command_execute_resp; + struct { + __le32 client_command; + } command_exec_data; + struct { + __le32 mode; + } mode_switch; }; }; =20 @@ -163,6 +187,7 @@ struct sahara_context { struct work_struct fw_work; struct work_struct dump_work; struct work_struct read_data_work; + struct work_struct cmd_work; struct mhi_device *mhi_dev; const char * const *image_table; u32 table_size; @@ -183,6 +208,24 @@ struct sahara_context { bool is_mem_dump_mode; bool non_streaming; const char *fw_folder; + bool is_cmd_mode; + bool receiving_trng_data; + size_t trng_size; + size_t trng_rcvd; + u32 trng_nbuf; + char *cmd_buff[SAHARA_NUM_CMD_BUF]; +}; + +/* + * Controller-scoped training data store (per MHI controller device). + * Stored as devres resource on mhi_dev->mhi_cntrl->mhi_dev->dev. + */ +struct sahara_ctrl_trng_data { + struct mutex lock; /* Protects data, size, copied and receiving */ + void *data; + size_t size; + size_t copied; + bool receiving; }; =20 struct sahara_variant { @@ -330,6 +373,48 @@ static int sahara_request_fw(struct sahara_context *co= ntext, const char *path) return ret; } =20 +static void sahara_ctrl_trng_release(struct device *dev, void *res) +{ + struct sahara_ctrl_trng_data *ct =3D res; + + mutex_lock(&ct->lock); + kfree(ct->data); + ct->data =3D NULL; + ct->size =3D 0; + ct->copied =3D 0; + ct->receiving =3D false; + mutex_unlock(&ct->lock); +} + +static int sahara_ctrl_trng_match(struct device *dev, void *res, void *mat= ch_data) +{ + /* Exactly one instance per controller */ + return 1; +} + +static struct sahara_ctrl_trng_data *sahara_ctrl_trng_get(struct device *d= ev) +{ + struct sahara_ctrl_trng_data *ct; + + ct =3D devres_find(dev, sahara_ctrl_trng_release, + sahara_ctrl_trng_match, NULL); + if (ct) + return ct; + + ct =3D devres_alloc(sahara_ctrl_trng_release, sizeof(*ct), GFP_KERNEL); + if (!ct) + return NULL; + + mutex_init(&ct->lock); + ct->data =3D NULL; + ct->size =3D 0; + ct->copied =3D 0; + ct->receiving =3D false; + + devres_add(dev, ct); + return ct; +} + static int sahara_find_image(struct sahara_context *context, u32 image_id) { char *fw_path; @@ -423,6 +508,11 @@ static void sahara_send_reset(struct sahara_context *c= ontext) context->is_mem_dump_mode =3D false; context->read_data_offset =3D 0; context->read_data_length =3D 0; + context->is_cmd_mode =3D false; + context->receiving_trng_data =3D false; + context->trng_size =3D 0; + context->trng_rcvd =3D 0; + context->trng_nbuf =3D 0; =20 context->tx[0]->cmd =3D cpu_to_le32(SAHARA_RESET_CMD); context->tx[0]->length =3D cpu_to_le32(SAHARA_RESET_LENGTH); @@ -458,7 +548,8 @@ static void sahara_hello(struct sahara_context *context) =20 if (le32_to_cpu(context->rx->hello.mode) !=3D SAHARA_MODE_IMAGE_TX_PENDIN= G && le32_to_cpu(context->rx->hello.mode) !=3D SAHARA_MODE_IMAGE_TX_COMPLE= TE && - le32_to_cpu(context->rx->hello.mode) !=3D SAHARA_MODE_MEMORY_DEBUG) { + le32_to_cpu(context->rx->hello.mode) !=3D SAHARA_MODE_MEMORY_DEBUG && + le32_to_cpu(context->rx->hello.mode) !=3D SAHARA_MODE_COMMAND) { dev_err(&context->mhi_dev->dev, "Unsupported hello packet - mode %d\n", le32_to_cpu(context->rx->hello.mode)); return; @@ -477,6 +568,153 @@ static void sahara_hello(struct sahara_context *conte= xt) dev_err(&context->mhi_dev->dev, "Unable to send hello response %d\n", re= t); } =20 +static void sahara_switch_mode_to_img_tx(struct sahara_context *context) +{ + int ret; + + context->tx[0]->cmd =3D cpu_to_le32(SAHARA_SWITCH_MODE_CMD); + context->tx[0]->length =3D cpu_to_le32(SAHARA_SWITCH_MODE_LENGTH); + context->tx[0]->mode_switch.mode =3D cpu_to_le32(SAHARA_MODE_IMAGE_TX_PEN= DING); + + ret =3D mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0], + SAHARA_SWITCH_MODE_LENGTH, MHI_EOT); + + if (ret) + dev_err(&context->mhi_dev->dev, "Unable to send mode switch %d\n", ret); +} + +static void sahara_command_execute(struct sahara_context *context, u32 cli= ent_command) +{ + int ret; + + context->tx[0]->cmd =3D cpu_to_le32(SAHARA_EXECUTE_CMD); + context->tx[0]->length =3D cpu_to_le32(SAHARA_COMMAND_EXECUTE_LENGTH); + context->tx[0]->command_execute.client_command =3D cpu_to_le32(client_com= mand); + + ret =3D mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0], + SAHARA_COMMAND_EXECUTE_LENGTH, MHI_EOT); + if (ret) + dev_err(&context->mhi_dev->dev, "Unable to send command execute %d\n", r= et); +} + +static void sahara_command_execute_data(struct sahara_context *context, u3= 2 client_command) +{ + int ret; + + context->tx[0]->cmd =3D cpu_to_le32(SAHARA_EXECUTE_DATA_CMD); + context->tx[0]->length =3D cpu_to_le32(SAHARA_COMMAND_EXEC_DATA_LENGTH); + context->tx[0]->command_exec_data.client_command =3D cpu_to_le32(client_c= ommand); + + ret =3D mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0], + SAHARA_COMMAND_EXEC_DATA_LENGTH, MHI_EOT); + if (ret) + dev_err(&context->mhi_dev->dev, "Unable to send execute data %d\n", ret); +} + +static void sahara_command_ready(struct sahara_context *context) +{ + if (le32_to_cpu(context->rx->length) !=3D SAHARA_COMMAND_READY_LENGTH) { + dev_err(&context->mhi_dev->dev, + "Malformed command ready packet - length %u\n", + le32_to_cpu(context->rx->length)); + return; + } + + context->is_cmd_mode =3D true; + context->receiving_trng_data =3D false; + + sahara_command_execute(context, SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST); +} + +static void sahara_command_execute_resp(struct sahara_context *context) +{ + struct device *dev =3D &context->mhi_dev->mhi_cntrl->mhi_dev->dev; + struct sahara_ctrl_trng_data *ct; + u32 client_cmd, resp_len; + int ret; + u64 remaining; + u32 i; + + if (le32_to_cpu(context->rx->length) !=3D SAHARA_COMMAND_EXEC_RESP_LENGTH= || + le32_to_cpu(context->rx->command_execute_resp.response_length) < 0) { + dev_err(&context->mhi_dev->dev, + "Malformed command execute resp packet - length %d\n", + le32_to_cpu(context->rx->length)); + return; + } + + client_cmd =3D le32_to_cpu(context->rx->command_execute_resp.client_comma= nd); + resp_len =3D le32_to_cpu(context->rx->command_execute_resp.response_lengt= h); + + sahara_command_execute_data(context, client_cmd); + + if (client_cmd =3D=3D SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST) { + sahara_command_execute(context, SAHARA_EXEC_CMD_GET_TRAINING_DATA); + return; + } + + if (client_cmd !=3D SAHARA_EXEC_CMD_GET_TRAINING_DATA) + return; + + ct =3D sahara_ctrl_trng_get(dev); + if (!ct) { + context->is_cmd_mode =3D false; + sahara_switch_mode_to_img_tx(context); + return; + } + + mutex_lock(&ct->lock); + kfree(ct->data); + ct->data =3D kzalloc(resp_len, GFP_KERNEL); + ct->size =3D resp_len; + ct->copied =3D 0; + ct->receiving =3D true; + mutex_unlock(&ct->lock); + + if (!ct->data) { + context->is_cmd_mode =3D false; + sahara_switch_mode_to_img_tx(context); + return; + } + + context->trng_size =3D resp_len; + context->trng_rcvd =3D 0; + context->receiving_trng_data =3D true; + + remaining =3D resp_len; + for (i =3D 0; i < SAHARA_NUM_CMD_BUF && remaining; i++) { + size_t pkt =3D min_t(size_t, remaining, SAHARA_PACKET_MAX_SIZE); + + ret =3D mhi_queue_buf(context->mhi_dev, DMA_FROM_DEVICE, + context->cmd_buff[i], pkt, + (remaining <=3D pkt) ? MHI_EOT : MHI_CHAIN); + if (ret) + break; + + remaining -=3D pkt; + } + + context->trng_nbuf =3D i; +} + +static void sahara_command_processing(struct work_struct *work) +{ + struct sahara_context *context =3D container_of(work, struct sahara_conte= xt, cmd_work); + int ret; + + if (le32_to_cpu(context->rx->cmd) =3D=3D SAHARA_EXECUTE_RESP_CMD) + sahara_command_execute_resp(context); + + if (!context->receiving_trng_data) { + ret =3D mhi_queue_buf(context->mhi_dev, DMA_FROM_DEVICE, + context->rx, SAHARA_PACKET_MAX_SIZE, MHI_EOT); + + if (ret) + dev_err(&context->mhi_dev->dev, + "Unable to requeue rx buf %d\n", ret); + } +} + static int read_data_helper(struct sahara_context *context, int buf_index) { enum mhi_flags mhi_flag; @@ -703,6 +941,9 @@ static void sahara_processing(struct work_struct *work) case SAHARA_MEM_DEBUG64_CMD: sahara_memory_debug64(context); break; + case SAHARA_CMD_READY_CMD: + sahara_command_ready(context); + break; default: dev_err(&context->mhi_dev->dev, "Unknown command %d\n", le32_to_cpu(context->rx->cmd)); @@ -1003,6 +1244,20 @@ static int sahara_mhi_probe(struct mhi_device *mhi_d= ev, const struct mhi_device_ INIT_WORK(&context->fw_work, sahara_processing); INIT_WORK(&context->dump_work, sahara_dump_processing); INIT_WORK(&context->read_data_work, sahara_read_data_processing); + INIT_WORK(&context->cmd_work, sahara_command_processing); + + for (i =3D 0; i < SAHARA_NUM_CMD_BUF; i++) { + context->cmd_buff[i] =3D devm_kzalloc(&mhi_dev->dev, + SAHARA_PACKET_MAX_SIZE, GFP_KERNEL); + if (!context->cmd_buff[i]) + return -ENOMEM; + } + + context->is_cmd_mode =3D false; + context->receiving_trng_data =3D false; + context->trng_size =3D 0; + context->trng_rcvd =3D 0; + context->trng_nbuf =3D 0; =20 context->active_image_id =3D SAHARA_IMAGE_ID_NONE; dev_set_drvdata(&mhi_dev->dev, context); @@ -1026,6 +1281,7 @@ static void sahara_mhi_remove(struct mhi_device *mhi_= dev) =20 cancel_work_sync(&context->fw_work); cancel_work_sync(&context->dump_work); + cancel_work_sync(&context->cmd_work); vfree(context->mem_dump); sahara_release_image(context); mhi_unprepare_from_transfer(mhi_dev); @@ -1042,15 +1298,71 @@ static void sahara_mhi_ul_xfer_cb(struct mhi_device= *mhi_dev, struct mhi_result static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_r= esult *mhi_result) { struct sahara_context *context =3D dev_get_drvdata(&mhi_dev->dev); + struct sahara_ctrl_trng_data *ct; + struct device *dev; + size_t copy; + int ret; + u32 i; + + if (mhi_result->transaction_status) + return; + + /* + * Raw training payload completions arrive for cmd_buff[] buffers. + * Do not schedule cmd_work for those. + */ + if (context->is_cmd_mode && context->receiving_trng_data && + mhi_result->buf_addr !=3D context->rx) { + dev =3D &context->mhi_dev->mhi_cntrl->mhi_dev->dev; + ct =3D sahara_ctrl_trng_get(dev); + if (!ct) + return; =20 - if (!mhi_result->transaction_status) { - context->rx_size =3D mhi_result->bytes_xferd; - if (context->is_mem_dump_mode) - schedule_work(&context->dump_work); - else - schedule_work(&context->fw_work); + for (i =3D 0; i < context->trng_nbuf; i++) { + if (mhi_result->buf_addr =3D=3D context->cmd_buff[i]) { + mutex_lock(&ct->lock); + copy =3D min_t(size_t, mhi_result->bytes_xferd, + ct->size - ct->copied); + memcpy((u8 *)ct->data + ct->copied, + mhi_result->buf_addr, copy); + ct->copied +=3D copy; + mutex_unlock(&ct->lock); + + context->trng_rcvd +=3D copy; + + if (context->trng_rcvd >=3D context->trng_size) { + mutex_lock(&ct->lock); + ct->receiving =3D false; + mutex_unlock(&ct->lock); + + context->receiving_trng_data =3D false; + context->is_cmd_mode =3D false; + + sahara_switch_mode_to_img_tx(context); + ret =3D mhi_queue_buf(context->mhi_dev, + DMA_FROM_DEVICE, + context->rx, + SAHARA_PACKET_MAX_SIZE, + MHI_EOT); + if (ret) + dev_err(&context->mhi_dev->dev, + "Unable to requeue rx buf %d\n", ret); + } + return; + } + } + return; } =20 + /* Normal Rx completion */ + context->rx_size =3D mhi_result->bytes_xferd; + if (context->is_mem_dump_mode) + schedule_work(&context->dump_work); + else if (context->is_cmd_mode) + schedule_work(&context->cmd_work); + else + schedule_work(&context->fw_work); + } =20 static const struct mhi_device_id sahara_mhi_match_table[] =3D { --=20 2.34.1