| 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 |
#ifdef DEBUG_STATUS |
| 41 |
static inline u32 _ahci_clear_status(volatile u32 *const reg, |
| 42 |
const char *const r, |
| 43 |
const char *const f) |
| 44 |
{ |
| 45 |
const u32 bits = *reg; |
| 46 |
if (bits) |
| 47 |
*reg = bits; |
| 48 |
printf("ahci: %s: %s == 0x%08x\n", f, r, bits); |
| 49 |
|
| 50 |
return bits; |
| 51 |
} |
| 52 |
#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__) |
| 53 |
#else |
| 54 |
static inline u32 _ahci_clear_status(volatile u32 *const reg) |
| 55 |
{ |
| 56 |
const u32 bits = *reg; |
| 57 |
if (bits) |
| 58 |
*reg = bits; |
| 59 |
return bits; |
| 60 |
} |
| 61 |
#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r) |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
static u8 *ahci_prdbuf_init(ahci_dev_t *const dev, |
| 66 |
u8 *const user_buf, const size_t len, |
| 67 |
const int out) |
| 68 |
{ |
| 69 |
if ((uintptr_t)user_buf & 1) { |
| 70 |
printf("ahci: Odd buffer pointer (%p).\n", user_buf); |
| 71 |
if (dev->buf) |
| 72 |
free(dev->buf - *(dev->buf - 1)); |
| 73 |
dev->buf = malloc(len + 2); |
| 74 |
if (!dev->buf) |
| 75 |
return NULL; |
| 76 |
dev->user_buf = user_buf; |
| 77 |
dev->write_back = !out; |
| 78 |
dev->buflen = len; |
| 79 |
if ((uintptr_t)dev->buf & 1) { |
| 80 |
dev->buf[0] = 1; |
| 81 |
dev->buf += 1; |
| 82 |
} else { |
| 83 |
dev->buf[0] = 1; |
| 84 |
dev->buf[1] = 2; |
| 85 |
dev->buf += 2; |
| 86 |
} |
| 87 |
if (out) |
| 88 |
memcpy(dev->buf, user_buf, len); |
| 89 |
return dev->buf; |
| 90 |
} else { |
| 91 |
return user_buf; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static void ahci_prdbuf_finalize(ahci_dev_t *const dev) |
| 96 |
{ |
| 97 |
if (dev->buf) { |
| 98 |
if (dev->write_back) |
| 99 |
memcpy(dev->user_buf, dev->buf, dev->buflen); |
| 100 |
free(dev->buf - *(dev->buf - 1)); |
| 101 |
} |
| 102 |
dev->buf = NULL; |
| 103 |
dev->user_buf = NULL; |
| 104 |
dev->write_back = 0; |
| 105 |
dev->buflen = 0; |
| 106 |
} |
| 107 |
|
| 108 |
int ahci_cmdengine_start(hba_port_t *const port) |
| 109 |
{ |
| 110 |
|
| 111 |
|
| 112 |
int timeout = 1000; |
| 113 |
while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) |
| 114 |
udelay(1); |
| 115 |
if (timeout < 0) { |
| 116 |
printf("ahci: Timeout during start of command engine.\n"); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
port->cmd_stat |= HBA_PxCMD_FRE; |
| 121 |
port->cmd_stat |= HBA_PxCMD_ST; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
int ahci_cmdengine_stop(hba_port_t *const port) |
| 126 |
{ |
| 127 |
port->cmd_stat &= ~HBA_PxCMD_ST; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
int timeout = 1000; |
| 132 |
while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--) |
| 133 |
udelay(1); |
| 134 |
if (timeout < 0) { |
| 135 |
printf("ahci: Timeout during stopping of command engine.\n"); |
| 136 |
return 1; |
| 137 |
} |
| 138 |
|
| 139 |
port->cmd_stat &= ~HBA_PxCMD_FRE; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
timeout = 1000; |
| 144 |
while ((port->cmd_stat & HBA_PxCMD_FR) && timeout--) |
| 145 |
udelay(1); |
| 146 |
if (timeout < 0) { |
| 147 |
printf("ahci: Timeout during stopping of command engine.\n"); |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) |
| 155 |
{ |
| 156 |
const int slotnum = 0; |
| 157 |
|
| 158 |
if (!(dev->port->cmd_stat & HBA_PxCMD_CR)) |
| 159 |
return -1; |
| 160 |
|
| 161 |
|
| 162 |
dev->port->cmd_issue |= (1 << slotnum); |
| 163 |
|
| 164 |
|
| 165 |
int timeout = 50000; |
| 166 |
while ((dev->port->cmd_issue & (1 << slotnum)) && |
| 167 |
!(dev->port->intr_status & HBA_PxIS_TFES) && |
| 168 |
timeout--) |
| 169 |
udelay(100); |
| 170 |
if (timeout < 0) { |
| 171 |
printf("ahci: Timeout during command execution.\n"); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
ahci_prdbuf_finalize(dev); |
| 176 |
|
| 177 |
const u32 intr_status = ahci_clear_status(dev->port, intr_status); |
| 178 |
if (intr_status & (HBA_PxIS_FATAL | HBA_PxIS_PCS)) { |
| 179 |
ahci_error_recovery(dev, intr_status); |
| 180 |
return -1; |
| 181 |
} else { |
| 182 |
return dev->cmdlist[slotnum].prd_bytes; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, |
| 187 |
u8 *const user_buf, size_t buf_len, |
| 188 |
const int out) |
| 189 |
{ |
| 190 |
const int slotnum = 0; |
| 191 |
|
| 192 |
size_t read_count = 0; |
| 193 |
|
| 194 |
memset((void *)&dev->cmdlist[slotnum], |
| 195 |
'\0', sizeof(dev->cmdlist[slotnum])); |
| 196 |
memset((void *)dev->cmdtable, |
| 197 |
'\0', sizeof(*dev->cmdtable)); |
| 198 |
dev->cmdlist[slotnum].cmd = CMD_CFL(FIS_H2D_FIS_LEN); |
| 199 |
dev->cmdlist[slotnum].cmdtable_base = virt_to_phys(dev->cmdtable); |
| 200 |
|
| 201 |
if (buf_len > 0) { |
| 202 |
size_t prdt_len; |
| 203 |
u8 *buf; |
| 204 |
int i; |
| 205 |
|
| 206 |
prdt_len = ((buf_len - 1) >> BYTES_PER_PRD_SHIFT) + 1; |
| 207 |
const size_t max_prdt_len = ARRAY_SIZE(dev->cmdtable->prdt); |
| 208 |
if (prdt_len > max_prdt_len) { |
| 209 |
prdt_len = max_prdt_len; |
| 210 |
buf_len = prdt_len << BYTES_PER_PRD_SHIFT; |
| 211 |
} |
| 212 |
|
| 213 |
dev->cmdlist[slotnum].prdt_length = prdt_len; |
| 214 |
read_count = buf_len; |
| 215 |
|
| 216 |
buf = ahci_prdbuf_init(dev, user_buf, buf_len, out); |
| 217 |
if (!buf) |
| 218 |
return 0; |
| 219 |
for (i = 0; i < prdt_len; ++i) { |
| 220 |
const size_t bytes = |
| 221 |
(buf_len < BYTES_PER_PRD) |
| 222 |
? buf_len : BYTES_PER_PRD; |
| 223 |
dev->cmdtable->prdt[i].data_base = virt_to_phys(buf); |
| 224 |
dev->cmdtable->prdt[i].flags = PRD_TABLE_BYTES(bytes); |
| 225 |
buf_len -= bytes; |
| 226 |
buf += bytes; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return read_count; |
| 231 |
} |
| 232 |
|
| 233 |
int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf) |
| 234 |
{ |
| 235 |
ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; |
| 236 |
|
| 237 |
ahci_cmdslot_prepare(dev, buf, 512, 0); |
| 238 |
|
| 239 |
dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; |
| 240 |
dev->cmdtable->fis[1] = FIS_H2D_CMD; |
| 241 |
dev->cmdtable->fis[2] = ata_dev->identify_cmd; |
| 242 |
|
| 243 |
if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512)) |
| 244 |
return -1; |
| 245 |
else |
| 246 |
return 0; |
| 247 |
} |