Patchwork ahci: Fix non-NCQ accesses for LBA > 16bits

login
register
mail settings
Submitter Alexander Graf
Date May 18, 2011, 12:48 p.m.
Message ID <1305722933-28282-1-git-send-email-agraf@suse.de>
Download mbox | patch
Permalink /patch/96164/
State New
Headers show

Comments

Alexander Graf - May 18, 2011, 12:48 p.m.
AHCI provides two ways of reading/writing data:

 1) NCQ
 2) ATA commands with the LBA in the command FIS

In the second code path, we didn't handle any LBAs that were bigger than
16 bits, so whenever a guest that used high LBA numbers wanted to access
data, the LBA got truncated down to 16 bits, giving the guest garbage.

This patch adds support for LBAs higher than 16 bits. I've tested that it
works just fine with SeaBIOS and Linux guests. This patch also unbreaks
the often reported grub errors people have seen with AHCI.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 hw/ide/ahci.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
Jan Kiszka - May 18, 2011, 7:16 p.m.
On 2011-05-18 14:48, Alexander Graf wrote:
> AHCI provides two ways of reading/writing data:
> 
>  1) NCQ
>  2) ATA commands with the LBA in the command FIS
> 
> In the second code path, we didn't handle any LBAs that were bigger than
> 16 bits, so whenever a guest that used high LBA numbers wanted to access
> data, the LBA got truncated down to 16 bits, giving the guest garbage.
> 
> This patch adds support for LBAs higher than 16 bits. I've tested that it
> works just fine with SeaBIOS and Linux guests. This patch also unbreaks
> the often reported grub errors people have seen with AHCI.

Cool! I actually had such a guest as well, but I didn't manage to look
closer so far. Now this patch cures it.

Jan

> 
> Signed-off-by: Alexander Graf <agraf@suse.de>
> ---
>  hw/ide/ahci.c |    9 +++++++--
>  1 files changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
> index c6e0c77..bc5c553 100644
> --- a/hw/ide/ahci.c
> +++ b/hw/ide/ahci.c
> @@ -884,8 +884,13 @@ static int handle_cmd(AHCIState *s, int port, int slot)
>          }
>  
>          if (ide_state->drive_kind != IDE_CD) {
> -            ide_set_sector(ide_state, (cmd_fis[6] << 16) | (cmd_fis[5] << 8) |
> -                           cmd_fis[4]);
> +            ide_set_sector(ide_state, ((uint64_t)cmd_fis[10] << 40)
> +                                    | ((uint64_t)cmd_fis[9] << 32)
> +                                    | ((uint64_t)cmd_fis[8] << 24)
> +                                    | ((uint64_t)(cmd_fis[7] & 0xf) << 24)
> +                                    | ((uint64_t)cmd_fis[6] << 16)
> +                                    | ((uint64_t)cmd_fis[5] << 8)
> +                                    | cmd_fis[4]);
>          }
>  
>          /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
Alexander Graf - May 18, 2011, 7:38 p.m.
On 18.05.2011, at 21:16, Jan Kiszka wrote:

> On 2011-05-18 14:48, Alexander Graf wrote:
>> AHCI provides two ways of reading/writing data:
>> 
>> 1) NCQ
>> 2) ATA commands with the LBA in the command FIS
>> 
>> In the second code path, we didn't handle any LBAs that were bigger than
>> 16 bits, so whenever a guest that used high LBA numbers wanted to access
>> data, the LBA got truncated down to 16 bits, giving the guest garbage.
>> 
>> This patch adds support for LBAs higher than 16 bits. I've tested that it
>> works just fine with SeaBIOS and Linux guests. This patch also unbreaks
>> the often reported grub errors people have seen with AHCI.
> 
> Cool! I actually had such a guest as well, but I didn't manage to look
> closer so far. Now this patch cures it.

Heh, yeah. Only costed me 2 days of my life - sigh :)


Alex

Patch

diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index c6e0c77..bc5c553 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -884,8 +884,13 @@  static int handle_cmd(AHCIState *s, int port, int slot)
         }
 
         if (ide_state->drive_kind != IDE_CD) {
-            ide_set_sector(ide_state, (cmd_fis[6] << 16) | (cmd_fis[5] << 8) |
-                           cmd_fis[4]);
+            ide_set_sector(ide_state, ((uint64_t)cmd_fis[10] << 40)
+                                    | ((uint64_t)cmd_fis[9] << 32)
+                                    | ((uint64_t)cmd_fis[8] << 24)
+                                    | ((uint64_t)(cmd_fis[7] & 0xf) << 24)
+                                    | ((uint64_t)cmd_fis[6] << 16)
+                                    | ((uint64_t)cmd_fis[5] << 8)
+                                    | cmd_fis[4]);
         }
 
         /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command