Skip to contentSkip to conversation

mmc: rtsx: improve rw performance

PATCH Superseded Ricky WU 2 participants 5 messages Dec 3, 2021 – Dec 9, 2021 MMC, SD and SDIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ricky WU

author
#0 lore raw

PATCH 1/1 mmc: rtsx: improve rw performance

sd_check_seq() to distinguish sequential rw or normal rw if this data is sequential call to sd_rw_sequential()

---
 drivers/mmc/host/rtsx_pci_sdmmc.c | 185 +++++++++++++++++++++++++++++-
 1 file changed, 180 insertions(+), 5 deletions(-)
drivers/mmc/host/rtsx_pci_sdmmc.c +180 -5
index 58cfaffa3c2d..9eb433b1f6f8 100644
@@ -22,6 +22,8 @@
 #include <asm/unaligned.h>
 #include <linux/pm_runtime.h>
 
+enum RW_MODE	{NORMAL_RW, SEQ_RW};
+
 struct realtek_pci_sdmmc {
 	struct platform_device	*pdev;
 	struct rtsx_pcr		*pcr;
@@ -31,6 +33,7 @@ struct realtek_pci_sdmmc {
 
 	struct work_struct	work;
 	struct mutex		host_mutex;
+	struct delayed_work		rw_idle_work;
 
 	u8			ssc_depth;
 	unsigned int		clock;
@@ -46,6 +49,12 @@ struct realtek_pci_sdmmc {
 	s32			cookie;
 	int			cookie_sg_count;
 	bool			using_cookie;
+
+	enum RW_MODE		rw_mode;
+	u8		prev_dir;
+	u8		cur_dir;
+	u64		prev_sec_addr;
+	u32		prev_sec_cnt;
 };
 
 static int sdmmc_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios);
@@ -226,6 +235,14 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
 	dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
 			__func__, cmd_idx, arg);
 
+	if (cmd_idx == MMC_SEND_STATUS && host->rw_mode == SEQ_RW) {
+		cmd->resp[0] = R1_READY_FOR_DATA | (R1_STATE_TRAN << 9);
+		goto out;
+	}
+
+	if (!mmc_op_multi(cmd->opcode))
+		host->rw_mode = NORMAL_RW;
+
 	rsp_type = sd_response_type(cmd);
 	if (rsp_type < 0)
 		goto out;
@@ -542,6 +559,93 @@ static int sd_write_long_data(struct realtek_pci_sdmmc *host,
 	return 0;
 }
 
+static int sd_rw_sequential(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+	struct rtsx_pcr *pcr = host->pcr;
+	struct mmc_host *mmc = host->mmc;
+	struct mmc_card *card = mmc->card;
+	struct mmc_data *data = mrq->data;
+	int uhs = mmc_card_uhs(card);
+	u8 cfg2;
+	int err;
+	size_t data_len = data->blksz * data->blocks;
+
+	cfg2 = SD_NO_CALCULATE_CRC7 | SD_CHECK_CRC16 |
+		SD_NO_WAIT_BUSY_END | SD_NO_CHECK_CRC7 | SD_RSP_LEN_0;
+
+	if (!uhs)
+		cfg2 |= SD_NO_CHECK_WAIT_CRC_TO;
+
+	rtsx_pci_init_cmd(pcr);
+	sd_cmd_set_data_len(pcr, data->blocks, data->blksz);
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
+			DMA_DONE_INT, DMA_DONE_INT);
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3,
+		0xFF, (u8)(data_len >> 24));
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2,
+		0xFF, (u8)(data_len >> 16));
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1,
+		0xFF, (u8)(data_len >> 8));
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)data_len);
+
+	if (host->cur_dir == DMA_DIR_FROM_CARD)
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+			0x03 | DMA_PACK_SIZE_MASK,
+			DMA_DIR_FROM_CARD | DMA_EN | DMA_512);
+	else
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+			0x03 | DMA_PACK_SIZE_MASK,
+			DMA_DIR_TO_CARD | DMA_EN | DMA_512);
+
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
+			0x01, RING_BUFFER);
+	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, cfg2);
+
+	if (host->cur_dir == DMA_DIR_FROM_CARD)
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
+				SD_TRANSFER_START | SD_TM_AUTO_READ_3);
+	else
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
+				SD_TRANSFER_START | SD_TM_AUTO_WRITE_3);
+
+	rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
+			SD_TRANSFER_END, SD_TRANSFER_END);
+	rtsx_pci_send_cmd_no_wait(pcr);
+
+	if (host->cur_dir == DMA_DIR_FROM_CARD)
+		err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 1, 10000);
+	else
+		err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 0, 10000);
+
+	if (err < 0) {
+		sd_clear_error(host);
+		return err;
+	}
+
+	return 0;
+}
+
+static int sd_stop_sequential(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+	struct rtsx_pcr *pcr = host->pcr;
+	struct mmc_command *cmd;
+
+	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+	cmd->opcode = MMC_STOP_TRANSMISSION;
+	cmd->arg = 0;
+	cmd->busy_timeout = 0;
+	if (host->cur_dir == DMA_DIR_FROM_CARD)
+		cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+	else
+		cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+	sd_send_cmd_get_rsp(host, cmd);
+	udelay(50);
+	rtsx_pci_write_register(pcr, RBCTL, RB_FLUSH, RB_FLUSH);
+	kfree(cmd);
+	return 0;
+}
+
 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc *host)
 {
 	rtsx_pci_write_register(host->pcr, SD_CFG1,
@@ -796,6 +900,45 @@ static inline int sd_rw_cmd(struct mmc_command *cmd)
 		(cmd->opcode == MMC_WRITE_BLOCK);
 }
 
+static void sd_rw_idle_work(struct work_struct *work)
+{
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct realtek_pci_sdmmc *host = container_of(dwork,
+			struct realtek_pci_sdmmc, rw_idle_work);
+	struct mmc_command *cmd;
+
+	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+	cmd->opcode = MMC_STOP_TRANSMISSION;
+	cmd->arg = 0;
+	cmd->busy_timeout = 0;
+	if (host->cur_dir == DMA_DIR_FROM_CARD)
+		cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+	else
+		cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+
+	sd_send_cmd_get_rsp(host, cmd);
+	host->rw_mode = NORMAL_RW;
+	kfree(cmd);
+}
+
+static int sd_check_seq(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+	struct mmc_command *cmd = mrq->cmd;
+	struct mmc_data *data = mrq->data;
+
+	if (!mmc_op_multi(cmd->opcode))
+		return 0;
+
+	if (host->prev_dir != host->cur_dir)
+		return 0;
+
+	if ((host->prev_sec_addr + host->prev_sec_cnt) != data->blk_addr)
+		return 0;
+
+	return 1;
+}
+
 static void sd_request(struct work_struct *work)
 {
 	struct realtek_pci_sdmmc *host = container_of(work,
@@ -841,12 +984,36 @@ static void sd_request(struct work_struct *work)
 	if (!data_size) {
 		sd_send_cmd_get_rsp(host, cmd);
 	} else if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
-		cmd->error = sd_rw_multi(host, mrq);
-		if (!host->using_cookie)
-			sdmmc_post_req(host->mmc, host->mrq, 0);
+		/* Check seq function*/
+		if (data->flags & MMC_DATA_READ)
+			host->cur_dir = DMA_DIR_FROM_CARD;
+		else
+			host->cur_dir = DMA_DIR_TO_CARD;
+
+		if (host->rw_mode == SEQ_RW) {
+			cancel_delayed_work(&host->rw_idle_work);
+			if (!sd_check_seq(host, mrq)) {
+				sd_stop_sequential(host, mrq);
+				host->rw_mode = NORMAL_RW;
+			}
+		}
+
+		if (host->rw_mode == SEQ_RW)
+			cmd->error = sd_rw_sequential(host, mrq);
+		else {
+			if (mmc_op_multi(cmd->opcode))
+				host->rw_mode = SEQ_RW;
+			cmd->error = sd_rw_multi(host, mrq);
+			if (!host->using_cookie)
+				sdmmc_post_req(host->mmc, host->mrq, 0);
+		}
+
+		if (cmd->error)
+			host->rw_mode = NORMAL_RW;
+
+		if (mmc_op_multi(cmd->opcode) && host->rw_mode == SEQ_RW)
+			mod_delayed_work(system_wq, &host->rw_idle_work, msecs_to_jiffies(150));
 
-		if (mmc_op_multi(cmd->opcode) && mrq->stop)
-			sd_send_cmd_get_rsp(host, mrq->stop);
 	} else {
 		sd_normal_rw(host, mrq);
 	}
@@ -867,6 +1034,11 @@ static void sd_request(struct work_struct *work)
 	}
 
 	mutex_lock(&host->host_mutex);
+	if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
+		host->prev_dir = host->cur_dir;
+		host->prev_sec_addr = data->blk_addr;
+		host->prev_sec_cnt = data->blocks;
+	}
 	host->mrq = NULL;
 	mutex_unlock(&host->host_mutex);
 
@@ -1457,6 +1629,7 @@ static void rtsx_pci_sdmmc_card_event(struct platform_device *pdev)
 	struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
 
 	host->cookie = -1;
+	host->rw_mode = NORMAL_RW;
 	mmc_detect_change(host->mmc, 0);
 }
 
@@ -1487,6 +1660,7 @@ static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
 	host->cookie = -1;
 	host->power_state = SDMMC_POWER_OFF;
 	INIT_WORK(&host->work, sd_request);
+	INIT_DELAYED_WORK(&host->rw_idle_work, sd_rw_idle_work);
 	platform_set_drvdata(pdev, host);
 	pcr->slots[RTSX_SD_CARD].p_dev = pdev;
 	pcr->slots[RTSX_SD_CARD].card_event = rtsx_pci_sdmmc_card_event;
@@ -1526,6 +1700,7 @@ static int rtsx_pci_sdmmc_drv_remove(struct platform_device *pdev)
 		pm_runtime_disable(&pdev->dev);
 	}
 
+	cancel_delayed_work_sync(&host->rw_idle_work);
 	cancel_work_sync(&host->work);
 
 	mutex_lock(&host->host_mutex);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 3 lines · Ricky WU

sd_check_seq() to distinguish sequential rw or normal rw if this data is sequential call to sd_rw_sequential()

Can you please extend this commit message? This doesn't answer why or what this change really does, please try to do that, as to help me to review this.

Kind regards
Uffe

Quoted 275 lines
---
 drivers/mmc/host/rtsx_pci_sdmmc.c | 185 +++++++++++++++++++++++++++++-
 1 file changed, 180 insertions(+), 5 deletions(-)
drivers/mmc/host/rtsx_pci_sdmmc.c +180 -6
index 58cfaffa3c2d..9eb433b1f6f8 100644
@@ -22,6 +22,8 @@
 #include <asm/unaligned.h>
 #include <linux/pm_runtime.h>
 
+enum RW_MODE   {NORMAL_RW, SEQ_RW};
+
 struct realtek_pci_sdmmc {
        struct platform_device  *pdev;
        struct rtsx_pcr         *pcr;
@@ -31,6 +33,7 @@ struct realtek_pci_sdmmc {
 
        struct work_struct      work;
        struct mutex            host_mutex;
+       struct delayed_work             rw_idle_work;
 
        u8                      ssc_depth;
        unsigned int            clock;
@@ -46,6 +49,12 @@ struct realtek_pci_sdmmc {
        s32                     cookie;
        int                     cookie_sg_count;
        bool                    using_cookie;
+
+       enum RW_MODE            rw_mode;
+       u8              prev_dir;
+       u8              cur_dir;
+       u64             prev_sec_addr;
+       u32             prev_sec_cnt;
 };
 
 static int sdmmc_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios);
@@ -226,6 +235,14 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
        dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
                        __func__, cmd_idx, arg);
 
+       if (cmd_idx == MMC_SEND_STATUS && host->rw_mode == SEQ_RW) {
+               cmd->resp[0] = R1_READY_FOR_DATA | (R1_STATE_TRAN << 9);
+               goto out;
+       }
+
+       if (!mmc_op_multi(cmd->opcode))
+               host->rw_mode = NORMAL_RW;
+
        rsp_type = sd_response_type(cmd);
        if (rsp_type < 0)
                goto out;
@@ -542,6 +559,93 @@ static int sd_write_long_data(struct realtek_pci_sdmmc *host,
        return 0;
 }
 
+static int sd_rw_sequential(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+       struct rtsx_pcr *pcr = host->pcr;
+       struct mmc_host *mmc = host->mmc;
+       struct mmc_card *card = mmc->card;
+       struct mmc_data *data = mrq->data;
+       int uhs = mmc_card_uhs(card);
+       u8 cfg2;
+       int err;
+       size_t data_len = data->blksz * data->blocks;
+
+       cfg2 = SD_NO_CALCULATE_CRC7 | SD_CHECK_CRC16 |
+               SD_NO_WAIT_BUSY_END | SD_NO_CHECK_CRC7 | SD_RSP_LEN_0;
+
+       if (!uhs)
+               cfg2 |= SD_NO_CHECK_WAIT_CRC_TO;
+
+       rtsx_pci_init_cmd(pcr);
+       sd_cmd_set_data_len(pcr, data->blocks, data->blksz);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
+                       DMA_DONE_INT, DMA_DONE_INT);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3,
+               0xFF, (u8)(data_len >> 24));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2,
+               0xFF, (u8)(data_len >> 16));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1,
+               0xFF, (u8)(data_len >> 8));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)data_len);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+                       0x03 | DMA_PACK_SIZE_MASK,
+                       DMA_DIR_FROM_CARD | DMA_EN | DMA_512);
+       else
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+                       0x03 | DMA_PACK_SIZE_MASK,
+                       DMA_DIR_TO_CARD | DMA_EN | DMA_512);
+
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
+                       0x01, RING_BUFFER);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, cfg2);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
+                               SD_TRANSFER_START | SD_TM_AUTO_READ_3);
+       else
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
+                               SD_TRANSFER_START | SD_TM_AUTO_WRITE_3);
+
+       rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
+                       SD_TRANSFER_END, SD_TRANSFER_END);
+       rtsx_pci_send_cmd_no_wait(pcr);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 1, 10000);
+       else
+               err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 0, 10000);
+
+       if (err < 0) {
+               sd_clear_error(host);
+               return err;
+       }
+
+       return 0;
+}
+
+static int sd_stop_sequential(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+       struct rtsx_pcr *pcr = host->pcr;
+       struct mmc_command *cmd;
+
+       cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+       cmd->opcode = MMC_STOP_TRANSMISSION;
+       cmd->arg = 0;
+       cmd->busy_timeout = 0;
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       else
+               cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+       sd_send_cmd_get_rsp(host, cmd);
+       udelay(50);
+       rtsx_pci_write_register(pcr, RBCTL, RB_FLUSH, RB_FLUSH);
+       kfree(cmd);
+       return 0;
+}
+
 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc *host)
 {
        rtsx_pci_write_register(host->pcr, SD_CFG1,
@@ -796,6 +900,45 @@ static inline int sd_rw_cmd(struct mmc_command *cmd)
                (cmd->opcode == MMC_WRITE_BLOCK);
 }
 
+static void sd_rw_idle_work(struct work_struct *work)
+{
+       struct delayed_work *dwork = to_delayed_work(work);
+       struct realtek_pci_sdmmc *host = container_of(dwork,
+                       struct realtek_pci_sdmmc, rw_idle_work);
+       struct mmc_command *cmd;
+
+       cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+       cmd->opcode = MMC_STOP_TRANSMISSION;
+       cmd->arg = 0;
+       cmd->busy_timeout = 0;
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       else
+               cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+
+       sd_send_cmd_get_rsp(host, cmd);
+       host->rw_mode = NORMAL_RW;
+       kfree(cmd);
+}
+
+static int sd_check_seq(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
+{
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_data *data = mrq->data;
+
+       if (!mmc_op_multi(cmd->opcode))
+               return 0;
+
+       if (host->prev_dir != host->cur_dir)
+               return 0;
+
+       if ((host->prev_sec_addr + host->prev_sec_cnt) != data->blk_addr)
+               return 0;
+
+       return 1;
+}
+
 static void sd_request(struct work_struct *work)
 {
        struct realtek_pci_sdmmc *host = container_of(work,
@@ -841,12 +984,36 @@ static void sd_request(struct work_struct *work)
        if (!data_size) {
                sd_send_cmd_get_rsp(host, cmd);
        } else if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
-               cmd->error = sd_rw_multi(host, mrq);
-               if (!host->using_cookie)
-                       sdmmc_post_req(host->mmc, host->mrq, 0);
+               /* Check seq function*/
+               if (data->flags & MMC_DATA_READ)
+                       host->cur_dir = DMA_DIR_FROM_CARD;
+               else
+                       host->cur_dir = DMA_DIR_TO_CARD;
+
+               if (host->rw_mode == SEQ_RW) {
+                       cancel_delayed_work(&host->rw_idle_work);
+                       if (!sd_check_seq(host, mrq)) {
+                               sd_stop_sequential(host, mrq);
+                               host->rw_mode = NORMAL_RW;
+                       }
+               }
+
+               if (host->rw_mode == SEQ_RW)
+                       cmd->error = sd_rw_sequential(host, mrq);
+               else {
+                       if (mmc_op_multi(cmd->opcode))
+                               host->rw_mode = SEQ_RW;
+                       cmd->error = sd_rw_multi(host, mrq);
+                       if (!host->using_cookie)
+                               sdmmc_post_req(host->mmc, host->mrq, 0);
+               }
+
+               if (cmd->error)
+                       host->rw_mode = NORMAL_RW;
+
+               if (mmc_op_multi(cmd->opcode) && host->rw_mode == SEQ_RW)
+                       mod_delayed_work(system_wq, &host->rw_idle_work, msecs_to_jiffies(150));
 
-               if (mmc_op_multi(cmd->opcode) && mrq->stop)
-                       sd_send_cmd_get_rsp(host, mrq->stop);
        } else {
                sd_normal_rw(host, mrq);
        }
@@ -867,6 +1034,11 @@ static void sd_request(struct work_struct *work)
        }
 
        mutex_lock(&host->host_mutex);
+       if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
+               host->prev_dir = host->cur_dir;
+               host->prev_sec_addr = data->blk_addr;
+               host->prev_sec_cnt = data->blocks;
+       }
        host->mrq = NULL;
        mutex_unlock(&host->host_mutex);
 
@@ -1457,6 +1629,7 @@ static void rtsx_pci_sdmmc_card_event(struct platform_device *pdev)
        struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
 
        host->cookie = -1;
+       host->rw_mode = NORMAL_RW;
        mmc_detect_change(host->mmc, 0);
 }
 
@@ -1487,6 +1660,7 @@ static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
        host->cookie = -1;
        host->power_state = SDMMC_POWER_OFF;
        INIT_WORK(&host->work, sd_request);
+       INIT_DELAYED_WORK(&host->rw_idle_work, sd_rw_idle_work);
        platform_set_drvdata(pdev, host);
        pcr->slots[RTSX_SD_CARD].p_dev = pdev;
        pcr->slots[RTSX_SD_CARD].card_event = rtsx_pci_sdmmc_card_event;
@@ -1526,6 +1700,7 @@ static int rtsx_pci_sdmmc_drv_remove(struct platform_device *pdev)
                pm_runtime_disable(&pdev->dev);
        }
 
+       cancel_delayed_work_sync(&host->rw_idle_work);
        cancel_work_sync(&host->work);
 
        mutex_lock(&host->host_mutex);
--

2.25.1

author
#2 lore raw
Quoted 10 lines

-----Original Message-----
From: Ulf Hansson <[email protected]>
Sent: Friday, December 3, 2021 10:33 PM
To: Ricky WU <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] mmc: rtsx: improve rw performance

Quoted 8 lines
Quoted 3 lines · Ricky WU

sd_check_seq() to distinguish sequential rw or normal rw if this data is sequential call to sd_rw_sequential()

Can you please extend this commit message? This doesn't answer why or what this change really does, please try to do that, as to help me to review this.

This patch is for improving sequential read/write performance. Before this, whether CMD is muti-RW or single-RW the driver do the same flow. This patch distinguishes the two and do different flow to get more performance on sequential RW.
sd_check_seq() to distinguish sequential RW (CMD 18/25) or normal RW (CMD 17/24) if the data is sequential call to sd_rw_sequential()

Quoted 301 lines

Kind regards
Uffe

Quoted 42 lines
---
 drivers/mmc/host/rtsx_pci_sdmmc.c | 185
+++++++++++++++++++++++++++++-
 1 file changed, 180 insertions(+), 5 deletions(-)
drivers/mmc/host/rtsx_pci_sdmmc.c +9 -0
index 58cfaffa3c2d..9eb433b1f6f8 100644
@@ -22,6 +22,8 @@
 #include <asm/unaligned.h>
 #include <linux/pm_runtime.h>
 
+enum RW_MODE   {NORMAL_RW, SEQ_RW};
+
 struct realtek_pci_sdmmc {
        struct platform_device  *pdev;
        struct rtsx_pcr         *pcr;
@@ -31,6 +33,7 @@ struct realtek_pci_sdmmc {
 
        struct work_struct      work;
        struct mutex            host_mutex;
+       struct delayed_work             rw_idle_work;
 
        u8                      ssc_depth;
        unsigned int            clock;
@@ -46,6 +49,12 @@ struct realtek_pci_sdmmc {
        s32                     cookie;
        int                     cookie_sg_count;
        bool                    using_cookie;
+
+       enum RW_MODE            rw_mode;
+       u8              prev_dir;
+       u8              cur_dir;
+       u64             prev_sec_addr;
+       u32             prev_sec_cnt;
 };
 
 static int sdmmc_init_sd_express(struct mmc_host *mmc, struct mmc_ios
Quoted 1 line

*ios); @@ -226,6 +235,14 @@ static void sd_send_cmd_get_rsp(struct

realtek_pci_sdmmc *host,

Quoted 1 line
        dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg =

0x%08x\n",

Quoted 3 lines
                        __func__, cmd_idx, arg);

+ if (cmd_idx == MMC_SEND_STATUS && host->rw_mode ==

SEQ_RW) {

Quoted 1 line

+ cmd->resp[0] = R1_READY_FOR_DATA |

(R1_STATE_TRAN << 9);

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +6 -0
+               goto out;
+       }
+
+       if (!mmc_op_multi(cmd->opcode))
+               host->rw_mode = NORMAL_RW;
+
        rsp_type = sd_response_type(cmd);
        if (rsp_type < 0)
                goto out;
@@ -542,6 +559,93 @@ static int sd_write_long_data(struct

realtek_pci_sdmmc *host,

Quoted 6 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +3 -0
        return 0;
 }
 
+static int sd_rw_sequential(struct realtek_pci_sdmmc *host, struct
+mmc_request *mrq) {
+       struct rtsx_pcr *pcr = host->pcr;

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct realtek_pci_sdmmc *host,

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +10 -0
+       struct mmc_host *mmc = host->mmc;
+       struct mmc_card *card = mmc->card;
+       struct mmc_data *data = mrq->data;
+       int uhs = mmc_card_uhs(card);
+       u8 cfg2;
+       int err;
+       size_t data_len = data->blksz * data->blocks;
+
+       cfg2 = SD_NO_CALCULATE_CRC7 | SD_CHECK_CRC16 |
+               SD_NO_WAIT_BUSY_END | SD_NO_CHECK_CRC7 |

SD_RSP_LEN_0;

Quoted 11 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +11 -0
+
+       if (!uhs)
+               cfg2 |= SD_NO_CHECK_WAIT_CRC_TO;
+
+       rtsx_pci_init_cmd(pcr);
+       sd_cmd_set_data_len(pcr, data->blocks, data->blksz);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
+                       DMA_DONE_INT, DMA_DONE_INT);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3,
+               0xFF, (u8)(data_len >> 24));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2,

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct realtek_pci_sdmmc *host,

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +10 -0
+               0xFF, (u8)(data_len >> 16));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1,
+               0xFF, (u8)(data_len >> 8));
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF,
+ (u8)data_len);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+                       0x03 | DMA_PACK_SIZE_MASK,
+                       DMA_DIR_FROM_CARD | DMA_EN |

DMA_512);

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct realtek_pci_sdmmc *host,

Quoted 11 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +11 -0
+       else
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
+                       0x03 | DMA_PACK_SIZE_MASK,
+                       DMA_DIR_TO_CARD | DMA_EN | DMA_512);
+
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
+                       0x01, RING_BUFFER);
+       rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, cfg2);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,

0xFF,

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct · line 607

Quoted 1 line
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
+                               SD_TRANSFER_START |

SD_TM_AUTO_READ_3);

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct · line 609

Quoted 2 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +2 -0
+       else
+               rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,

0xFF,

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct realtek_pci_sdmmc *host,

Quoted 9 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +9 -0
+                               SD_TRANSFER_START |
+ SD_TM_AUTO_WRITE_3);
+
+       rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
+                       SD_TRANSFER_END, SD_TRANSFER_END);
+       rtsx_pci_send_cmd_no_wait(pcr);
+
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               err = rtsx_pci_dma_transfer(pcr, data->sg,

host->sg_count, 1, 10000);

Quoted 14 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +14 -0
+       else
+               err = rtsx_pci_dma_transfer(pcr, data->sg,
+ host->sg_count, 0, 10000);
+
+       if (err < 0) {
+               sd_clear_error(host);
+               return err;
+       }
+
+       return 0;
+}
+
+static int sd_stop_sequential(struct realtek_pci_sdmmc *host, struct
+mmc_request *mrq) {

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct realtek_pci_sdmmc *host,

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +10 -0
+       struct rtsx_pcr *pcr = host->pcr;
+       struct mmc_command *cmd;
+
+       cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+       cmd->opcode = MMC_STOP_TRANSMISSION;
+       cmd->arg = 0;
+       cmd->busy_timeout = 0;
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |

MMC_CMD_AC;

drivers/mmc/host/rtsx_pci_sdmmc.c static int sd_write_long_data(struct · line 644

Quoted 2 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +2 -0
+       else
+               cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |

MMC_CMD_AC;

Quoted 16 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +7 -0
+       sd_send_cmd_get_rsp(host, cmd);
+       udelay(50);
+       rtsx_pci_write_register(pcr, RBCTL, RB_FLUSH, RB_FLUSH);
+       kfree(cmd);
+       return 0;
+}
+
 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host)  {
        rtsx_pci_write_register(host->pcr, SD_CFG1, @@ -796,6 +900,45
@@ static inline int sd_rw_cmd(struct mmc_command *cmd)
                (cmd->opcode == MMC_WRITE_BLOCK);  }
drivers/mmc/host/rtsx_pci_sdmmc.c +3 -0
+static void sd_rw_idle_work(struct work_struct *work) {
+       struct delayed_work *dwork = to_delayed_work(work);
+       struct realtek_pci_sdmmc *host = container_of(dwork,

drivers/mmc/host/rtsx_pci_sdmmc.c static inline int sd_rw_cmd(struct mmc_command *cmd)

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +10 -0
+                       struct realtek_pci_sdmmc, rw_idle_work);
+       struct mmc_command *cmd;
+
+       cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+
+       cmd->opcode = MMC_STOP_TRANSMISSION;
+       cmd->arg = 0;
+       cmd->busy_timeout = 0;
+       if (host->cur_dir == DMA_DIR_FROM_CARD)
+               cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |

MMC_CMD_AC;

Quoted 47 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +39 -3
+       else
+               cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
+ MMC_CMD_AC;
+
+       sd_send_cmd_get_rsp(host, cmd);
+       host->rw_mode = NORMAL_RW;
+       kfree(cmd);
+}
+
+static int sd_check_seq(struct realtek_pci_sdmmc *host, struct
+mmc_request *mrq) {
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_data *data = mrq->data;
+
+       if (!mmc_op_multi(cmd->opcode))
+               return 0;
+
+       if (host->prev_dir != host->cur_dir)
+               return 0;
+
+       if ((host->prev_sec_addr + host->prev_sec_cnt) != data->blk_addr)
+               return 0;
+
+       return 1;
+}
+
 static void sd_request(struct work_struct *work)  {
        struct realtek_pci_sdmmc *host = container_of(work, @@ -841,12
+984,36 @@ static void sd_request(struct work_struct *work)
        if (!data_size) {
                sd_send_cmd_get_rsp(host, cmd);
        } else if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
-               cmd->error = sd_rw_multi(host, mrq);
-               if (!host->using_cookie)
-                       sdmmc_post_req(host->mmc, host->mrq, 0);
+               /* Check seq function*/
+               if (data->flags & MMC_DATA_READ)
+                       host->cur_dir = DMA_DIR_FROM_CARD;
+               else
+                       host->cur_dir = DMA_DIR_TO_CARD;
+
+               if (host->rw_mode == SEQ_RW) {
+                       cancel_delayed_work(&host->rw_idle_work);
+                       if (!sd_check_seq(host, mrq)) {
+                               sd_stop_sequential(host, mrq);
+                               host->rw_mode = NORMAL_RW;
+                       }

drivers/mmc/host/rtsx_pci_sdmmc.c

Quoted 10 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +10 -0
+               }
+
+               if (host->rw_mode == SEQ_RW)
+                       cmd->error = sd_rw_sequential(host, mrq);
+               else {
+                       if (mmc_op_multi(cmd->opcode))
+                               host->rw_mode = SEQ_RW;
+                       cmd->error = sd_rw_multi(host, mrq);
+                       if (!host->using_cookie)
+                               sdmmc_post_req(host->mmc,

host->mrq, 0);

drivers/mmc/host/rtsx_pci_sdmmc.c static void sd_request(struct work_struct *work)

Quoted 6 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +6 -0
+               }
+
+               if (cmd->error)
+                       host->rw_mode = NORMAL_RW;
+
+               if (mmc_op_multi(cmd->opcode) && host->rw_mode ==

SEQ_RW)

drivers/mmc/host/rtsx_pci_sdmmc.c static void rtsx_pci_sdmmc_card_event(struct

Quoted 21 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +7 -2
+                       mod_delayed_work(system_wq,
+ &host->rw_idle_work, msecs_to_jiffies(150));
 
-               if (mmc_op_multi(cmd->opcode) && mrq->stop)
-                       sd_send_cmd_get_rsp(host, mrq->stop);
        } else {
                sd_normal_rw(host, mrq);
        }
@@ -867,6 +1034,11 @@ static void sd_request(struct work_struct *work)
        }
 
        mutex_lock(&host->host_mutex);
+       if (sd_rw_cmd(cmd) || sdio_extblock_cmd(cmd, data)) {
+               host->prev_dir = host->cur_dir;
+               host->prev_sec_addr = data->blk_addr;
+               host->prev_sec_cnt = data->blocks;
+       }
        host->mrq = NULL;
        mutex_unlock(&host->host_mutex);
 
@@ -1457,6 +1629,7 @@ static void rtsx_pci_sdmmc_card_event(struct

platform_device *pdev)

drivers/mmc/host/rtsx_pci_sdmmc.c static int rtsx_pci_sdmmc_drv_probe(struct

Quoted 7 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
        struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
 
        host->cookie = -1;
+       host->rw_mode = NORMAL_RW;
        mmc_detect_change(host->mmc, 0);  }
 
@@ -1487,6 +1660,7 @@ static int rtsx_pci_sdmmc_drv_probe(struct

platform_device *pdev)

Quoted 8 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
        host->cookie = -1;
        host->power_state = SDMMC_POWER_OFF;
        INIT_WORK(&host->work, sd_request);
+       INIT_DELAYED_WORK(&host->rw_idle_work, sd_rw_idle_work);
        platform_set_drvdata(pdev, host);
        pcr->slots[RTSX_SD_CARD].p_dev = pdev;
        pcr->slots[RTSX_SD_CARD].card_event =

rtsx_pci_sdmmc_card_event; @@ -1526,6 +1700,7 @@ static int

rtsx_pci_sdmmc_drv_remove(struct platform_device *pdev)

Quoted 9 lines
                pm_runtime_disable(&pdev->dev);
        }
+       cancel_delayed_work_sync(&host->rw_idle_work);
        cancel_work_sync(&host->work);

        mutex_lock(&host->host_mutex);

--
2.25.1

------Please consider the environment before printing this e-mail.

#3 lore raw
Quoted 13 lines · Ricky WU
Quoted 13 lines

-----Original Message-----
From: Ulf Hansson <[email protected]>
Sent: Friday, December 3, 2021 10:33 PM
To: Ricky WU <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] mmc: rtsx: improve rw performance

Quoted 1 line · Ricky WU
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

sd_check_seq() to distinguish sequential rw or normal rw if this data is sequential call to sd_rw_sequential()

Can you please extend this commit message? This doesn't answer why or what this change really does, please try to do that, as to help me to review this.

This patch is for improving sequential read/write performance.

I would not use the term "sequential", but rather "multi-block read/writes".

Quoted 3 lines

Before this, whether CMD is muti-RW or single-RW the driver do the same flow. This patch distinguishes the two and do different flow to get more performance on sequential RW.

Alright, thanks for clarifying.

So to be clear, please update the commit message to say that it's improving performance for multi-block read/write. Then please add also some information about how that is achieved.

Moreover, please rename the functions in the code according to this as well, as to make it more clear. For example, use sd_rw_multi() (and sd_rw_single() if that is needed), rather than sd_rw_sequential().

Quoted 2 lines

sd_check_seq() to distinguish sequential RW (CMD 18/25) or normal RW (CMD 17/24) if the data is sequential call to sd_rw_sequential()

I will wait for a v2 from you, then I will give it another try to review.

[...]

Kind regards
Uffe

author
#4 lore raw
Quoted 45 lines

-----Original Message-----
From: Ulf Hansson <[email protected]>
Sent: Wednesday, December 8, 2021 6:27 PM
To: Ricky WU <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] mmc: rtsx: improve rw performance

Quoted 11 lines · Ricky WU
Quoted 11 lines

-----Original Message-----
From: Ulf Hansson <[email protected]>
Sent: Friday, December 3, 2021 10:33 PM
To: Ricky WU <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] mmc: rtsx: improve rw performance

Quoted 7 lines
Quoted 7 lines · Ricky WU
Quoted 3 lines

sd_check_seq() to distinguish sequential rw or normal rw if this data is sequential call to sd_rw_sequential()

Can you please extend this commit message? This doesn't answer why or what this change really does, please try to do that, as to help me to

review this.

Quoted 3 lines
Quoted 1 line

This patch is for improving sequential read/write performance.

I would not use the term "sequential", but rather "multi-block read/writes".

Quoted 1 line

Before this, whether CMD is muti-RW or single-RW the driver do the same

flow.

Quoted 2 lines

This patch distinguishes the two and do different flow to get more performance on sequential RW.

Alright, thanks for clarifying.

So to be clear, please update the commit message to say that it's improving performance for multi-block read/write. Then please add also some information about how that is achieved.

Quoted 9 lines

Moreover, please rename the functions in the code according to this as well, as to make it more clear. For example, use sd_rw_multi() (and sd_rw_single() if that is needed), rather than sd_rw_sequential().

Quoted 2 lines

sd_check_seq() to distinguish sequential RW (CMD 18/25) or normal RW (CMD 17/24) if the data is sequential call to sd_rw_sequential()

I will wait for a v2 from you, then I will give it another try to review.

Ok, I will do the v2 patch.
And I need to clarify, this patch is improving performance that the CMD is multi-block and the date is sequential. I will change the function name for more clarity.

Quoted 5 lines

[...]

Kind regards
Uffe
------Please consider the environment before printing this e-mail.