soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / drivers / storage / ahci_ata.c
blob062b7ac71b2bf724f0ddaf917de40b9876455217
1 /*
3 * Copyright (C) 2012 secunet Security Networks AG
4 * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
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>
38 #include "ahci_private.h"
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)
44 ahci_dev_t *const dev = (ahci_dev_t *)ata_dev;
46 if (count == 0)
47 return 0;
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;
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;
66 #endif
67 } else {
68 printf("ahci: Unsupported ATA read command (0x%x).\n",
69 ata_dev->read_cmd);
70 return -1;
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;
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;
90 #endif
91 dev->cmdtable->fis[12] = (sectors >> 0) & 0xff;
92 dev->cmdtable->fis[13] = (sectors >> 8) & 0xff;
94 if (ahci_cmdslot_exec(dev) < 0)
95 return -1;
96 else
97 return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift;