diff options
Diffstat (limited to 'payloads/libpayload/drivers')
-rw-r--r-- | payloads/libpayload/drivers/Makefile.inc | 10 | ||||
-rw-r--r-- | payloads/libpayload/drivers/storage/ahci.c | 273 | ||||
-rw-r--r-- | payloads/libpayload/drivers/storage/ahci_ata.c | 100 | ||||
-rw-r--r-- | payloads/libpayload/drivers/storage/ahci_atapi.c | 67 | ||||
-rw-r--r-- | payloads/libpayload/drivers/storage/ahci_common.c | 250 | ||||
-rw-r--r-- | payloads/libpayload/drivers/storage/ahci_private.h | 35 |
6 files changed, 461 insertions, 274 deletions
diff --git a/payloads/libpayload/drivers/Makefile.inc b/payloads/libpayload/drivers/Makefile.inc index a1891beb73..901c77e69d 100644 --- a/payloads/libpayload/drivers/Makefile.inc +++ b/payloads/libpayload/drivers/Makefile.inc @@ -54,10 +54,18 @@ libc-$(CONFIG_GEODELX_VIDEO_CONSOLE) += video/font8x16.c libc-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += video/corebootfb.c libc-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += video/font8x16.c +# AHCI/ATAPI driver libc-$(CONFIG_STORAGE) += storage/storage.c +libc-$(CONFIG_STORAGE_AHCI) += storage/ahci.c +libc-$(CONFIG_STORAGE_AHCI) += storage/ahci_common.c +ifeq ($(CONFIG_STORAGE_ATA),y) libc-$(CONFIG_STORAGE_ATA) += storage/ata.c +libc-$(CONFIG_STORAGE_ATA) += storage/ahci_ata.c +endif +ifeq ($(CONFIG_STORAGE_ATAPI),y) libc-$(CONFIG_STORAGE_ATAPI) += storage/atapi.c -libc-$(CONFIG_STORAGE_AHCI) += storage/ahci.c +libc-$(CONFIG_STORAGE_ATAPI) += storage/ahci_atapi.c +endif # USB stack libc-$(CONFIG_USB) += usb/usbinit.c diff --git a/payloads/libpayload/drivers/storage/ahci.c b/payloads/libpayload/drivers/storage/ahci.c index ec4db4ca7a..5711fa263d 100644 --- a/payloads/libpayload/drivers/storage/ahci.c +++ b/payloads/libpayload/drivers/storage/ahci.c @@ -71,54 +71,8 @@ static inline int ahci_port_is_active(const hba_port_t *const port) == (HBA_PxSSTS_IPM_ACTIVE | HBA_PxSSTS_DET_ESTABLISHED); } -static int ahci_cmdengine_start(hba_port_t *const port) -{ - /* CR has to be clear before starting the command engine. - This shouldn't take too long, but we should time out nevertheless. */ - int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ - while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) - udelay(1); - if (timeout < 0) { - printf("ahci: Timeout during start of command engine.\n"); - return 1; - } - - port->cmd_stat |= HBA_PxCMD_FRE; - port->cmd_stat |= HBA_PxCMD_ST; - return 0; -} - -static int ahci_cmdengine_stop(hba_port_t *const port) -{ - port->cmd_stat &= ~HBA_PxCMD_ST; - - /* Wait for the controller to clear CR. - This shouldn't take too long, but we should time out nevertheless. */ - int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ - while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) - udelay(1); - if (timeout < 0) { - printf("ahci: Timeout during stopping of command engine.\n"); - return 1; - } - - port->cmd_stat &= ~HBA_PxCMD_FRE; - - /* Wait for the controller to clear FR. - This shouldn't take too long, but we should time out nevertheless. */ - timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ - while ((port->cmd_stat & HBA_PxCMD_FR) && timeout--) - udelay(1); - if (timeout < 0) { - printf("ahci: Timeout during stopping of command engine.\n"); - return 1; - } - - return 0; -} - /** Do minimal error recovery. */ -static int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status) +int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status) { /* Command engine has to be restarted. We don't call ahci_cmdengine_stop() here as it also checks @@ -145,231 +99,6 @@ static int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status) return -1; } -/** Give a buffer with even address. */ -static u8 *ahci_prdbuf_init(ahci_dev_t *const dev, - u8 *const user_buf, const size_t len, - const int out) -{ - if ((u32)user_buf & 1) { - printf("ahci: Odd buffer pointer (%p).\n", user_buf); - if (dev->buf) /* orphaned buffer */ - free((void *)dev->buf - *(dev->buf - 1)); - dev->buf = malloc(len + 2); - if (!dev->buf) - return NULL; - dev->user_buf = user_buf; - dev->write_back = !out; - dev->buflen = len; - if ((u32)dev->buf & 1) { - dev->buf[0] = 1; - dev->buf += 1; - } else { - dev->buf[0] = 1; - dev->buf[1] = 2; - dev->buf += 2; - } - if (out) - memcpy(dev->buf, user_buf, len); - return dev->buf; - } else { - return user_buf; - } -} - -static void ahci_prdbuf_finalize(ahci_dev_t *const dev) -{ - if (dev->buf) { - if (dev->write_back) - memcpy(dev->user_buf, dev->buf, dev->buflen); - free((void *)dev->buf - *(dev->buf - 1)); - } - dev->buf = NULL; - dev->user_buf = NULL; - dev->write_back = 0; - dev->buflen = 0; -} - -static ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) -{ - const int slotnum = 0; /* We always use the first slot. */ - - if (!(dev->port->cmd_stat & HBA_PxCMD_CR)) - return -1; - - /* Trigger command execution. */ - dev->port->cmd_issue |= (1 << slotnum); - - /* Wait for the controller to finish command execution. */ - int timeout = 50000; /* Time out after 50000 * 100us == 5s. */ - while ((dev->port->cmd_issue & (1 << slotnum)) && - !(dev->port->intr_status & HBA_PxIS_TFES) && - timeout--) - udelay(100); - if (timeout < 0) { - printf("ahci: Timeout during command execution.\n"); - return -1; - } - - ahci_prdbuf_finalize(dev); - - const u32 intr_status = ahci_clear_status(dev->port, intr_status); - if (intr_status & (HBA_PxIS_FATAL | HBA_PxIS_PCS)) { - ahci_error_recovery(dev, intr_status); - return -1; - } else { - return dev->cmdlist[slotnum].prd_bytes; - } -} - -static size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, - u8 *const user_buf, size_t buf_len, - const int out) -{ - const int slotnum = 0; /* We always use the first slot. */ - - size_t read_count = 0; - - memset((void *)&dev->cmdlist[slotnum], - '\0', sizeof(dev->cmdlist[slotnum])); - memset((void *)dev->cmdtable, - '\0', sizeof(*dev->cmdtable)); - dev->cmdlist[slotnum].cmd = CMD_CFL(FIS_H2D_FIS_LEN); - dev->cmdlist[slotnum].cmdtable_base = virt_to_phys(dev->cmdtable); - - if (buf_len > 0) { - size_t prdt_len; - u8 *buf; - int i; - - prdt_len = ((buf_len - 1) >> BYTES_PER_PRD_SHIFT) + 1; - const size_t max_prdt_len = ARRAY_SIZE(dev->cmdtable->prdt); - if (prdt_len > max_prdt_len) { - prdt_len = max_prdt_len; - buf_len = prdt_len << BYTES_PER_PRD_SHIFT; - } - - dev->cmdlist[slotnum].prdt_length = prdt_len; - read_count = buf_len; - - buf = ahci_prdbuf_init(dev, user_buf, buf_len, out); - if (!buf) - return 0; - for (i = 0; i < prdt_len; ++i) { - const size_t bytes = - (buf_len < BYTES_PER_PRD) - ? buf_len : BYTES_PER_PRD; - dev->cmdtable->prdt[i].data_base = virt_to_phys(buf); - dev->cmdtable->prdt[i].flags = PRD_TABLE_BYTES(bytes); - buf_len -= bytes; - buf += bytes; - } - } - - return read_count; -} - -static ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, - const lba_t start, size_t count, - u8 *const buf) -{ - ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; - - if (count == 0) - return 0; - - if (ata_dev->read_cmd == ATA_READ_DMA) { - if (start >= (1 << 28)) { - printf("ahci: Sector is not 28-bit addressable.\n"); - return -1; - } else if (count > 256) { - printf("ahci: Sector count too high (max. 256).\n"); - count = 256; - } -#ifdef CONFIG_STORAGE_64BIT_LBA - } else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { - if (start >= (1ULL << 48)) { - printf("ahci: Sector is not 48-bit addressable.\n"); - return -1; - } else if (count > (64 * 1024)) { - printf("ahci: Sector count too high (max. 65536).\n"); - count = 64 * 1024; - } -#endif - } else { - printf("ahci: Unsupported ATA read command (0x%x).\n", - ata_dev->read_cmd); - return -1; - } - - const size_t bytes = count << ata_dev->sector_size_shift; - const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0); - const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift; - - dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[ 1] = FIS_H2D_CMD; - dev->cmdtable->fis[ 2] = ata_dev->read_cmd; - dev->cmdtable->fis[ 4] = (start >> 0) & 0xff; - dev->cmdtable->fis[ 5] = (start >> 8) & 0xff; - dev->cmdtable->fis[ 6] = (start >> 16) & 0xff; - dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA; - dev->cmdtable->fis[ 8] = (start >> 24) & 0xff; -#ifdef CONFIG_STORAGE_64BIT_LBA - if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { - dev->cmdtable->fis[ 9] = (start >> 32) & 0xff; - dev->cmdtable->fis[10] = (start >> 40) & 0xff; - } -#endif - dev->cmdtable->fis[12] = (sectors >> 0) & 0xff; - dev->cmdtable->fis[13] = (sectors >> 8) & 0xff; - - if (ahci_cmdslot_exec(dev) < 0) - return -1; - else - return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift; -} - -static ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, - const u8 *const cmd, const size_t cmdlen, - u8 *const buf, const size_t buflen) -{ - ahci_dev_t *const dev = (ahci_dev_t *)_dev; - - if ((cmdlen != 12) && (cmdlen != 16)) { - printf("ahci: Only 12- and 16-byte packet commands allowed.\n"); - return -1; - } - - const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0); - u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */ - if (byte_limit & 1) ++byte_limit; /* even limit */ - - dev->cmdlist[0].cmd |= CMD_ATAPI; - dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[1] = FIS_H2D_CMD; - dev->cmdtable->fis[2] = ATA_PACKET; - dev->cmdtable->fis[5] = byte_limit & 0xff; - dev->cmdtable->fis[6] = byte_limit >> 8; - memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen); - - return ahci_cmdslot_exec(dev); -} - -static int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf) -{ - ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; - - ahci_cmdslot_prepare(dev, buf, 512, 0); - - dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[1] = FIS_H2D_CMD; - dev->cmdtable->fis[2] = ata_dev->identify_cmd; - - if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512)) - return -1; - else - return 0; -} - static int ahci_dev_init(hba_ctrl_t *const ctrl, hba_port_t *const port, const int portnum) diff --git a/payloads/libpayload/drivers/storage/ahci_ata.c b/payloads/libpayload/drivers/storage/ahci_ata.c new file mode 100644 index 0000000000..1efec23ec4 --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_ata.c @@ -0,0 +1,100 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + +#include "ahci_private.h" + + +ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, + const lba_t start, size_t count, + u8 *const buf) +{ + ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; + + if (count == 0) + return 0; + + if (ata_dev->read_cmd == ATA_READ_DMA) { + if (start >= (1 << 28)) { + printf("ahci: Sector is not 28-bit addressable.\n"); + return -1; + } else if (count > 256) { + printf("ahci: Sector count too high (max. 256).\n"); + count = 256; + } +#ifdef CONFIG_STORAGE_64BIT_LBA + } else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { + if (start >= (1ULL << 48)) { + printf("ahci: Sector is not 48-bit addressable.\n"); + return -1; + } else if (count > (64 * 1024)) { + printf("ahci: Sector count too high (max. 65536).\n"); + count = 64 * 1024; + } +#endif + } else { + printf("ahci: Unsupported ATA read command (0x%x).\n", + ata_dev->read_cmd); + return -1; + } + + const size_t bytes = count << ata_dev->sector_size_shift; + const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0); + const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift; + + dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[ 1] = FIS_H2D_CMD; + dev->cmdtable->fis[ 2] = ata_dev->read_cmd; + dev->cmdtable->fis[ 4] = (start >> 0) & 0xff; + dev->cmdtable->fis[ 5] = (start >> 8) & 0xff; + dev->cmdtable->fis[ 6] = (start >> 16) & 0xff; + dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA; + dev->cmdtable->fis[ 8] = (start >> 24) & 0xff; +#ifdef CONFIG_STORAGE_64BIT_LBA + if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { + dev->cmdtable->fis[ 9] = (start >> 32) & 0xff; + dev->cmdtable->fis[10] = (start >> 40) & 0xff; + } +#endif + dev->cmdtable->fis[12] = (sectors >> 0) & 0xff; + dev->cmdtable->fis[13] = (sectors >> 8) & 0xff; + + if (ahci_cmdslot_exec(dev) < 0) + return -1; + else + return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift; +} diff --git a/payloads/libpayload/drivers/storage/ahci_atapi.c b/payloads/libpayload/drivers/storage/ahci_atapi.c new file mode 100644 index 0000000000..11305c22c3 --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_atapi.c @@ -0,0 +1,67 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + + +#include "ahci_private.h" + + +ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, + const u8 *const cmd, const size_t cmdlen, + u8 *const buf, const size_t buflen) +{ + ahci_dev_t *const dev = (ahci_dev_t *)_dev; + + if ((cmdlen != 12) && (cmdlen != 16)) { + printf("ahci: Only 12- and 16-byte packet commands allowed.\n"); + return -1; + } + + const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0); + u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */ + if (byte_limit & 1) ++byte_limit; /* even limit */ + + dev->cmdlist[0].cmd |= CMD_ATAPI; + dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[1] = FIS_H2D_CMD; + dev->cmdtable->fis[2] = ATA_PACKET; + dev->cmdtable->fis[5] = byte_limit & 0xff; + dev->cmdtable->fis[6] = byte_limit >> 8; + memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen); + + return ahci_cmdslot_exec(dev); +} diff --git a/payloads/libpayload/drivers/storage/ahci_common.c b/payloads/libpayload/drivers/storage/ahci_common.c new file mode 100644 index 0000000000..bcb0ce70bc --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_common.c @@ -0,0 +1,250 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + +#include "ahci_private.h" + + +#ifdef DEBUG_STATUS +static inline u32 _ahci_clear_status(volatile u32 *const reg, + const char *const r, + const char *const f) +{ + const u32 bits = *reg; + if (bits) + *reg = bits; + printf("ahci: %s: %s == 0x%08x\n", f, r, bits); + + return bits; +} +#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__) +#else +static inline u32 _ahci_clear_status(volatile u32 *const reg) +{ + const u32 bits = *reg; + if (bits) + *reg = bits; + return bits; +} +#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r) +#endif + + +/** Give a buffer with even address. */ +static u8 *ahci_prdbuf_init(ahci_dev_t *const dev, + u8 *const user_buf, const size_t len, + const int out) +{ + if ((u32)user_buf & 1) { + printf("ahci: Odd buffer pointer (%p).\n", user_buf); + if (dev->buf) /* orphaned buffer */ + free((void *)dev->buf - *(dev->buf - 1)); + dev->buf = malloc(len + 2); + if (!dev->buf) + return NULL; + dev->user_buf = user_buf; + dev->write_back = !out; + dev->buflen = len; + if ((u32)dev->buf & 1) { + dev->buf[0] = 1; + dev->buf += 1; + } else { + dev->buf[0] = 1; + dev->buf[1] = 2; + dev->buf += 2; + } + if (out) + memcpy(dev->buf, user_buf, len); + return dev->buf; + } else { + return user_buf; + } +} + +static void ahci_prdbuf_finalize(ahci_dev_t *const dev) +{ + if (dev->buf) { + if (dev->write_back) + memcpy(dev->user_buf, dev->buf, dev->buflen); + free((void *)dev->buf - *(dev->buf - 1)); + } + dev->buf = NULL; + dev->user_buf = NULL; + dev->write_back = 0; + dev->buflen = 0; +} + +int ahci_cmdengine_start(hba_port_t *const port) +{ + /* CR has to be clear before starting the command engine. + This shouldn't take too long, but we should time out nevertheless. */ + int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ + while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) + udelay(1); + if (timeout < 0) { + printf("ahci: Timeout during start of command engine.\n"); + return 1; + } + + port->cmd_stat |= HBA_PxCMD_FRE; + port->cmd_stat |= HBA_PxCMD_ST; + return 0; +} + +int ahci_cmdengine_stop(hba_port_t *const port) +{ + port->cmd_stat &= ~HBA_PxCMD_ST; + + /* Wait for the controller to clear CR. + This shouldn't take too long, but we should time out nevertheless. */ + int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ + while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) + udelay(1); + if (timeout < 0) { + printf("ahci: Timeout during stopping of command engine.\n"); + return 1; + } + + port->cmd_stat &= ~HBA_PxCMD_FRE; + + /* Wait for the controller to clear FR. + This shouldn't take too long, but we should time out nevertheless. */ + timeout = 1000; /* Time out after 1000 * 1us == 1ms. */ + while ((port->cmd_stat & HBA_PxCMD_FR) && timeout--) + udelay(1); + if (timeout < 0) { + printf("ahci: Timeout during stopping of command engine.\n"); + return 1; + } + + return 0; +} + +ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) +{ + const int slotnum = 0; /* We always use the first slot. */ + + if (!(dev->port->cmd_stat & HBA_PxCMD_CR)) + return -1; + + /* Trigger command execution. */ + dev->port->cmd_issue |= (1 << slotnum); + + /* Wait for the controller to finish command execution. */ + int timeout = 50000; /* Time out after 50000 * 100us == 5s. */ + while ((dev->port->cmd_issue & (1 << slotnum)) && + !(dev->port->intr_status & HBA_PxIS_TFES) && + timeout--) + udelay(100); + if (timeout < 0) { + printf("ahci: Timeout during command execution.\n"); + return -1; + } + + ahci_prdbuf_finalize(dev); + + const u32 intr_status = ahci_clear_status(dev->port, intr_status); + if (intr_status & (HBA_PxIS_FATAL | HBA_PxIS_PCS)) { + ahci_error_recovery(dev, intr_status); + return -1; + } else { + return dev->cmdlist[slotnum].prd_bytes; + } +} + +size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, + u8 *const user_buf, size_t buf_len, + const int out) +{ + const int slotnum = 0; /* We always use the first slot. */ + + size_t read_count = 0; + + memset((void *)&dev->cmdlist[slotnum], + '\0', sizeof(dev->cmdlist[slotnum])); + memset((void *)dev->cmdtable, + '\0', sizeof(*dev->cmdtable)); + dev->cmdlist[slotnum].cmd = CMD_CFL(FIS_H2D_FIS_LEN); + dev->cmdlist[slotnum].cmdtable_base = virt_to_phys(dev->cmdtable); + + if (buf_len > 0) { + size_t prdt_len; + u8 *buf; + int i; + + prdt_len = ((buf_len - 1) >> BYTES_PER_PRD_SHIFT) + 1; + const size_t max_prdt_len = ARRAY_SIZE(dev->cmdtable->prdt); + if (prdt_len > max_prdt_len) { + prdt_len = max_prdt_len; + buf_len = prdt_len << BYTES_PER_PRD_SHIFT; + } + + dev->cmdlist[slotnum].prdt_length = prdt_len; + read_count = buf_len; + + buf = ahci_prdbuf_init(dev, user_buf, buf_len, out); + if (!buf) + return 0; + for (i = 0; i < prdt_len; ++i) { + const size_t bytes = + (buf_len < BYTES_PER_PRD) + ? buf_len : BYTES_PER_PRD; + dev->cmdtable->prdt[i].data_base = virt_to_phys(buf); + dev->cmdtable->prdt[i].flags = PRD_TABLE_BYTES(bytes); + buf_len -= bytes; + buf += bytes; + } + } + + return read_count; +} + +int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf) +{ + ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; + + ahci_cmdslot_prepare(dev, buf, 512, 0); + + dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[1] = FIS_H2D_CMD; + dev->cmdtable->fis[2] = ata_dev->identify_cmd; + + if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512)) + return -1; + else + return 0; +} diff --git a/payloads/libpayload/drivers/storage/ahci_private.h b/payloads/libpayload/drivers/storage/ahci_private.h index ba1471b5d5..c966133798 100644 --- a/payloads/libpayload/drivers/storage/ahci_private.h +++ b/payloads/libpayload/drivers/storage/ahci_private.h @@ -2,6 +2,7 @@ * This file is part of the libpayload project. * * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -198,4 +199,36 @@ typedef struct { size_t buflen; } ahci_dev_t; -#endif +/* + * ahci_common.c + */ +int ahci_cmdengine_start(hba_port_t *const port); + +int ahci_cmdengine_stop(hba_port_t *const port); + +ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev); + +size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, + u8 *const user_buf, size_t buf_len, + const int out); + +int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf); + +int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status); + +/* + * ahci_atapi.c + */ +ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, + const u8 *const cmd, const size_t cmdlen, + u8 *const buf, const size_t buflen); + +/* + * ahci_ata.c + */ +ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, + const lba_t start, size_t count, + u8 *const buf); + + +#endif /* _AHCI_PRIVATE_H */ |