9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include <hw/sysbus.h>
32 #include "cpu-common.h"
34 #include <hw/ide/pci.h>
35 #include <hw/ide/ahci.h>
37 /* #define DEBUG_AHCI */
40 #define DPRINTF(port, fmt, ...) \
41 do { fprintf(stderr, "ahci: %s: [%d] ", __FUNCTION__, port); \
42 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
44 #define DPRINTF(port, fmt, ...) do {} while(0)
47 static void check_cmd(AHCIState *s, int port);
48 static int handle_cmd(AHCIState *s,int port,int slot);
49 static void ahci_reset_port(AHCIState *s, int port);
50 static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis);
51 static void ahci_init_d2h(AHCIDevice *ad);
53 static uint32_t ahci_port_read(AHCIState *s, int port, int offset)
57 pr = &s->dev[port].port_regs;
63 case PORT_LST_ADDR_HI:
64 val = pr->lst_addr_hi;
69 case PORT_FIS_ADDR_HI:
70 val = pr->fis_addr_hi;
82 val = ((uint16_t)s->dev[port].port.ifs[0].error << 8) |
83 s->dev[port].port.ifs[0].status;
89 if (s->dev[port].port.ifs[0].bs) {
90 val = SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP |
91 SATA_SCR_SSTATUS_SPD_GEN1 | SATA_SCR_SSTATUS_IPM_ACTIVE;
93 val = SATA_SCR_SSTATUS_DET_NODEV;
103 pr->scr_act &= ~s->dev[port].finished;
104 s->dev[port].finished = 0;
114 DPRINTF(port, "offset: 0x%x val: 0x%x\n", offset, val);
119 static void ahci_irq_raise(AHCIState *s, AHCIDevice *dev)
121 struct AHCIPCIState *d = container_of(s, AHCIPCIState, ahci);
123 DPRINTF(0, "raise irq\n");
125 if (msi_enabled(&d->card)) {
126 msi_notify(&d->card, 0);
128 qemu_irq_raise(s->irq);
132 static void ahci_irq_lower(AHCIState *s, AHCIDevice *dev)
134 struct AHCIPCIState *d = container_of(s, AHCIPCIState, ahci);
136 DPRINTF(0, "lower irq\n");
138 if (!msi_enabled(&d->card)) {
139 qemu_irq_lower(s->irq);
143 static void ahci_check_irq(AHCIState *s)
147 DPRINTF(-1, "check irq %#x\n", s->control_regs.irqstatus);
149 s->control_regs.irqstatus = 0;
150 for (i = 0; i < s->ports; i++) {
151 AHCIPortRegs *pr = &s->dev[i].port_regs;
152 if (pr->irq_stat & pr->irq_mask) {
153 s->control_regs.irqstatus |= (1 << i);
157 if (s->control_regs.irqstatus &&
158 (s->control_regs.ghc & HOST_CTL_IRQ_EN)) {
159 ahci_irq_raise(s, NULL);
161 ahci_irq_lower(s, NULL);
165 static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
168 DPRINTF(d->port_no, "trigger irq %#x -> %x\n",
169 irq_type, d->port_regs.irq_mask & irq_type);
171 d->port_regs.irq_stat |= irq_type;
175 static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
177 target_phys_addr_t len = wanted;
180 cpu_physical_memory_unmap(*ptr, len, 1, len);
183 *ptr = cpu_physical_memory_map(addr, &len, 1);
185 cpu_physical_memory_unmap(*ptr, len, 1, len);
190 static void ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
192 AHCIPortRegs *pr = &s->dev[port].port_regs;
194 DPRINTF(port, "offset: 0x%x val: 0x%x\n", offset, val);
198 map_page(&s->dev[port].lst,
199 ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
200 s->dev[port].cur_cmd = NULL;
202 case PORT_LST_ADDR_HI:
203 pr->lst_addr_hi = val;
204 map_page(&s->dev[port].lst,
205 ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
206 s->dev[port].cur_cmd = NULL;
210 map_page(&s->dev[port].res_fis,
211 ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
213 case PORT_FIS_ADDR_HI:
214 pr->fis_addr_hi = val;
215 map_page(&s->dev[port].res_fis,
216 ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
219 pr->irq_stat &= ~val;
223 pr->irq_mask = val & 0xfdc000ff;
227 pr->cmd = val & ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON);
229 if (pr->cmd & PORT_CMD_START) {
230 pr->cmd |= PORT_CMD_LIST_ON;
233 if (pr->cmd & PORT_CMD_FIS_RX) {
234 pr->cmd |= PORT_CMD_FIS_ON;
237 /* XXX usually the FIS would be pending on the bus here and
238 issuing deferred until the OS enables FIS receival.
239 Instead, we only submit it once - which works in most
240 cases, but is a hack. */
241 if ((pr->cmd & PORT_CMD_FIS_ON) &&
242 !s->dev[port].init_d2h_sent) {
243 ahci_init_d2h(&s->dev[port]);
244 s->dev[port].init_d2h_sent = 1;
250 s->dev[port].port.ifs[0].error = (val >> 8) & 0xff;
251 s->dev[port].port.ifs[0].status = val & 0xff;
260 if (((pr->scr_ctl & AHCI_SCR_SCTL_DET) == 1) &&
261 ((val & AHCI_SCR_SCTL_DET) == 0)) {
262 ahci_reset_port(s, port);
274 pr->cmd_issue |= val;
282 static uint64_t ahci_mem_read(void *opaque, target_phys_addr_t addr,
285 AHCIState *s = opaque;
288 if (addr < AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR) {
291 val = s->control_regs.cap;
294 val = s->control_regs.ghc;
297 val = s->control_regs.irqstatus;
299 case HOST_PORTS_IMPL:
300 val = s->control_regs.impl;
303 val = s->control_regs.version;
307 DPRINTF(-1, "(addr 0x%08X), val 0x%08X\n", (unsigned) addr, val);
308 } else if ((addr >= AHCI_PORT_REGS_START_ADDR) &&
309 (addr < (AHCI_PORT_REGS_START_ADDR +
310 (s->ports * AHCI_PORT_ADDR_OFFSET_LEN)))) {
311 val = ahci_port_read(s, (addr - AHCI_PORT_REGS_START_ADDR) >> 7,
312 addr & AHCI_PORT_ADDR_OFFSET_MASK);
320 static void ahci_mem_write(void *opaque, target_phys_addr_t addr,
321 uint64_t val, unsigned size)
323 AHCIState *s = opaque;
325 /* Only aligned reads are allowed on AHCI */
327 fprintf(stderr, "ahci: Mis-aligned write to addr 0x"
328 TARGET_FMT_plx "\n", addr);
332 if (addr < AHCI_GENERIC_HOST_CONTROL_REGS_MAX_ADDR) {
333 DPRINTF(-1, "(addr 0x%08X), val 0x%08"PRIX64"\n", (unsigned) addr, val);
336 case HOST_CAP: /* R/WO, RO */
337 /* FIXME handle R/WO */
339 case HOST_CTL: /* R/W */
340 if (val & HOST_CTL_RESET) {
341 DPRINTF(-1, "HBA Reset\n");
342 ahci_reset(container_of(s, AHCIPCIState, ahci));
344 s->control_regs.ghc = (val & 0x3) | HOST_CTL_AHCI_EN;
348 case HOST_IRQ_STAT: /* R/WC, RO */
349 s->control_regs.irqstatus &= ~val;
352 case HOST_PORTS_IMPL: /* R/WO, RO */
353 /* FIXME handle R/WO */
355 case HOST_VERSION: /* RO */
356 /* FIXME report write? */
359 DPRINTF(-1, "write to unknown register 0x%x\n", (unsigned)addr);
361 } else if ((addr >= AHCI_PORT_REGS_START_ADDR) &&
362 (addr < (AHCI_PORT_REGS_START_ADDR +
363 (s->ports * AHCI_PORT_ADDR_OFFSET_LEN)))) {
364 ahci_port_write(s, (addr - AHCI_PORT_REGS_START_ADDR) >> 7,
365 addr & AHCI_PORT_ADDR_OFFSET_MASK, val);
370 static MemoryRegionOps ahci_mem_ops = {
371 .read = ahci_mem_read,
372 .write = ahci_mem_write,
373 .endianness = DEVICE_LITTLE_ENDIAN,
376 static uint64_t ahci_idp_read(void *opaque, target_phys_addr_t addr,
379 AHCIState *s = opaque;
381 if (addr == s->idp_offset) {
384 } else if (addr == s->idp_offset + 4) {
385 /* data register - do memory read at location selected by index */
386 return ahci_mem_read(opaque, s->idp_index, size);
392 static void ahci_idp_write(void *opaque, target_phys_addr_t addr,
393 uint64_t val, unsigned size)
395 AHCIState *s = opaque;
397 if (addr == s->idp_offset) {
398 /* index register - mask off reserved bits */
399 s->idp_index = (uint32_t)val & ((AHCI_MEM_BAR_SIZE - 1) & ~3);
400 } else if (addr == s->idp_offset + 4) {
401 /* data register - do memory write at location selected by index */
402 ahci_mem_write(opaque, s->idp_index, val, size);
406 static MemoryRegionOps ahci_idp_ops = {
407 .read = ahci_idp_read,
408 .write = ahci_idp_write,
409 .endianness = DEVICE_LITTLE_ENDIAN,
413 static void ahci_reg_init(AHCIState *s)
417 s->control_regs.cap = (s->ports - 1) |
418 (AHCI_NUM_COMMAND_SLOTS << 8) |
419 (AHCI_SUPPORTED_SPEED_GEN1 << AHCI_SUPPORTED_SPEED) |
420 HOST_CAP_NCQ | HOST_CAP_AHCI;
422 s->control_regs.impl = (1 << s->ports) - 1;
424 s->control_regs.version = AHCI_VERSION_1_0;
426 for (i = 0; i < s->ports; i++) {
427 s->dev[i].port_state = STATE_RUN;
431 static uint32_t read_from_sglist(uint8_t *buffer, uint32_t len,
435 uint32_t total = 0, once;
436 ScatterGatherEntry *cur_prd;
439 cur_prd = sglist->sg;
440 sgcount = sglist->nsg;
441 for (i = 0; len && sgcount; i++) {
442 once = MIN(cur_prd->len, len);
443 cpu_physical_memory_read(cur_prd->base, buffer, once);
454 static uint32_t write_to_sglist(uint8_t *buffer, uint32_t len,
458 uint32_t total = 0, once;
459 ScatterGatherEntry *cur_prd;
462 DPRINTF(-1, "total: 0x%x bytes\n", len);
464 cur_prd = sglist->sg;
465 sgcount = sglist->nsg;
466 for (i = 0; len && sgcount; i++) {
467 once = MIN(cur_prd->len, len);
468 DPRINTF(-1, "write 0x%x bytes to 0x%lx\n", once, (long)cur_prd->base);
469 cpu_physical_memory_write(cur_prd->base, buffer, once);
480 static void check_cmd(AHCIState *s, int port)
482 AHCIPortRegs *pr = &s->dev[port].port_regs;
485 if ((pr->cmd & PORT_CMD_START) && pr->cmd_issue) {
486 for (slot = 0; (slot < 32) && pr->cmd_issue; slot++) {
487 if ((pr->cmd_issue & (1 << slot)) &&
488 !handle_cmd(s, port, slot)) {
489 pr->cmd_issue &= ~(1 << slot);
495 static void ahci_check_cmd_bh(void *opaque)
497 AHCIDevice *ad = opaque;
499 qemu_bh_delete(ad->check_bh);
502 if ((ad->busy_slot != -1) &&
503 !(ad->port.ifs[0].status & (BUSY_STAT|DRQ_STAT))) {
505 ad->port_regs.cmd_issue &= ~(1 << ad->busy_slot);
509 check_cmd(ad->hba, ad->port_no);
512 static void ahci_init_d2h(AHCIDevice *ad)
514 uint8_t init_fis[0x20];
515 IDEState *ide_state = &ad->port.ifs[0];
517 memset(init_fis, 0, sizeof(init_fis));
522 if (ide_state->drive_kind == IDE_CD) {
523 init_fis[5] = ide_state->lcyl;
524 init_fis[6] = ide_state->hcyl;
527 ahci_write_fis_d2h(ad, init_fis);
530 static void ahci_reset_port(AHCIState *s, int port)
532 AHCIDevice *d = &s->dev[port];
533 AHCIPortRegs *pr = &d->port_regs;
534 IDEState *ide_state = &d->port.ifs[0];
537 DPRINTF(port, "reset port\n");
539 ide_bus_reset(&d->port);
540 ide_state->ncq_queues = AHCI_MAX_CMDS;
546 d->init_d2h_sent = 0;
548 ide_state = &s->dev[port].port.ifs[0];
549 if (!ide_state->bs) {
553 /* reset ncq queue */
554 for (i = 0; i < AHCI_MAX_CMDS; i++) {
555 NCQTransferState *ncq_tfs = &s->dev[port].ncq_tfs[i];
556 if (!ncq_tfs->used) {
560 if (ncq_tfs->aiocb) {
561 bdrv_aio_cancel(ncq_tfs->aiocb);
562 ncq_tfs->aiocb = NULL;
565 /* Maybe we just finished the request thanks to bdrv_aio_cancel() */
566 if (!ncq_tfs->used) {
570 qemu_sglist_destroy(&ncq_tfs->sglist);
574 s->dev[port].port_state = STATE_RUN;
575 if (!ide_state->bs) {
576 s->dev[port].port_regs.sig = 0;
577 ide_state->status = SEEK_STAT | WRERR_STAT;
578 } else if (ide_state->drive_kind == IDE_CD) {
579 s->dev[port].port_regs.sig = SATA_SIGNATURE_CDROM;
580 ide_state->lcyl = 0x14;
581 ide_state->hcyl = 0xeb;
582 DPRINTF(port, "set lcyl = %d\n", ide_state->lcyl);
583 ide_state->status = SEEK_STAT | WRERR_STAT | READY_STAT;
585 s->dev[port].port_regs.sig = SATA_SIGNATURE_DISK;
586 ide_state->status = SEEK_STAT | WRERR_STAT;
589 ide_state->error = 1;
593 static void debug_print_fis(uint8_t *fis, int cmd_len)
598 fprintf(stderr, "fis:");
599 for (i = 0; i < cmd_len; i++) {
600 if ((i & 0xf) == 0) {
601 fprintf(stderr, "\n%02x:",i);
603 fprintf(stderr, "%02x ",fis[i]);
605 fprintf(stderr, "\n");
609 static void ahci_write_fis_sdb(AHCIState *s, int port, uint32_t finished)
611 AHCIPortRegs *pr = &s->dev[port].port_regs;
615 if (!s->dev[port].res_fis ||
616 !(pr->cmd & PORT_CMD_FIS_RX)) {
620 sdb_fis = &s->dev[port].res_fis[RES_FIS_SDBFIS];
621 ide_state = &s->dev[port].port.ifs[0];
624 *(uint32_t*)sdb_fis = 0;
627 sdb_fis[0] = ide_state->error;
628 sdb_fis[2] = ide_state->status & 0x77;
629 s->dev[port].finished |= finished;
630 *(uint32_t*)(sdb_fis + 4) = cpu_to_le32(s->dev[port].finished);
632 ahci_trigger_irq(s, &s->dev[port], PORT_IRQ_STAT_SDBS);
635 static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)
637 AHCIPortRegs *pr = &ad->port_regs;
640 target_phys_addr_t cmd_len = 0x80;
643 if (!ad->res_fis || !(pr->cmd & PORT_CMD_FIS_RX)) {
649 uint64_t tbl_addr = le64_to_cpu(ad->cur_cmd->tbl_addr);
650 cmd_fis = cpu_physical_memory_map(tbl_addr, &cmd_len, 0);
654 d2h_fis = &ad->res_fis[RES_FIS_RFIS];
657 d2h_fis[1] = (ad->hba->control_regs.irqstatus ? (1 << 6) : 0);
658 d2h_fis[2] = ad->port.ifs[0].status;
659 d2h_fis[3] = ad->port.ifs[0].error;
661 d2h_fis[4] = cmd_fis[4];
662 d2h_fis[5] = cmd_fis[5];
663 d2h_fis[6] = cmd_fis[6];
664 d2h_fis[7] = cmd_fis[7];
665 d2h_fis[8] = cmd_fis[8];
666 d2h_fis[9] = cmd_fis[9];
667 d2h_fis[10] = cmd_fis[10];
668 d2h_fis[11] = cmd_fis[11];
669 d2h_fis[12] = cmd_fis[12];
670 d2h_fis[13] = cmd_fis[13];
671 for (i = 14; i < 0x20; i++) {
675 if (d2h_fis[2] & ERR_STAT) {
676 ahci_trigger_irq(ad->hba, ad, PORT_IRQ_STAT_TFES);
679 ahci_trigger_irq(ad->hba, ad, PORT_IRQ_D2H_REG_FIS);
682 cpu_physical_memory_unmap(cmd_fis, cmd_len, 0, cmd_len);
686 static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist)
688 AHCICmdHdr *cmd = ad->cur_cmd;
689 uint32_t opts = le32_to_cpu(cmd->opts);
690 uint64_t prdt_addr = le64_to_cpu(cmd->tbl_addr) + 0x80;
691 int sglist_alloc_hint = opts >> AHCI_CMD_HDR_PRDT_LEN;
692 target_phys_addr_t prdt_len = (sglist_alloc_hint * sizeof(AHCI_SG));
693 target_phys_addr_t real_prdt_len = prdt_len;
698 if (!sglist_alloc_hint) {
699 DPRINTF(ad->port_no, "no sg list given by guest: 0x%08x\n", opts);
704 if (!(prdt = cpu_physical_memory_map(prdt_addr, &prdt_len, 0))){
705 DPRINTF(ad->port_no, "map failed\n");
709 if (prdt_len < real_prdt_len) {
710 DPRINTF(ad->port_no, "mapped less than expected\n");
715 /* Get entries in the PRDT, init a qemu sglist accordingly */
716 if (sglist_alloc_hint > 0) {
717 AHCI_SG *tbl = (AHCI_SG *)prdt;
719 qemu_sglist_init(sglist, sglist_alloc_hint);
720 for (i = 0; i < sglist_alloc_hint; i++) {
721 /* flags_size is zero-based */
722 qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
723 le32_to_cpu(tbl[i].flags_size) + 1);
728 cpu_physical_memory_unmap(prdt, prdt_len, 0, prdt_len);
732 static void ncq_cb(void *opaque, int ret)
734 NCQTransferState *ncq_tfs = (NCQTransferState *)opaque;
735 IDEState *ide_state = &ncq_tfs->drive->port.ifs[0];
737 /* Clear bit for this tag in SActive */
738 ncq_tfs->drive->port_regs.scr_act &= ~(1 << ncq_tfs->tag);
742 ide_state->error = ABRT_ERR;
743 ide_state->status = READY_STAT | ERR_STAT;
744 ncq_tfs->drive->port_regs.scr_err |= (1 << ncq_tfs->tag);
746 ide_state->status = READY_STAT | SEEK_STAT;
749 ahci_write_fis_sdb(ncq_tfs->drive->hba, ncq_tfs->drive->port_no,
750 (1 << ncq_tfs->tag));
752 DPRINTF(ncq_tfs->drive->port_no, "NCQ transfer tag %d finished\n",
755 bdrv_acct_done(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct);
756 qemu_sglist_destroy(&ncq_tfs->sglist);
760 static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
763 NCQFrame *ncq_fis = (NCQFrame*)cmd_fis;
764 uint8_t tag = ncq_fis->tag >> 3;
765 NCQTransferState *ncq_tfs = &s->dev[port].ncq_tfs[tag];
768 /* error - already in use */
769 fprintf(stderr, "%s: tag %d already used\n", __FUNCTION__, tag);
774 ncq_tfs->drive = &s->dev[port];
775 ncq_tfs->slot = slot;
776 ncq_tfs->lba = ((uint64_t)ncq_fis->lba5 << 40) |
777 ((uint64_t)ncq_fis->lba4 << 32) |
778 ((uint64_t)ncq_fis->lba3 << 24) |
779 ((uint64_t)ncq_fis->lba2 << 16) |
780 ((uint64_t)ncq_fis->lba1 << 8) |
781 (uint64_t)ncq_fis->lba0;
783 /* Note: We calculate the sector count, but don't currently rely on it.
784 * The total size of the DMA buffer tells us the transfer size instead. */
785 ncq_tfs->sector_count = ((uint16_t)ncq_fis->sector_count_high << 8) |
786 ncq_fis->sector_count_low;
788 DPRINTF(port, "NCQ transfer LBA from %"PRId64" to %"PRId64", "
789 "drive max %"PRId64"\n",
790 ncq_tfs->lba, ncq_tfs->lba + ncq_tfs->sector_count - 2,
791 s->dev[port].port.ifs[0].nb_sectors - 1);
793 ahci_populate_sglist(&s->dev[port], &ncq_tfs->sglist);
796 switch(ncq_fis->command) {
797 case READ_FPDMA_QUEUED:
798 DPRINTF(port, "NCQ reading %d sectors from LBA %"PRId64", "
800 ncq_tfs->sector_count-1, ncq_tfs->lba, ncq_tfs->tag);
802 DPRINTF(port, "tag %d aio read %"PRId64"\n",
803 ncq_tfs->tag, ncq_tfs->lba);
805 bdrv_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
806 (ncq_tfs->sector_count-1) * BDRV_SECTOR_SIZE,
808 ncq_tfs->aiocb = dma_bdrv_read(ncq_tfs->drive->port.ifs[0].bs,
809 &ncq_tfs->sglist, ncq_tfs->lba,
812 case WRITE_FPDMA_QUEUED:
813 DPRINTF(port, "NCQ writing %d sectors to LBA %"PRId64", tag %d\n",
814 ncq_tfs->sector_count-1, ncq_tfs->lba, ncq_tfs->tag);
816 DPRINTF(port, "tag %d aio write %"PRId64"\n",
817 ncq_tfs->tag, ncq_tfs->lba);
819 bdrv_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
820 (ncq_tfs->sector_count-1) * BDRV_SECTOR_SIZE,
822 ncq_tfs->aiocb = dma_bdrv_write(ncq_tfs->drive->port.ifs[0].bs,
823 &ncq_tfs->sglist, ncq_tfs->lba,
827 DPRINTF(port, "error: tried to process non-NCQ command as NCQ\n");
828 qemu_sglist_destroy(&ncq_tfs->sglist);
833 static int handle_cmd(AHCIState *s, int port, int slot)
840 target_phys_addr_t cmd_len;
842 if (s->dev[port].port.ifs[0].status & (BUSY_STAT|DRQ_STAT)) {
843 /* Engine currently busy, try again later */
844 DPRINTF(port, "engine busy\n");
848 cmd = &((AHCICmdHdr *)s->dev[port].lst)[slot];
850 if (!s->dev[port].lst) {
851 DPRINTF(port, "error: lst not given but cmd handled");
855 /* remember current slot handle for later */
856 s->dev[port].cur_cmd = cmd;
858 opts = le32_to_cpu(cmd->opts);
859 tbl_addr = le64_to_cpu(cmd->tbl_addr);
862 cmd_fis = cpu_physical_memory_map(tbl_addr, &cmd_len, 1);
865 DPRINTF(port, "error: guest passed us an invalid cmd fis\n");
869 /* The device we are working for */
870 ide_state = &s->dev[port].port.ifs[0];
872 if (!ide_state->bs) {
873 DPRINTF(port, "error: guest accessed unused port");
877 debug_print_fis(cmd_fis, 0x90);
878 //debug_print_fis(cmd_fis, (opts & AHCI_CMD_HDR_CMD_FIS_LEN) * 4);
880 switch (cmd_fis[0]) {
881 case SATA_FIS_TYPE_REGISTER_H2D:
884 DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
885 "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
891 switch (cmd_fis[1]) {
892 case SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER:
897 DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
898 "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
904 switch (s->dev[port].port_state) {
906 if (cmd_fis[15] & ATA_SRST) {
907 s->dev[port].port_state = STATE_RESET;
911 if (!(cmd_fis[15] & ATA_SRST)) {
912 ahci_reset_port(s, port);
917 if (cmd_fis[1] == SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
919 /* Check for NCQ command */
920 if ((cmd_fis[2] == READ_FPDMA_QUEUED) ||
921 (cmd_fis[2] == WRITE_FPDMA_QUEUED)) {
922 process_ncq_command(s, port, cmd_fis, slot);
926 /* Decompose the FIS */
927 ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
928 ide_state->feature = cmd_fis[3];
929 if (!ide_state->nsector) {
930 ide_state->nsector = 256;
933 if (ide_state->drive_kind != IDE_CD) {
935 * We set the sector depending on the sector defined in the FIS.
936 * Unfortunately, the spec isn't exactly obvious on this one.
938 * Apparently LBA48 commands set fis bytes 10,9,8,6,5,4 to the
939 * 48 bit sector number. ATA_CMD_READ_DMA_EXT is an example for
942 * Non-LBA48 commands however use 7[lower 4 bits],6,5,4 to define a
943 * 28-bit sector number. ATA_CMD_READ_DMA is an example for such
946 * Since the spec doesn't explicitly state what each field should
947 * do, I simply assume non-used fields as reserved and OR everything
948 * together, independent of the command.
950 ide_set_sector(ide_state, ((uint64_t)cmd_fis[10] << 40)
951 | ((uint64_t)cmd_fis[9] << 32)
952 /* This is used for LBA48 commands */
953 | ((uint64_t)cmd_fis[8] << 24)
954 /* This is used for non-LBA48 commands */
955 | ((uint64_t)(cmd_fis[7] & 0xf) << 24)
956 | ((uint64_t)cmd_fis[6] << 16)
957 | ((uint64_t)cmd_fis[5] << 8)
961 /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
962 * table to ide_state->io_buffer
964 if (opts & AHCI_CMD_ATAPI) {
965 memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
966 ide_state->lcyl = 0x14;
967 ide_state->hcyl = 0xeb;
968 debug_print_fis(ide_state->io_buffer, 0x10);
969 ide_state->feature = IDE_FEATURE_DMA;
970 s->dev[port].done_atapi_packet = 0;
971 /* XXX send PIO setup FIS */
974 ide_state->error = 0;
976 /* Reset transferred byte counter */
979 /* We're ready to process the command in FIS byte 2. */
980 ide_exec_cmd(&s->dev[port].port, cmd_fis[2]);
982 if (s->dev[port].port.ifs[0].status & READY_STAT) {
983 ahci_write_fis_d2h(&s->dev[port], cmd_fis);
988 cpu_physical_memory_unmap(cmd_fis, cmd_len, 1, cmd_len);
990 if (s->dev[port].port.ifs[0].status & (BUSY_STAT|DRQ_STAT)) {
991 /* async command, complete later */
992 s->dev[port].busy_slot = slot;
996 /* done handling the command */
1000 /* DMA dev <-> ram */
1001 static int ahci_start_transfer(IDEDMA *dma)
1003 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1004 IDEState *s = &ad->port.ifs[0];
1005 uint32_t size = (uint32_t)(s->data_end - s->data_ptr);
1006 /* write == ram -> device */
1007 uint32_t opts = le32_to_cpu(ad->cur_cmd->opts);
1008 int is_write = opts & AHCI_CMD_WRITE;
1009 int is_atapi = opts & AHCI_CMD_ATAPI;
1012 if (is_atapi && !ad->done_atapi_packet) {
1013 /* already prepopulated iobuffer */
1014 ad->done_atapi_packet = 1;
1018 if (!ahci_populate_sglist(ad, &s->sg)) {
1022 DPRINTF(ad->port_no, "%sing %d bytes on %s w/%s sglist\n",
1023 is_write ? "writ" : "read", size, is_atapi ? "atapi" : "ata",
1024 has_sglist ? "" : "o");
1026 if (is_write && has_sglist && (s->data_ptr < s->data_end)) {
1027 read_from_sglist(s->data_ptr, size, &s->sg);
1030 if (!is_write && has_sglist && (s->data_ptr < s->data_end)) {
1031 write_to_sglist(s->data_ptr, size, &s->sg);
1034 /* update number of transferred bytes */
1035 ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + size);
1038 /* declare that we processed everything */
1039 s->data_ptr = s->data_end;
1042 qemu_sglist_destroy(&s->sg);
1045 s->end_transfer_func(s);
1047 if (!(s->status & DRQ_STAT)) {
1049 ahci_trigger_irq(ad->hba, ad, PORT_IRQ_STAT_DSS);
1055 static void ahci_start_dma(IDEDMA *dma, IDEState *s,
1056 BlockDriverCompletionFunc *dma_cb)
1058 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1060 DPRINTF(ad->port_no, "\n");
1061 ad->dma_cb = dma_cb;
1062 ad->dma_status |= BM_STATUS_DMAING;
1066 static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
1068 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1069 IDEState *s = &ad->port.ifs[0];
1072 ahci_populate_sglist(ad, &s->sg);
1074 s->io_buffer_size = 0;
1075 for (i = 0; i < s->sg.nsg; i++) {
1076 s->io_buffer_size += s->sg.sg[i].len;
1079 DPRINTF(ad->port_no, "len=%#x\n", s->io_buffer_size);
1080 return s->io_buffer_size != 0;
1083 static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)
1085 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1086 IDEState *s = &ad->port.ifs[0];
1087 uint8_t *p = s->io_buffer + s->io_buffer_index;
1088 int l = s->io_buffer_size - s->io_buffer_index;
1090 if (ahci_populate_sglist(ad, &s->sg)) {
1095 write_to_sglist(p, l, &s->sg);
1097 read_from_sglist(p, l, &s->sg);
1100 /* update number of transferred bytes */
1101 ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + l);
1102 s->io_buffer_index += l;
1104 DPRINTF(ad->port_no, "len=%#x\n", l);
1109 static int ahci_dma_set_unit(IDEDMA *dma, int unit)
1111 /* only a single unit per link */
1115 static int ahci_dma_add_status(IDEDMA *dma, int status)
1117 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1118 ad->dma_status |= status;
1119 DPRINTF(ad->port_no, "set status: %x\n", status);
1121 if (status & BM_STATUS_INT) {
1122 ahci_trigger_irq(ad->hba, ad, PORT_IRQ_STAT_DSS);
1128 static int ahci_dma_set_inactive(IDEDMA *dma)
1130 AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
1132 DPRINTF(ad->port_no, "dma done\n");
1134 /* update d2h status */
1135 ahci_write_fis_d2h(ad, NULL);
1139 if (!ad->check_bh) {
1140 /* maybe we still have something to process, check later */
1141 ad->check_bh = qemu_bh_new(ahci_check_cmd_bh, ad);
1142 qemu_bh_schedule(ad->check_bh);
1148 static void ahci_irq_set(void *opaque, int n, int level)
1152 static void ahci_dma_restart_cb(void *opaque, int running, RunState state)
1156 static int ahci_dma_reset(IDEDMA *dma)
1161 static const IDEDMAOps ahci_dma_ops = {
1162 .start_dma = ahci_start_dma,
1163 .start_transfer = ahci_start_transfer,
1164 .prepare_buf = ahci_dma_prepare_buf,
1165 .rw_buf = ahci_dma_rw_buf,
1166 .set_unit = ahci_dma_set_unit,
1167 .add_status = ahci_dma_add_status,
1168 .set_inactive = ahci_dma_set_inactive,
1169 .restart_cb = ahci_dma_restart_cb,
1170 .reset = ahci_dma_reset,
1173 void ahci_init(AHCIState *s, DeviceState *qdev, int ports)
1179 s->dev = g_malloc0(sizeof(AHCIDevice) * ports);
1181 /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
1182 memory_region_init_io(&s->mem, &ahci_mem_ops, s, "ahci", AHCI_MEM_BAR_SIZE);
1183 memory_region_init_io(&s->idp, &ahci_idp_ops, s, "ahci-idp", 32);
1185 irqs = qemu_allocate_irqs(ahci_irq_set, s, s->ports);
1187 for (i = 0; i < s->ports; i++) {
1188 AHCIDevice *ad = &s->dev[i];
1190 ide_bus_new(&ad->port, qdev, i);
1191 ide_init2(&ad->port, irqs[i]);
1195 ad->port.dma = &ad->dma;
1196 ad->port.dma->ops = &ahci_dma_ops;
1197 ad->port_regs.cmd = PORT_CMD_SPIN_UP | PORT_CMD_POWER_ON;
1201 void ahci_uninit(AHCIState *s)
1203 memory_region_destroy(&s->mem);
1204 memory_region_destroy(&s->idp);
1208 void ahci_reset(void *opaque)
1210 struct AHCIPCIState *d = opaque;
1214 d->ahci.control_regs.irqstatus = 0;
1215 d->ahci.control_regs.ghc = 0;
1217 for (i = 0; i < d->ahci.ports; i++) {
1218 pr = &d->ahci.dev[i].port_regs;
1222 ahci_reset_port(&d->ahci, i);
1226 typedef struct SysbusAHCIState {
1227 SysBusDevice busdev;
1232 static const VMStateDescription vmstate_sysbus_ahci = {
1233 .name = "sysbus-ahci",
1237 static int sysbus_ahci_init(SysBusDevice *dev)
1239 SysbusAHCIState *s = FROM_SYSBUS(SysbusAHCIState, dev);
1240 ahci_init(&s->ahci, &dev->qdev, s->num_ports);
1242 sysbus_init_mmio(dev, &s->ahci.mem);
1243 sysbus_init_irq(dev, &s->ahci.irq);
1245 qemu_register_reset(ahci_reset, &s->ahci);
1249 static Property sysbus_ahci_properties[] = {
1250 DEFINE_PROP_UINT32("num-ports", SysbusAHCIState, num_ports, 1),
1251 DEFINE_PROP_END_OF_LIST(),
1254 static void sysbus_ahci_class_init(ObjectClass *klass, void *data)
1256 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
1257 DeviceClass *dc = DEVICE_CLASS(klass);
1259 sbc->init = sysbus_ahci_init;
1260 dc->vmsd = &vmstate_sysbus_ahci;
1261 dc->props = sysbus_ahci_properties;
1264 static TypeInfo sysbus_ahci_info = {
1265 .name = "sysbus-ahci",
1266 .parent = TYPE_SYS_BUS_DEVICE,
1267 .instance_size = sizeof(SysbusAHCIState),
1268 .class_init = sysbus_ahci_class_init,
1271 static void sysbus_ahci_register_types(void)
1273 type_register_static(&sysbus_ahci_info);
1276 type_init(sysbus_ahci_register_types)