scsi: ufs: core: mcq: Fix the incorrect OCS value for the device command
authorStanley Chu <stanley.chu@mediatek.com>
Sat, 10 Jun 2023 02:15:51 +0000 (10:15 +0800)
committerMartin K. Petersen <martin.petersen@oracle.com>
Fri, 16 Jun 2023 16:04:30 +0000 (12:04 -0400)
commit0fef6bb730c490fcdc4347dbd21646d3ffe62cf5
tree6a2ea645048ab097a43444b9e39d5de622e055ea
parente246514ae698813ab1eadb1600aeea0a0c959769
scsi: ufs: core: mcq: Fix the incorrect OCS value for the device command

In MCQ mode, when a device command uses a hardware queue shared with other
commands, a race condition may occur in the following scenario:

 1. A device command is completed in CQx with CQE entry "e".

 2. The interrupt handler copies the "cqe" pointer to "hba->dev_cmd.cqe"
    and completes "hba->dev_cmd.complete".

 3. The "ufshcd_wait_for_dev_cmd()" function is awakened and retrieves the
    OCS value from "hba->dev_cmd.cqe".

However, there is a possibility that the CQE entry "e" will be overwritten
by newly completed commands in CQx, resulting in an incorrect OCS value
being received by "ufshcd_wait_for_dev_cmd()".

To avoid this race condition, the OCS value should be immediately copied to
the struct "lrb" of the device command. Then "ufshcd_wait_for_dev_cmd()"
can retrieve the OCS value from the struct "lrb".

Fixes: 57b1c0ef89ac ("scsi: ufs: core: mcq: Add support to allocate multiple queues")
Suggested-by: Can Guo <quic_cang@quicinc.com>
Signed-off-by: Stanley Chu <stanley.chu@mediatek.com>
Link: https://lore.kernel.org/r/20230610021553.1213-2-powen.kao@mediatek.com
Tested-by: Po-Wen Kao <powen.kao@mediatek.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/ufs/core/ufshcd.c
include/ufs/ufshcd.h