ahci.c revision 13fe8ee80c2ad706f6d07067ab94702a9f432a68
/* $Id$ */
/** @file
* AHCI host adapter driver to boot from SATA disks.
*/
/*
* Copyright (C) 2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <stdint.h>
#include <string.h>
#include "biosint.h"
#include "ebda.h"
#include "inlines.h"
#include "pciutil.h"
#include "vds.h"
#if DEBUG_AHCI
#else
# define DBG_AHCI(...)
#endif
/* Number of S/G table entries in EDDS. */
#define NUM_EDDS_SG 16
/**
* AHCI PRDT structure.
*/
typedef struct
{
} ahci_prdt;
/**
* AHCI controller data.
*/
typedef struct
{
/** The AHCI command list as defined by chapter 4.2.2 of the Intel AHCI spec.
* Because the BIOS doesn't support NCQ only the first command header is defined
* to save memory. - Must be aligned on a 1K boundary.
*/
/** Align the next structure on a 128 byte boundary. */
/** The command table of one request as defined by chapter 4.2.3 of the Intel AHCI spec.
* Must be aligned on 128 byte boundary.
*/
/** The ATAPI command region.
* Located 40h bytes after the beginning of the CFIS (Command FIS).
*/
/** Align the PRDT structure on a 128 byte boundary. */
/** Physical Region Descriptor Table (PRDT) array. In other
*/
/** Memory for the received command FIS area as specified by chapter 4.2.1
* of the Intel AHCI spec. This area is normally 256 bytes big but to save memory
* only the first 96 bytes are used because it is assumed that the controller
* never writes to the UFIS or reserved area. - Must be aligned on a 256byte boundary.
*/
/** Current port which uses the memory to communicate with the controller. */
/** Saved high bits of EAX. */
/** VDS EDDS DMA buffer descriptor structure. */
} ahci_t;
/* The AHCI specific data must fit into 1KB (statically allocated). */
/** PCI configuration fields. */
#define PCI_CONFIG_CAP 0x34
#define PCI_CAP_ID_SATACR 0x12
#define VBOX_AHCI_NO_DEVICE 0xffff
/** Global register set. */
#define AHCI_HBA_SIZE 0x100
//@todo: what are the casts good for?
# define AHCI_GHC_HR RT_BIT_32(0)
/** Per port register set. */
#define AHCI_PORT_SIZE 0x80
#define AHCI_REG_PORT_CLB 0x00
#define AHCI_REG_PORT_CLBU 0x04
#define AHCI_REG_PORT_FB 0x08
#define AHCI_REG_PORT_FBU 0x0c
#define AHCI_REG_PORT_IS 0x10
# define AHCI_REG_PORT_IS_DHRS RT_BIT_32(0)
#define AHCI_REG_PORT_IE 0x14
#define AHCI_REG_PORT_CMD 0x18
# define AHCI_REG_PORT_CMD_ST RT_BIT_32(0)
#define AHCI_REG_PORT_TFD 0x20
#define AHCI_REG_PORT_SIG 0x24
#define AHCI_REG_PORT_SSTS 0x28
#define AHCI_REG_PORT_SCTL 0x2c
#define AHCI_REG_PORT_SERR 0x30
#define AHCI_REG_PORT_SACT 0x34
#define AHCI_REG_PORT_CI 0x38
/** Returns the absolute register offset from a given port and port register. */
#define AHCI_REG_IDX 0
#define AHCI_REG_DATA 4
/** Writes the given value to a AHCI register. */
/** Reads from a AHCI register. */
/** Writes to the given port register. */
/** Reads from the given port register. */
#define ATA_CMD_IDENTIFY_DEVICE 0xEC
#define ATA_CMD_IDENTIFY_PACKET 0xA1
#define ATA_CMD_PACKET 0xA0
#define AHCI_CMD_READ_DMA_EXT 0x25
#define AHCI_CMD_WRITE_DMA_EXT 0x35
/* Warning: Destroys high bits of EAX. */
".386" \
"in eax, dx" \
"mov dx, ax" \
"shr eax, 16" \
"xchg ax, dx" \
/* Warning: Destroys high bits of EAX. */
".386" \
"xchg ax, cx" \
"shl eax, 16" \
"mov ax, cx" \
"out dx, eax" \
* Instead, each externally callable routine must save the high bits before
* modifying them and restore the high bits before exiting.
*/
/* Note: Reading high EAX bits destroys them - *must* be restored later. */
".386" \
"shr eax, 16" \
".386" \
"shl eax, 16" \
{
}
{
}
/**
* Sets a given set of bits in a register.
*/
{
}
/**
* Clears a given set of bits in a register.
*/
{
}
/**
* Returns whether at least one of the bits in the given mask is set
* for a register.
*/
{
}
/**
* Extracts a range of bits from a register and shifts them
* to the right.
*/
{
}
/**
* Converts a segment:offset pair into a 32bit physical address.
*/
{
}
/**
* Issues a command to the SATA controller and waits for completion.
*/
{
if (port != 0xff)
{
/* Prepare the command header. */
/* Enable Command and FIS receive engine. */
/* Queue command. */
/* Wait for a D2H FIS. */
DBG_AHCI("AHCI: Waiting for D2H FIS\n");
{
// This is where we'd need some kind of a yield functionality...
}
AHCI_REG_PORT_IS_DHRS); /* Acknowledge received D2H FIS. */
/* Disable command engine. */
/** @todo: Examine status. */
}
else
DBG_AHCI("AHCI: Invalid port given\n");
}
/**
* Issue command to device.
*/
{
/* Prepare the FIS. */
/* Lock memory needed for DMA. */
DBG_AHCI("AHCI: S/G list for %lu bytes (skip %u)\n",
/* Set up the PRDT. */
++prdt_idx;
++prdt_idx;
}
#ifdef DEBUG_AHCI
}
#endif
/* Build variable part of first command DWORD (reuses 'cmd'). */
if (cmd == AHCI_CMD_WRITE_DMA_EXT)
else if (cmd == ATA_CMD_PACKET) {
} else
cmd = 0;
/* Unlock the buffer again. */
}
/**
* Deinits the curent active port.
*/
{
if (port != 0xff)
{
/* Put the port into an idle state. */
{
DBG_AHCI("AHCI: Waiting for the port to idle\n");
}
/*
* Port idles, set up memory for commands and received FIS and program the
* address registers.
*/
//@todo: merge memsets?
/* Disable all interrupts. */
}
}
/**
* Brings a port into a minimal state to make device detection possible
* or to queue requests.
*/
{
/* Deinit any other port first. */
/* Put the port into an idle state. */
{
DBG_AHCI("AHCI: Waiting for the port to idle\n");
}
/*
* Port idles, set up memory for commands and received FIS and program the
* address registers.
*/
//@todo: just one memset?
DBG_AHCI("AHCI: FIS receive area %lx from %x:%x\n",
VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_FB, ahci_addr_to_phys(&ahci->abFisRecv));
VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_CLB, ahci_addr_to_phys(&ahci->aCmdHdr));
/* Disable all interrupts. */
/* Clear all errors. */
}
/**
* Read sectors from an attached AHCI device.
*
* @returns status code.
* @param bios_dsk Pointer to disk request packet (in the
* EBDA).
*/
{
if (device_id > BX_MAX_AHCI_DEVICES)
DBG_AHCI("%s: transferred %lu bytes\n", __func__, ((ahci_t __far *)(bios_dsk->ahci_seg :> 0))->aCmdHdr[1]);
#ifdef DMA_WORKAROUND
#endif
return 0; //@todo!!
}
/**
* Write sectors to an attached AHCI device.
*
* @returns status code.
* @param bios_dsk Pointer to disk request packet (in the
* EBDA).
*/
{
if (device_id > BX_MAX_AHCI_DEVICES)
DBG_AHCI("%s: transferred %lu bytes\n", __func__, ((ahci_t __far *)(bios_dsk->ahci_seg :> 0))->aCmdHdr[1]);
return 0; //@todo!!
}
//@todo: move
#define ATA_DATA_NO 0x00
#define ATA_DATA_IN 0x01
#define ATA_DATA_OUT 0x02
{
/* Data out is currently not supported. */
if (inout == ATA_DATA_OUT) {
return 1;
}
/* The skip length must be even. */
if (skip_b & 1) {
return 1;
}
/* Convert to AHCI specific device number. */
// bios_dsk->drqp.sect_sz = 2048;
/* Copy the ATAPI command where the HBA can fetch it. */
/* Reset transferred counts. */
// @todo: clear in calling code?
/* Set up a PRD entry to throw away the beginning of the transfer. */
}
#ifdef DMA_WORKAROUND
#endif
// return 0; //@todo!!
}
{
/* Reset connection. */
/*
* According to the spec we should wait at least 1msec until the reset
* is cleared but this is a virtual controller so we don't have to.
*/
/* Check if there is a device on the port. */
{
if (devcount_ahci < BX_MAX_AHCI_DEVICES)
{
/* Device detected, enable FIS receive. */
/* Check signature to determine device type. */
if (val == 0x101)
{
DBG_AHCI("AHCI: Detected hard disk\n");
/* Identify device. */
/* Calculate index into the generic device table. */
/** @todo update sectors to be a 64 bit number (also lba...). */
/* Get logical CHS geometry. */
switch (devcount_ahci)
{
case 0:
idxCmosChsBase = 0x40;
break;
case 1:
idxCmosChsBase = 0x48;
break;
case 2:
idxCmosChsBase = 0x50;
break;
case 3:
idxCmosChsBase = 0x58;
break;
default:
idxCmosChsBase = 0;
}
{
}
else
/* Store the ID of the disk in the BIOS hdidmap. */
hdcount++;
/* Update hdcount in the BDA. */
hdcount++;
}
else if (val == 0xeb140101)
{
DBG_AHCI("AHCI: Detected ATAPI device\n");
/* Identify packet device. */
/* Calculate index into the generic device table. */
/* Store the ID of the device in the BIOS cdidmap. */
cdcount++;
}
else
DBG_AHCI("AHCI: Ignoring unknown device\n");
}
else
DBG_AHCI("AHCI: Reached maximum device count, skipping\n");
}
}
/**
* Allocates 1K of conventional memory.
*/
static uint16_t ahci_mem_alloc(void)
{
if (base_mem_kb == 0)
return 0;
base_mem_kb--; /* Allocate one block. */
return ahci_seg;
}
/**
* Initializes the AHCI HBA and detects attached devices.
*/
{
DBG_AHCI("AHCI: Controller version: 0x%x (major) 0x%x (minor)\n",
/* Allocate 1K of base memory. */
ahci_seg = ahci_mem_alloc();
if (ahci_seg == 0)
{
DBG_AHCI("AHCI: Could not allocate 1K of memory, can't boot from controller\n");
return 0;
}
DBG_AHCI("AHCI: ahci_seg=%04x, size=%04x, pointer at EBDA:%04x (EBDA size=%04x)\n",
bios_dsk->ahci_devcnt = 0;
/* Physical address of memory used for throwing away ATAPI data when reading 512-byte
* blocks from 2048-byte CD sectors.
*/
/* Reset the controller. */
do
{
} while (val & AHCI_GHC_HR != 0);
/* Go through the ports. */
i = 0;
while (i < 32)
{
{
DBG_AHCI("AHCI: Port %u is present\n", i);
ahci_port_detect_device(ahci_seg :> 0, i);
cPorts--;
if (cPorts == 0)
break;
}
i++;
}
return 0;
}
/**
* Init the AHCI driver and detect attached disks.
*/
{
if (busdevfn != VBOX_AHCI_NO_DEVICE)
{
/* Examine the capability list and search for the Serial ATA Capability Register. */
while (u8PciCapOff != 0)
{
if (u8PciCapId == PCI_CAP_ID_SATACR)
break;
/* Go on to the next capability. */
}
if (u8PciCapOff != 0)
{
/* Advance to the stuff behind the id and next capability pointer. */
u8PciCapOff += 2;
if (u8Rev == 0x10)
{
switch (u16BarOff & 0xf)
{
case 0x04:
u8Bar = 0x10;
break;
case 0x05:
u8Bar = 0x14;
break;
case 0x06:
u8Bar = 0x18;
break;
case 0x07:
u8Bar = 0x1c;
break;
case 0x08:
u8Bar = 0x20;
break;
case 0x09:
u8Bar = 0x24;
break;
case 0x0f:
default:
/* Reserved or unsupported. */
}
/* Get the offset inside the BAR from bits 4:15. */
if (u8Bar != 0x00)
{
if ((u32Bar & 0x01) != 0)
{
int rc;
}
else
DBG_AHCI("BAR is MMIO\n");
}
}
else
}
else
}
else
DBG_AHCI("No AHCI HBA!\n");
}