public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Leif Lindholm <leif.lindholm@linaro.org>
To: Haojian Zhuang <haojian.zhuang@linaro.org>
Cc: ryan.harkin@linaro.org, edk2-devel@lists.01.org,
	ard.biesheuvel@linaro.org
Subject: Re: [PATCH v5 8/9] MmcDxe: expand to support multiple blocks
Date: Mon, 14 Nov 2016 17:15:25 +0000	[thread overview]
Message-ID: <20161114171525.GA27644@bivouac.eciton.net> (raw)
In-Reply-To: <1479019678-12621-9-git-send-email-haojian.zhuang@linaro.org>

On Sun, Nov 13, 2016 at 02:47:57PM +0800, Haojian Zhuang wrote:
> Make use of DMA to transfer multiple blocks at one time. It could
> improve the performance on MMC/SD driver.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Haojian Zhuang <haojian.zhuang@linaro.org>
> Tested-by: Ryan Harkin <ryan.harkin@linaro.org>
> ---
>  EmbeddedPkg/Include/Protocol/MmcHost.h    |   6 +
>  EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c | 177 ++++++++++++++++++++----------
>  2 files changed, 123 insertions(+), 60 deletions(-)
> 
> diff --git a/EmbeddedPkg/Include/Protocol/MmcHost.h b/EmbeddedPkg/Include/Protocol/MmcHost.h
> index 5028045..cd5335a 100644
> --- a/EmbeddedPkg/Include/Protocol/MmcHost.h
> +++ b/EmbeddedPkg/Include/Protocol/MmcHost.h
> @@ -62,6 +62,7 @@ typedef UINT32 MMC_CMD;
>  #define MMC_CMD20             (MMC_INDX(20) | MMC_CMD_WAIT_RESPONSE)
>  #define MMC_CMD23             (MMC_INDX(23) | MMC_CMD_WAIT_RESPONSE)
>  #define MMC_CMD24             (MMC_INDX(24) | MMC_CMD_WAIT_RESPONSE)
> +#define MMC_CMD25             (MMC_INDX(25) | MMC_CMD_WAIT_RESPONSE)
>  #define MMC_CMD55             (MMC_INDX(55) | MMC_CMD_WAIT_RESPONSE)
>  #define MMC_ACMD41            (MMC_INDX(41) | MMC_CMD_WAIT_RESPONSE | MMC_CMD_NO_CRC_RESPONSE)
>  #define MMC_ACMD51            (MMC_INDX(51) | MMC_CMD_WAIT_RESPONSE)
> @@ -152,6 +153,10 @@ typedef EFI_STATUS (EFIAPI *MMC_SETIOS) (
>    IN  UINT32                    TimingMode
>    );
>  
> +typedef BOOLEAN (EFIAPI *MMC_ISMULTIBLOCK) (
> +  IN  EFI_MMC_HOST_PROTOCOL     *This
> +  );
> +
>  
>  struct _EFI_MMC_HOST_PROTOCOL {
>  
> @@ -169,6 +174,7 @@ struct _EFI_MMC_HOST_PROTOCOL {
>    MMC_WRITEBLOCKDATA      WriteBlockData;
>  
>    MMC_SETIOS              SetIos;
> +  MMC_ISMULTIBLOCK        IsMultiBlock;
>  
>  };
>  
> diff --git a/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c b/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c
> index 0e1ef57..1fcb3b5 100644
> --- a/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c
> +++ b/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c
> @@ -126,6 +126,95 @@ MmcStopTransmission (
>  #define MMCI0_BLOCKLEN 512
>  #define MMCI0_TIMEOUT  10000
>  
> +STATIC EFI_STATUS

STATIC
EFI_STATUS

> +MmcTransferBlock (
> +  IN EFI_BLOCK_IO_PROTOCOL    *This,
> +  IN UINTN                    Cmd,
> +  IN UINTN                    Transfer,
> +  IN UINT32                   MediaId,
> +  IN EFI_LBA                  Lba,
> +  IN UINTN                    BufferSize,
> +  OUT VOID                    *Buffer
> +  )
> +{
> +  EFI_STATUS              Status;
> +  UINTN                   CmdArg;
> +  INTN                    Timeout;
> +  UINT32                  Response[4];
> +  MMC_HOST_INSTANCE       *MmcHostInstance;
> +  EFI_MMC_HOST_PROTOCOL   *MmcHost;
> +
> +  MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
> +  MmcHost = MmcHostInstance->MmcHost;
> +
> +  //Set command argument based on the card access mode (Byte mode or Block mode)
> +  if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) {

The code would be more readable if instead of BIT1 there was a
symbolic name of the flag we're testing for.

(OK, I see the code has been moved from elsewhere in this form, but it
would be good to make the code more clear.)

> +    CmdArg = Lba;
> +  } else {
> +    CmdArg = Lba * This->Media->BlockSize;
> +  }
> +
> +  Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((EFI_D_ERROR, "%a(MMC_CMD%d): Error %r\n", __func__, Cmd, Status));
> +    return Status;
> +  }
> +
> +  if (Transfer == MMC_IOBLOCKS_READ) {
> +    // Read Data
> +    Status = MmcHost->ReadBlockData (MmcHost, Lba, BufferSize, Buffer);
> +    if (EFI_ERROR (Status)) {
> +      DEBUG ((EFI_D_BLKIO, "%a(): Error Read Block Data and Status = %r\n", __func__, Status));
> +      MmcStopTransmission (MmcHost);
> +      return Status;
> +    }
> +    Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);
> +    if (EFI_ERROR (Status)) {
> +      DEBUG ((EFI_D_ERROR, "%a() : Error MmcProgrammingState\n", __func__));
> +      return Status;
> +    }
> +  } else {
> +    // Write Data
> +    Status = MmcHost->WriteBlockData (MmcHost, Lba, BufferSize, Buffer);
> +    if (EFI_ERROR (Status)) {
> +      DEBUG ((EFI_D_BLKIO, "%a(): Error Write Block Data and Status = %r\n", __func__, Status));
> +      MmcStopTransmission (MmcHost);
> +      return Status;
> +    }
> +  }
> +
> +  // Command 13 - Read status and wait for programming to complete (return to tran)
> +  Timeout = MMCI0_TIMEOUT;
> +  CmdArg = MmcHostInstance->CardInfo.RCA << 16;
> +  Response[0] = 0;
> +  while(!(Response[0] & MMC_R0_READY_FOR_DATA)
> +        && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
> +        && Timeout--) {
> +    Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
> +    if (!EFI_ERROR (Status)) {
> +      MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
> +      if (Response[0] & MMC_R0_READY_FOR_DATA) {
> +        break;  // Prevents delay once finished
> +      }
> +    }
> +    gBS->Stall (1);

Can you provide a comment of why this stall was chosen?
(Ah, again, moved code. Ignore.)

> +  }
> +
> +  if (BufferSize > This->Media->BlockSize) {
> +    Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
> +    if (EFI_ERROR (Status)) {
> +      DEBUG ((EFI_D_BLKIO, "%a(): Error and Status:%r\n", __func__, Status));
> +    }
> +  }
> +
> +  Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n"));
> +    return Status;
> +  }
> +  return Status;
> +}
> +
>  EFI_STATUS
>  MmcIoBlocks (
>    IN EFI_BLOCK_IO_PROTOCOL    *This,
> @@ -145,6 +234,7 @@ MmcIoBlocks (
>    EFI_MMC_HOST_PROTOCOL   *MmcHost;
>    UINTN                   BytesRemainingToBeTransfered;
>    UINTN                   BlockCount;
> +  UINTN                   ConsumeSize;
>  
>    BlockCount = 1;
>    MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
> @@ -165,6 +255,10 @@ MmcIoBlocks (
>      return EFI_NO_MEDIA;
>    }
>  
> +  if (MmcHost->IsMultiBlock && MmcHost->IsMultiBlock(MmcHost)) {
> +    BlockCount = (BufferSize + This->Media->BlockSize - 1) / This->Media->BlockSize;
> +  }
> +
>    // All blocks must be within the device
>    if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) {
>      return EFI_INVALID_PARAMETER;
> @@ -196,7 +290,7 @@ MmcIoBlocks (
>      CmdArg = MmcHostInstance->CardInfo.RCA << 16;
>      Response[0] = 0;
>      Timeout = 20;
> -    while(   (!(Response[0] & MMC_R0_READY_FOR_DATA))
> +    while(!(Response[0] & MMC_R0_READY_FOR_DATA)

This is cleanup, but it is unrelated to functional changes, so please
leave out.

>            && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
>            && Timeout--) {
>        Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
> @@ -210,75 +304,38 @@ MmcIoBlocks (
>        return EFI_NOT_READY;
>      }
>  
> -    //Set command argument based on the card access mode (Byte mode or Block mode)
> -    if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) {
> -      CmdArg = Lba;
> -    } else {
> -      CmdArg = Lba * This->Media->BlockSize;
> -    }
> -
>      if (Transfer == MMC_IOBLOCKS_READ) {
> -      // Read a single block
> -      Cmd = MMC_CMD17;
> -    } else {
> -      // Write a single block
> -      Cmd = MMC_CMD24;
> -    }
> -    Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);
> -    if (EFI_ERROR (Status)) {
> -      DEBUG ((EFI_D_ERROR, "MmcIoBlocks(MMC_CMD%d): Error %r\n", Cmd, Status));
> -      return Status;
> -    }
> -
> -    if (Transfer == MMC_IOBLOCKS_READ) {
> -      // Read one block of Data
> -      Status = MmcHost->ReadBlockData (MmcHost, Lba, This->Media->BlockSize, Buffer);
> -      if (EFI_ERROR (Status)) {
> -        DEBUG ((EFI_D_BLKIO, "MmcIoBlocks(): Error Read Block Data and Status = %r\n", Status));
> -        MmcStopTransmission (MmcHost);
> -        return Status;
> -      }
> -      Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);
> -      if (EFI_ERROR (Status)) {
> -        DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcProgrammingState\n"));
> -        return Status;
> +      if (BlockCount == 1) {
> +        // Read a single block
> +        Cmd = MMC_CMD17;
> +      } else {
> +	// Read multiple blocks
> +	Cmd = MMC_CMD18;
>        }
>      } else {
> -      // Write one block of Data
> -      Status = MmcHost->WriteBlockData (MmcHost, Lba, This->Media->BlockSize, Buffer);
> -      if (EFI_ERROR (Status)) {
> -        DEBUG ((EFI_D_BLKIO, "MmcIoBlocks(): Error Write Block Data and Status = %r\n", Status));
> -        MmcStopTransmission (MmcHost);
> -        return Status;
> +      if (BlockCount == 1) {
> +        // Write a single block
> +        Cmd = MMC_CMD24;
> +      } else {
> +	// Write multiple blocks
> +	Cmd = MMC_CMD25;
>        }
>      }
>  
> -    // Command 13 - Read status and wait for programming to complete (return to tran)
> -    Timeout = MMCI0_TIMEOUT;
> -    CmdArg = MmcHostInstance->CardInfo.RCA << 16;
> -    Response[0] = 0;
> -    while(   (!(Response[0] & MMC_R0_READY_FOR_DATA))
> -          && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
> -          && Timeout--) {
> -      Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
> -      if (!EFI_ERROR (Status)) {
> -        MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
> -        if ((Response[0] & MMC_R0_READY_FOR_DATA)) {
> -          break;  // Prevents delay once finished
> -        }
> -      }
> -      gBS->Stall (1);
> +    ConsumeSize = BlockCount * This->Media->BlockSize;
> +    if (BytesRemainingToBeTransfered < ConsumeSize) {
> +      ConsumeSize = BytesRemainingToBeTransfered;
>      }
> -
> -    Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
> +    Status = MmcTransferBlock (This, Cmd, Transfer, MediaId, Lba, ConsumeSize, Buffer);
>      if (EFI_ERROR (Status)) {
> -      DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n"));
> -      return Status;
> +      DEBUG ((EFI_D_ERROR, "%a(): Failed to transfer block and Status:%r\n", __func__, Status));
>      }
>  
> -    BytesRemainingToBeTransfered -= This->Media->BlockSize;
> -    Lba    += BlockCount;
> -    Buffer = (UINT8 *)Buffer + This->Media->BlockSize;
> +    BytesRemainingToBeTransfered -= ConsumeSize;
> +    if (BytesRemainingToBeTransfered > 0) {
> +      Lba    += BlockCount;
> +      Buffer = (UINT8 *)Buffer + ConsumeSize;
> +    }
>    }
>  
>    return EFI_SUCCESS;
> -- 
> 2.7.4
> 


  reply	other threads:[~2016-11-14 17:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-13  6:47 [PATCH v5 0/9] enhance MMC Haojian Zhuang
2016-11-13  6:47 ` [PATCH v5 1/9] MmcDxe: wait OCR busy bit free Haojian Zhuang
2016-11-13  6:47 ` [PATCH v5 2/9] MmcDxe: move ECSD into CardInfo structure Haojian Zhuang
2016-11-13  6:47 ` [PATCH v5 3/9] MmcDxe: declare ECSD structure Haojian Zhuang
2016-11-13  6:47 ` [PATCH v5 4/9] MmcDxe: add SPEC_VERS field in CSD structure Haojian Zhuang
2016-11-13  6:47 ` [PATCH v5 5/9] MmcDxe: add interface to change io width and speed Haojian Zhuang
2016-11-14 16:12   ` Leif Lindholm
2016-11-13  6:47 ` [PATCH v5 6/9] MmcDxe: set io bus width before reading EXTCSD Haojian Zhuang
2016-11-14 16:36   ` Leif Lindholm
2016-11-13  6:47 ` [PATCH v5 7/9] MmcDxe: set iospeed and bus width in SD stack Haojian Zhuang
2016-11-14 17:08   ` Leif Lindholm
2016-11-13  6:47 ` [PATCH v5 8/9] MmcDxe: expand to support multiple blocks Haojian Zhuang
2016-11-14 17:15   ` Leif Lindholm [this message]
2016-11-13  6:47 ` [PATCH v5 9/9] PL180: update for indentifying SD Haojian Zhuang
2016-11-14 17:27   ` Leif Lindholm
2016-11-14 15:53 ` [PATCH v5 0/9] enhance MMC Leif Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161114171525.GA27644@bivouac.eciton.net \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox