blob: ec4db4ca7a5fa73ce0acaaf65041f9afbcf1aea7 [file] [log] [blame]
Nico Huber1f6bd942012-08-30 15:36:57 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 *
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.
16 *
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.
28 */
29
30//#define DEBUG_STATUS
31
32#include <stdlib.h>
33#include <stdint.h>
34#include <string.h>
35#include <libpayload.h>
36#include <pci.h>
37#include <storage/ata.h>
38#include <storage/ahci.h>
39
40#include "ahci_private.h"
41
42
43#ifdef DEBUG_STATUS
44static inline u32 _ahci_clear_status(volatile u32 *const reg,
45 const char *const r,
46 const char *const f)
47{
48 const u32 bits = *reg;
49 if (bits)
50 *reg = bits;
51 printf("ahci: %s: %s == 0x%08x\n", f, r, bits);
52
53 return bits;
54}
55#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__)
56#else
57static inline u32 _ahci_clear_status(volatile u32 *const reg)
58{
59 const u32 bits = *reg;
60 if (bits)
61 *reg = bits;
62 return bits;
63}
64#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r)
65#endif
66
67
68static inline int ahci_port_is_active(const hba_port_t *const port)
69{
70 return (port->sata_status & (HBA_PxSSTS_IPM_MASK | HBA_PxSSTS_DET_MASK))
71 == (HBA_PxSSTS_IPM_ACTIVE | HBA_PxSSTS_DET_ESTABLISHED);
72}
73
74static int ahci_cmdengine_start(hba_port_t *const port)
75{
Nico Hubera00f9832013-06-17 17:47:24 +020076 /* CR has to be clear before starting the command engine.
Nico Huber1f6bd942012-08-30 15:36:57 +020077 This shouldn't take too long, but we should time out nevertheless. */
78 int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */
79 while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--)
80 udelay(1);
81 if (timeout < 0) {
82 printf("ahci: Timeout during start of command engine.\n");
83 return 1;
84 }
85
86 port->cmd_stat |= HBA_PxCMD_FRE;
87 port->cmd_stat |= HBA_PxCMD_ST;
88 return 0;
89}
90
91static int ahci_cmdengine_stop(hba_port_t *const port)
92{
93 port->cmd_stat &= ~HBA_PxCMD_ST;
94
Nico Hubera00f9832013-06-17 17:47:24 +020095 /* Wait for the controller to clear CR.
Nico Huber1f6bd942012-08-30 15:36:57 +020096 This shouldn't take too long, but we should time out nevertheless. */
97 int timeout = 1000; /* Time out after 1000 * 1us == 1ms. */
Nico Hubera00f9832013-06-17 17:47:24 +020098 while ((port->cmd_stat & HBA_PxCMD_CR) && timeout--)
Nico Huber1f6bd942012-08-30 15:36:57 +020099 udelay(1);
100 if (timeout < 0) {
101 printf("ahci: Timeout during stopping of command engine.\n");
102 return 1;
103 }
104
105 port->cmd_stat &= ~HBA_PxCMD_FRE;
Nico Hubera00f9832013-06-17 17:47:24 +0200106
107 /* Wait for the controller to clear FR.
108 This shouldn't take too long, but we should time out nevertheless. */
109 timeout = 1000; /* Time out after 1000 * 1us == 1ms. */
110 while ((port->cmd_stat & HBA_PxCMD_FR) && timeout--)
111 udelay(1);
112 if (timeout < 0) {
113 printf("ahci: Timeout during stopping of command engine.\n");
114 return 1;
115 }
116
Nico Huber1f6bd942012-08-30 15:36:57 +0200117 return 0;
118}
119
120/** Do minimal error recovery. */
121static int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status)
122{
123 /* Command engine has to be restarted.
124 We don't call ahci_cmdengine_stop() here as it also checks
125 HBA_PxCMD_FR which won't clear on fatal errors. */
126 dev->port->cmd_stat &= ~HBA_PxCMD_ST;
127
128 /* Always clear sata_error. */
129 ahci_clear_status(dev->port, sata_error);
130
131 /* Perform COMRESET if appropriate. */
132 const u32 tfd = dev->port->taskfile_data;
133 if ((tfd & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)) |
134 (intr_status & HBA_PxIS_PCS)) {
135 const u32 sctl = dev->port->sata_control & ~HBA_PxSCTL_DET_MASK;
136 dev->port->sata_control = sctl | HBA_PxSCTL_DET_COMRESET;
137 mdelay(1);
138 dev->port->sata_control = sctl;
139 }
140
141 if (ahci_port_is_active(dev->port))
142 /* Start command engine. */
143 return ahci_cmdengine_start(dev->port);
144 else
145 return -1;
146}
147
148/** Give a buffer with even address. */
149static u8 *ahci_prdbuf_init(ahci_dev_t *const dev,
150 u8 *const user_buf, const size_t len,
151 const int out)
152{
153 if ((u32)user_buf & 1) {
154 printf("ahci: Odd buffer pointer (%p).\n", user_buf);
155 if (dev->buf) /* orphaned buffer */
156 free((void *)dev->buf - *(dev->buf - 1));
157 dev->buf = malloc(len + 2);
158 if (!dev->buf)
159 return NULL;
160 dev->user_buf = user_buf;
161 dev->write_back = !out;
162 dev->buflen = len;
163 if ((u32)dev->buf & 1) {
164 dev->buf[0] = 1;
165 dev->buf += 1;
166 } else {
167 dev->buf[0] = 1;
168 dev->buf[1] = 2;
169 dev->buf += 2;
170 }
171 if (out)
172 memcpy(dev->buf, user_buf, len);
173 return dev->buf;
174 } else {
175 return user_buf;
176 }
177}
178
179static void ahci_prdbuf_finalize(ahci_dev_t *const dev)
180{
181 if (dev->buf) {
182 if (dev->write_back)
183 memcpy(dev->user_buf, dev->buf, dev->buflen);
184 free((void *)dev->buf - *(dev->buf - 1));
185 }
186 dev->buf = NULL;
187 dev->user_buf = NULL;
188 dev->write_back = 0;
189 dev->buflen = 0;
190}
191
192static ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev)
193{
194 const int slotnum = 0; /* We always use the first slot. */
195
196 if (!(dev->port->cmd_stat & HBA_PxCMD_CR))
197 return -1;
198
199 /* Trigger command execution. */
200 dev->port->cmd_issue |= (1 << slotnum);
201
202 /* Wait for the controller to finish command execution. */
203 int timeout = 50000; /* Time out after 50000 * 100us == 5s. */
204 while ((dev->port->cmd_issue & (1 << slotnum)) &&
205 !(dev->port->intr_status & HBA_PxIS_TFES) &&
206 timeout--)
207 udelay(100);
208 if (timeout < 0) {
209 printf("ahci: Timeout during command execution.\n");
210 return -1;
211 }
212
213 ahci_prdbuf_finalize(dev);
214
215 const u32 intr_status = ahci_clear_status(dev->port, intr_status);
216 if (intr_status & (HBA_PxIS_FATAL | HBA_PxIS_PCS)) {
217 ahci_error_recovery(dev, intr_status);
218 return -1;
219 } else {
220 return dev->cmdlist[slotnum].prd_bytes;
221 }
222}
223
224static size_t ahci_cmdslot_prepare(ahci_dev_t *const dev,
225 u8 *const user_buf, size_t buf_len,
226 const int out)
227{
228 const int slotnum = 0; /* We always use the first slot. */
229
230 size_t read_count = 0;
231
232 memset((void *)&dev->cmdlist[slotnum],
233 '\0', sizeof(dev->cmdlist[slotnum]));
234 memset((void *)dev->cmdtable,
235 '\0', sizeof(*dev->cmdtable));
236 dev->cmdlist[slotnum].cmd = CMD_CFL(FIS_H2D_FIS_LEN);
237 dev->cmdlist[slotnum].cmdtable_base = virt_to_phys(dev->cmdtable);
238
239 if (buf_len > 0) {
240 size_t prdt_len;
241 u8 *buf;
242 int i;
243
244 prdt_len = ((buf_len - 1) >> BYTES_PER_PRD_SHIFT) + 1;
245 const size_t max_prdt_len = ARRAY_SIZE(dev->cmdtable->prdt);
246 if (prdt_len > max_prdt_len) {
247 prdt_len = max_prdt_len;
248 buf_len = prdt_len << BYTES_PER_PRD_SHIFT;
249 }
250
251 dev->cmdlist[slotnum].prdt_length = prdt_len;
252 read_count = buf_len;
253
254 buf = ahci_prdbuf_init(dev, user_buf, buf_len, out);
255 if (!buf)
256 return 0;
257 for (i = 0; i < prdt_len; ++i) {
258 const size_t bytes =
259 (buf_len < BYTES_PER_PRD)
260 ? buf_len : BYTES_PER_PRD;
261 dev->cmdtable->prdt[i].data_base = virt_to_phys(buf);
262 dev->cmdtable->prdt[i].flags = PRD_TABLE_BYTES(bytes);
263 buf_len -= bytes;
264 buf += bytes;
265 }
266 }
267
268 return read_count;
269}
270
271static ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev,
272 const lba_t start, size_t count,
273 u8 *const buf)
274{
275 ahci_dev_t *const dev = (ahci_dev_t *)ata_dev;
276
277 if (count == 0)
278 return 0;
279
280 if (ata_dev->read_cmd == ATA_READ_DMA) {
281 if (start >= (1 << 28)) {
282 printf("ahci: Sector is not 28-bit addressable.\n");
283 return -1;
284 } else if (count > 256) {
285 printf("ahci: Sector count too high (max. 256).\n");
286 count = 256;
287 }
288#ifdef CONFIG_STORAGE_64BIT_LBA
289 } else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) {
290 if (start >= (1ULL << 48)) {
291 printf("ahci: Sector is not 48-bit addressable.\n");
292 return -1;
293 } else if (count > (64 * 1024)) {
294 printf("ahci: Sector count too high (max. 65536).\n");
295 count = 64 * 1024;
296 }
297#endif
298 } else {
299 printf("ahci: Unsupported ATA read command (0x%x).\n",
300 ata_dev->read_cmd);
301 return -1;
302 }
303
304 const size_t bytes = count << ata_dev->sector_size_shift;
305 const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0);
306 const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift;
307
308 dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE;
309 dev->cmdtable->fis[ 1] = FIS_H2D_CMD;
310 dev->cmdtable->fis[ 2] = ata_dev->read_cmd;
311 dev->cmdtable->fis[ 4] = (start >> 0) & 0xff;
312 dev->cmdtable->fis[ 5] = (start >> 8) & 0xff;
313 dev->cmdtable->fis[ 6] = (start >> 16) & 0xff;
314 dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA;
315 dev->cmdtable->fis[ 8] = (start >> 24) & 0xff;
316#ifdef CONFIG_STORAGE_64BIT_LBA
317 if (ata_dev->read_cmd == ATA_READ_DMA_EXT) {
318 dev->cmdtable->fis[ 9] = (start >> 32) & 0xff;
319 dev->cmdtable->fis[10] = (start >> 40) & 0xff;
320 }
321#endif
322 dev->cmdtable->fis[12] = (sectors >> 0) & 0xff;
323 dev->cmdtable->fis[13] = (sectors >> 8) & 0xff;
324
325 if (ahci_cmdslot_exec(dev) < 0)
326 return -1;
327 else
328 return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift;
329}
330
331static ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev,
332 const u8 *const cmd, const size_t cmdlen,
333 u8 *const buf, const size_t buflen)
334{
335 ahci_dev_t *const dev = (ahci_dev_t *)_dev;
336
337 if ((cmdlen != 12) && (cmdlen != 16)) {
338 printf("ahci: Only 12- and 16-byte packet commands allowed.\n");
339 return -1;
340 }
341
342 const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0);
343 u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */
344 if (byte_limit & 1) ++byte_limit; /* even limit */
345
346 dev->cmdlist[0].cmd |= CMD_ATAPI;
347 dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE;
348 dev->cmdtable->fis[1] = FIS_H2D_CMD;
349 dev->cmdtable->fis[2] = ATA_PACKET;
350 dev->cmdtable->fis[5] = byte_limit & 0xff;
351 dev->cmdtable->fis[6] = byte_limit >> 8;
352 memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen);
353
354 return ahci_cmdslot_exec(dev);
355}
356
357static int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf)
358{
359 ahci_dev_t *const dev = (ahci_dev_t *)ata_dev;
360
361 ahci_cmdslot_prepare(dev, buf, 512, 0);
362
363 dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE;
364 dev->cmdtable->fis[1] = FIS_H2D_CMD;
365 dev->cmdtable->fis[2] = ata_dev->identify_cmd;
366
367 if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512))
368 return -1;
369 else
370 return 0;
371}
372
373static int ahci_dev_init(hba_ctrl_t *const ctrl,
374 hba_port_t *const port,
375 const int portnum)
376{
377 int ret = 1;
378
379 const int ncs = HBA_CAPS_DECODE_NCS(ctrl->caps);
380
381 /* Allocate command list, one command table and received FIS. */
382 cmd_t *const cmdlist = memalign(1024, ncs * sizeof(cmd_t));
383 cmdtable_t *const cmdtable = memalign(128, sizeof(cmdtable_t));
384 rcvd_fis_t *const rcvd_fis = memalign(256, sizeof(rcvd_fis_t));
385 /* Allocate our device structure. */
386 ahci_dev_t *const dev = calloc(1, sizeof(ahci_dev_t));
387 if (!cmdlist || !cmdtable || !rcvd_fis || !dev)
388 goto _cleanup_ret;
389 memset((void *)cmdlist, '\0', ncs * sizeof(cmd_t));
390 memset((void *)cmdtable, '\0', sizeof(*cmdtable));
391 memset((void *)rcvd_fis, '\0', sizeof(*rcvd_fis));
392
393 /* Set command list base and received FIS base. */
394 if (ahci_cmdengine_stop(port))
395 return 1;
396 port->cmdlist_base = virt_to_phys(cmdlist);
397 port->frameinfo_base = virt_to_phys(rcvd_fis);
398 if (ahci_cmdengine_start(port))
399 return 1;
400 /* Put port into active state. */
401 port->cmd_stat |= HBA_PxCMD_ICC_ACTIVE;
402
403 dev->ctrl = ctrl;
404 dev->port = port;
405 dev->cmdlist = cmdlist;
406 dev->cmdtable = cmdtable;
407 dev->rcvd_fis = rcvd_fis;
408
Nico Huber354066e2013-06-17 17:42:35 +0200409 /*
410 * Wait for D2H Register FIS with device' signature.
411 * The drive has to spin up here, so wait up to 30s.
412 */
413 const int timeout_s = 30; /* Time out after 30s. */
414 int timeout = timeout_s * 100;
Nico Huber1f6bd942012-08-30 15:36:57 +0200415 while ((port->taskfile_data & HBA_PxTFD_BSY) && timeout--)
416 mdelay(10);
417
Nico Huber354066e2013-06-17 17:42:35 +0200418 if (port->taskfile_data & HBA_PxTFD_BSY)
419 printf("ahci: Timed out after %d seconds "
420 "of waiting for device to spin up.\n", timeout_s);
421
Nico Huber1f6bd942012-08-30 15:36:57 +0200422 /* Initialize device or fall through to clean up. */
423 switch (port->signature) {
424 case HBA_PxSIG_ATA:
425 printf("ahci: ATA drive on port #%d.\n", portnum);
426#ifdef CONFIG_STORAGE_ATA
427 dev->ata_dev.identify = ahci_identify_device;
428 dev->ata_dev.read_sectors = ahci_ata_read_sectors;
429 return ata_attach_device(&dev->ata_dev, PORT_TYPE_SATA);
430#endif
431 break;
432 case HBA_PxSIG_ATAPI:
433 printf("ahci: ATAPI drive on port #%d.\n", portnum);
434#ifdef CONFIG_STORAGE_ATAPI
435 dev->atapi_dev.identify = ahci_identify_device;
436 dev->atapi_dev.packet_read_cmd = ahci_packet_read_cmd;
437 return atapi_attach_device(&dev->atapi_dev, PORT_TYPE_SATA);
438#endif
439 break;
440 default:
441 printf("ahci: Unsupported device (signature == 0x%08x) "
442 "on port #%d.\n", port->signature, portnum);
443 break;
444 }
445 ret = 2;
446
447_cleanup_ret:
448 /* Clean up (not reached for initialized devices). */
449 if (dev)
450 free(dev);
451 if (!ahci_cmdengine_stop(port)) {
452 port->cmdlist_base = 0;
453 port->frameinfo_base = 0;
454 if (rcvd_fis)
455 free((void *)rcvd_fis);
456 if (cmdtable)
457 free((void *)cmdtable);
458 if (cmdlist)
459 free((void *)cmdlist);
460 }
461 return ret;
462}
463
464static void ahci_port_probe(hba_ctrl_t *const ctrl,
465 hba_port_t *const port,
466 const int portnum)
467{
468 /* If staggered spin-up is supported, spin-up device. */
469 if (ctrl->caps & HBA_CAPS_SSS) {
470 port->cmd_stat |= HBA_PxCMD_SUD;
471 }
472
473 /* Wait 1s if we just told the device to spin up or
474 if it's the first port. */
475 if ((ctrl->caps & HBA_CAPS_SSS) ||
476 !(ctrl->ports_impl & ((1 << (portnum - 1)) - 1))) {
477 /* Wait for port to become active. */
478 int timeout = 100; /* Time out after 100 * 100us == 10ms. */
479 while (!ahci_port_is_active(port) && timeout--)
480 udelay(100);
481 }
482 if (!ahci_port_is_active(port))
483 return;
484
485 ahci_clear_status(port, sata_error);
486 ahci_clear_status(port, intr_status);
487
488 ahci_dev_init(ctrl, port, portnum);
489}
490
491#ifdef CONFIG_STORAGE_AHCI_ONLY_TESTED
492static u32 working_controllers[] = {
Nico Huber5d1edf62013-05-21 12:26:47 +0200493 0x8086 | 0x2929 << 16, /* Mobile ICH9 */
494 0x8086 | 0x1e03 << 16, /* Mobile Panther Point PCH */
Nico Huber1f6bd942012-08-30 15:36:57 +0200495};
496#endif
497static void ahci_init_pci(pcidev_t dev)
498{
499 int i;
500
501 const u16 class = pci_read_config16(dev, 0xa);
502 if (class != 0x0106)
503 return;
504 const u16 vendor = pci_read_config16(dev, 0x00);
505 const u16 device = pci_read_config16(dev, 0x02);
506
507#ifdef CONFIG_STORAGE_AHCI_ONLY_TESTED
508 const u32 vendor_device = pci_read_config32(dev, 0x0);
509 for (i = 0; i < ARRAY_SIZE(working_controllers); ++i)
510 if (vendor_device == working_controllers[i])
511 break;
512 if (i == ARRAY_SIZE(working_controllers)) {
513 printf("ahci: Not using untested SATA controller "
514 "%02x:%02x.%02x (%04x:%04x).\n", PCI_BUS(dev),
515 PCI_SLOT(dev), PCI_FUNC(dev), vendor, device);
516 return;
517 }
518#endif
519
520 printf("ahci: Found SATA controller %02x:%02x.%02x (%04x:%04x).\n",
521 PCI_BUS(dev), PCI_SLOT(dev), PCI_FUNC(dev), vendor, device);
522
523 hba_ctrl_t *const ctrl = phys_to_virt(
524 pci_read_config32(dev, 0x24) & ~0x3ff);
525 hba_port_t *const ports = ctrl->ports;
526
527 /* Reset host controller. */
528 ctrl->global_ctrl |= HBA_CTRL_RESET;
529 /* Reset has to be finished after 1s. */
530 delay(1);
531 if (ctrl->global_ctrl & HBA_CTRL_RESET) {
532 printf("ahci: ERROR: "
533 "Controller reset didn't finish within 1s.\n");
534 return;
535 }
536
537 /* Set AHCI access mode. */
538 ctrl->global_ctrl |= HBA_CTRL_AHCI_EN;
539
540 /* Probe for devices. */
541 for (i = 0; i < 32; ++i) {
542 if (ctrl->ports_impl & (1 << i))
543 ahci_port_probe(ctrl, &ports[i], i + 1);
544 }
545}
546
547void ahci_initialize(void)
548{
549 int bus, dev, func;
550
551 for (bus = 0; bus < 256; ++bus) {
552 for (dev = 0; dev < 32; ++dev) {
553 const u16 class =
554 pci_read_config16(PCI_DEV(bus, dev, 0), 0xa);
555 if (class != 0xffff) {
556 for (func = 0; func < 8; ++func)
557 ahci_init_pci(PCI_DEV(bus, dev, func));
558 }
559 }
560 }
561}