| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <string.h> |
| 33 |
#include <libpayload.h> |
| 34 |
#include <pci.h> |
| 35 |
#include <storage/ata.h> |
| 36 |
#include <storage/ahci.h> |
| 37 |
|
| 38 |
#include "ahci_private.h" |
| 39 |
|
| 40 |
ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, |
| 41 |
const lba_t start, size_t count, |
| 42 |
u8 *const buf) |
| 43 |
{ |
| 44 |
ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; |
| 45 |
|
| 46 |
if (count == 0) |
| 47 |
return 0; |
| 48 |
|
| 49 |
if (ata_dev->read_cmd == ATA_READ_DMA) { |
| 50 |
if (start >= (1 << 28)) { |
| 51 |
printf("ahci: Sector is not 28-bit addressable.\n"); |
| 52 |
return -1; |
| 53 |
} else if (count > 256) { |
| 54 |
printf("ahci: Sector count too high (max. 256).\n"); |
| 55 |
count = 256; |
| 56 |
} |
| 57 |
#if CONFIG(LP_STORAGE_64BIT_LBA) |
| 58 |
} else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { |
| 59 |
if (start >= (1ULL << 48)) { |
| 60 |
printf("ahci: Sector is not 48-bit addressable.\n"); |
| 61 |
return -1; |
| 62 |
} else if (count > (64 * 1024)) { |
| 63 |
printf("ahci: Sector count too high (max. 65536).\n"); |
| 64 |
count = 64 * 1024; |
| 65 |
} |
| 66 |
#endif |
| 67 |
} else { |
| 68 |
printf("ahci: Unsupported ATA read command (0x%x).\n", |
| 69 |
ata_dev->read_cmd); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
const size_t bytes = count << ata_dev->sector_size_shift; |
| 74 |
const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0); |
| 75 |
const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift; |
| 76 |
|
| 77 |
dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE; |
| 78 |
dev->cmdtable->fis[ 1] = FIS_H2D_CMD; |
| 79 |
dev->cmdtable->fis[ 2] = ata_dev->read_cmd; |
| 80 |
dev->cmdtable->fis[ 4] = (start >> 0) & 0xff; |
| 81 |
dev->cmdtable->fis[ 5] = (start >> 8) & 0xff; |
| 82 |
dev->cmdtable->fis[ 6] = (start >> 16) & 0xff; |
| 83 |
dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA; |
| 84 |
dev->cmdtable->fis[ 8] = (start >> 24) & 0xff; |
| 85 |
#if CONFIG(LP_STORAGE_64BIT_LBA) |
| 86 |
if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { |
| 87 |
dev->cmdtable->fis[ 9] = (start >> 32) & 0xff; |
| 88 |
dev->cmdtable->fis[10] = (start >> 40) & 0xff; |
| 89 |
} |
| 90 |
#endif |
| 91 |
dev->cmdtable->fis[12] = (sectors >> 0) & 0xff; |
| 92 |
dev->cmdtable->fis[13] = (sectors >> 8) & 0xff; |
| 93 |
|
| 94 |
if (ahci_cmdslot_exec(dev) < 0) |
| 95 |
return -1; |
| 96 |
else |
| 97 |
return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift; |
| 98 |
} |