4 * Copyright (c) 2010 qiaochong@loongson.cn
5 * Copyright (c) 2010 Roland Elek <elek.roland@gmail.com>
6 * Copyright (c) 2010 Sebastian Herbszt <herbszt@gmx.de>
7 * Copyright (c) 2010 Alexander Graf <agraf@suse.de>
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/>.
25 #include <hw/pci/msi.h>
26 #include <hw/i386/pc.h>
27 #include <hw/pci/pci.h>
28 #include <hw/sysbus.h>
30 #include "monitor/monitor.h"
31 #include "sysemu/dma.h"
33 #include <hw/ide/pci.h>
34 #include <hw/ide/ahci.h>
36 /* #define DEBUG_AHCI */
39 #define DPRINTF(port, fmt, ...) \
40 do { fprintf(stderr, "ahci: %s: [%d] ", __FUNCTION__, port); \
41 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(port, fmt, ...) do {} while(0)
46 static void check_cmd(AHCIState
*s
, int port
);
47 static int handle_cmd(AHCIState
*s
,int port
,int slot
);
48 static void ahci_reset_port(AHCIState
*s
, int port
);
49 static void ahci_write_fis_d2h(AHCIDevice
*ad
, uint8_t *cmd_fis
);
50 static void ahci_init_d2h(AHCIDevice
*ad
);
52 static uint32_t ahci_port_read(AHCIState
*s
, int port
, int offset
)
56 pr
= &s
->dev
[port
].port_regs
;
62 case PORT_LST_ADDR_HI
:
63 val
= pr
->lst_addr_hi
;
68 case PORT_FIS_ADDR_HI
:
69 val
= pr
->fis_addr_hi
;
81 val
= ((uint16_t)s
->dev
[port
].port
.ifs
[0].error
<< 8) |
82 s
->dev
[port
].port
.ifs
[0].status
;
88 if (s
->dev
[port
].port
.ifs
[0].bs
) {
89 val
= SATA_SCR_SSTATUS_DET_DEV_PRESENT_PHY_UP
|
90 SATA_SCR_SSTATUS_SPD_GEN1
| SATA_SCR_SSTATUS_IPM_ACTIVE
;
92 val
= SATA_SCR_SSTATUS_DET_NODEV
;
102 pr
->scr_act
&= ~s
->dev
[port
].finished
;
103 s
->dev
[port
].finished
= 0;
113 DPRINTF(port
, "offset: 0x%x val: 0x%x\n", offset
, val
);
118 static void ahci_irq_raise(AHCIState
*s
, AHCIDevice
*dev
)
120 AHCIPCIState
*d
= container_of(s
, AHCIPCIState
, ahci
);
121 PCIDevice
*pci_dev
= PCI_DEVICE(d
);
123 DPRINTF(0, "raise irq\n");
125 if (msi_enabled(pci_dev
)) {
126 msi_notify(pci_dev
, 0);
128 qemu_irq_raise(s
->irq
);
132 static void ahci_irq_lower(AHCIState
*s
, AHCIDevice
*dev
)
134 AHCIPCIState
*d
= container_of(s
, AHCIPCIState
, ahci
);
136 DPRINTF(0, "lower irq\n");
138 if (!msi_enabled(PCI_DEVICE(d
))) {
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
)
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
= true;
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
, hwaddr 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
, hwaddr 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");
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 const 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
, hwaddr 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
, hwaddr 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 const 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 void check_cmd(AHCIState
*s
, int port
)
433 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
436 if ((pr
->cmd
& PORT_CMD_START
) && pr
->cmd_issue
) {
437 for (slot
= 0; (slot
< 32) && pr
->cmd_issue
; slot
++) {
438 if ((pr
->cmd_issue
& (1 << slot
)) &&
439 !handle_cmd(s
, port
, slot
)) {
440 pr
->cmd_issue
&= ~(1 << slot
);
446 static void ahci_check_cmd_bh(void *opaque
)
448 AHCIDevice
*ad
= opaque
;
450 qemu_bh_delete(ad
->check_bh
);
453 if ((ad
->busy_slot
!= -1) &&
454 !(ad
->port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
))) {
456 ad
->port_regs
.cmd_issue
&= ~(1 << ad
->busy_slot
);
460 check_cmd(ad
->hba
, ad
->port_no
);
463 static void ahci_init_d2h(AHCIDevice
*ad
)
465 uint8_t init_fis
[20];
466 IDEState
*ide_state
= &ad
->port
.ifs
[0];
468 memset(init_fis
, 0, sizeof(init_fis
));
473 if (ide_state
->drive_kind
== IDE_CD
) {
474 init_fis
[5] = ide_state
->lcyl
;
475 init_fis
[6] = ide_state
->hcyl
;
478 ahci_write_fis_d2h(ad
, init_fis
);
481 static void ahci_reset_port(AHCIState
*s
, int port
)
483 AHCIDevice
*d
= &s
->dev
[port
];
484 AHCIPortRegs
*pr
= &d
->port_regs
;
485 IDEState
*ide_state
= &d
->port
.ifs
[0];
488 DPRINTF(port
, "reset port\n");
490 ide_bus_reset(&d
->port
);
491 ide_state
->ncq_queues
= AHCI_MAX_CMDS
;
497 d
->init_d2h_sent
= false;
499 ide_state
= &s
->dev
[port
].port
.ifs
[0];
500 if (!ide_state
->bs
) {
504 /* reset ncq queue */
505 for (i
= 0; i
< AHCI_MAX_CMDS
; i
++) {
506 NCQTransferState
*ncq_tfs
= &s
->dev
[port
].ncq_tfs
[i
];
507 if (!ncq_tfs
->used
) {
511 if (ncq_tfs
->aiocb
) {
512 bdrv_aio_cancel(ncq_tfs
->aiocb
);
513 ncq_tfs
->aiocb
= NULL
;
516 /* Maybe we just finished the request thanks to bdrv_aio_cancel() */
517 if (!ncq_tfs
->used
) {
521 qemu_sglist_destroy(&ncq_tfs
->sglist
);
525 s
->dev
[port
].port_state
= STATE_RUN
;
526 if (!ide_state
->bs
) {
527 s
->dev
[port
].port_regs
.sig
= 0;
528 ide_state
->status
= SEEK_STAT
| WRERR_STAT
;
529 } else if (ide_state
->drive_kind
== IDE_CD
) {
530 s
->dev
[port
].port_regs
.sig
= SATA_SIGNATURE_CDROM
;
531 ide_state
->lcyl
= 0x14;
532 ide_state
->hcyl
= 0xeb;
533 DPRINTF(port
, "set lcyl = %d\n", ide_state
->lcyl
);
534 ide_state
->status
= SEEK_STAT
| WRERR_STAT
| READY_STAT
;
536 s
->dev
[port
].port_regs
.sig
= SATA_SIGNATURE_DISK
;
537 ide_state
->status
= SEEK_STAT
| WRERR_STAT
;
540 ide_state
->error
= 1;
544 static void debug_print_fis(uint8_t *fis
, int cmd_len
)
549 fprintf(stderr
, "fis:");
550 for (i
= 0; i
< cmd_len
; i
++) {
551 if ((i
& 0xf) == 0) {
552 fprintf(stderr
, "\n%02x:",i
);
554 fprintf(stderr
, "%02x ",fis
[i
]);
556 fprintf(stderr
, "\n");
560 static void ahci_write_fis_sdb(AHCIState
*s
, int port
, uint32_t finished
)
562 AHCIPortRegs
*pr
= &s
->dev
[port
].port_regs
;
566 if (!s
->dev
[port
].res_fis
||
567 !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
571 sdb_fis
= &s
->dev
[port
].res_fis
[RES_FIS_SDBFIS
];
572 ide_state
= &s
->dev
[port
].port
.ifs
[0];
575 *(uint32_t*)sdb_fis
= 0;
578 sdb_fis
[0] = ide_state
->error
;
579 sdb_fis
[2] = ide_state
->status
& 0x77;
580 s
->dev
[port
].finished
|= finished
;
581 *(uint32_t*)(sdb_fis
+ 4) = cpu_to_le32(s
->dev
[port
].finished
);
583 ahci_trigger_irq(s
, &s
->dev
[port
], PORT_IRQ_STAT_SDBS
);
586 static void ahci_write_fis_d2h(AHCIDevice
*ad
, uint8_t *cmd_fis
)
588 AHCIPortRegs
*pr
= &ad
->port_regs
;
591 dma_addr_t cmd_len
= 0x80;
594 if (!ad
->res_fis
|| !(pr
->cmd
& PORT_CMD_FIS_RX
)) {
600 uint64_t tbl_addr
= le64_to_cpu(ad
->cur_cmd
->tbl_addr
);
601 cmd_fis
= dma_memory_map(ad
->hba
->as
, tbl_addr
, &cmd_len
,
602 DMA_DIRECTION_TO_DEVICE
);
606 d2h_fis
= &ad
->res_fis
[RES_FIS_RFIS
];
609 d2h_fis
[1] = (ad
->hba
->control_regs
.irqstatus
? (1 << 6) : 0);
610 d2h_fis
[2] = ad
->port
.ifs
[0].status
;
611 d2h_fis
[3] = ad
->port
.ifs
[0].error
;
613 d2h_fis
[4] = cmd_fis
[4];
614 d2h_fis
[5] = cmd_fis
[5];
615 d2h_fis
[6] = cmd_fis
[6];
616 d2h_fis
[7] = cmd_fis
[7];
617 d2h_fis
[8] = cmd_fis
[8];
618 d2h_fis
[9] = cmd_fis
[9];
619 d2h_fis
[10] = cmd_fis
[10];
620 d2h_fis
[11] = cmd_fis
[11];
621 d2h_fis
[12] = cmd_fis
[12];
622 d2h_fis
[13] = cmd_fis
[13];
623 for (i
= 14; i
< 20; i
++) {
627 if (d2h_fis
[2] & ERR_STAT
) {
628 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_STAT_TFES
);
631 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_D2H_REG_FIS
);
634 dma_memory_unmap(ad
->hba
->as
, cmd_fis
, cmd_len
,
635 DMA_DIRECTION_TO_DEVICE
, cmd_len
);
639 static int ahci_populate_sglist(AHCIDevice
*ad
, QEMUSGList
*sglist
, int offset
)
641 AHCICmdHdr
*cmd
= ad
->cur_cmd
;
642 uint32_t opts
= le32_to_cpu(cmd
->opts
);
643 uint64_t prdt_addr
= le64_to_cpu(cmd
->tbl_addr
) + 0x80;
644 int sglist_alloc_hint
= opts
>> AHCI_CMD_HDR_PRDT_LEN
;
645 dma_addr_t prdt_len
= (sglist_alloc_hint
* sizeof(AHCI_SG
));
646 dma_addr_t real_prdt_len
= prdt_len
;
654 IDEBus
*bus
= &ad
->port
;
655 BusState
*qbus
= BUS(bus
);
657 if (!sglist_alloc_hint
) {
658 DPRINTF(ad
->port_no
, "no sg list given by guest: 0x%08x\n", opts
);
663 if (!(prdt
= dma_memory_map(ad
->hba
->as
, prdt_addr
, &prdt_len
,
664 DMA_DIRECTION_TO_DEVICE
))){
665 DPRINTF(ad
->port_no
, "map failed\n");
669 if (prdt_len
< real_prdt_len
) {
670 DPRINTF(ad
->port_no
, "mapped less than expected\n");
675 /* Get entries in the PRDT, init a qemu sglist accordingly */
676 if (sglist_alloc_hint
> 0) {
677 AHCI_SG
*tbl
= (AHCI_SG
*)prdt
;
679 for (i
= 0; i
< sglist_alloc_hint
; i
++) {
680 /* flags_size is zero-based */
681 tbl_entry_size
= (le32_to_cpu(tbl
[i
].flags_size
) + 1);
682 if (offset
<= (sum
+ tbl_entry_size
)) {
684 off_pos
= offset
- sum
;
687 sum
+= tbl_entry_size
;
689 if ((off_idx
== -1) || (off_pos
< 0) || (off_pos
> tbl_entry_size
)) {
690 DPRINTF(ad
->port_no
, "%s: Incorrect offset! "
691 "off_idx: %d, off_pos: %d\n",
692 __func__
, off_idx
, off_pos
);
697 qemu_sglist_init(sglist
, qbus
->parent
, (sglist_alloc_hint
- off_idx
),
699 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[off_idx
].addr
+ off_pos
),
700 le32_to_cpu(tbl
[off_idx
].flags_size
) + 1 - off_pos
);
702 for (i
= off_idx
+ 1; i
< sglist_alloc_hint
; i
++) {
703 /* flags_size is zero-based */
704 qemu_sglist_add(sglist
, le64_to_cpu(tbl
[i
].addr
),
705 le32_to_cpu(tbl
[i
].flags_size
) + 1);
710 dma_memory_unmap(ad
->hba
->as
, prdt
, prdt_len
,
711 DMA_DIRECTION_TO_DEVICE
, prdt_len
);
715 static void ncq_cb(void *opaque
, int ret
)
717 NCQTransferState
*ncq_tfs
= (NCQTransferState
*)opaque
;
718 IDEState
*ide_state
= &ncq_tfs
->drive
->port
.ifs
[0];
720 /* Clear bit for this tag in SActive */
721 ncq_tfs
->drive
->port_regs
.scr_act
&= ~(1 << ncq_tfs
->tag
);
725 ide_state
->error
= ABRT_ERR
;
726 ide_state
->status
= READY_STAT
| ERR_STAT
;
727 ncq_tfs
->drive
->port_regs
.scr_err
|= (1 << ncq_tfs
->tag
);
729 ide_state
->status
= READY_STAT
| SEEK_STAT
;
732 ahci_write_fis_sdb(ncq_tfs
->drive
->hba
, ncq_tfs
->drive
->port_no
,
733 (1 << ncq_tfs
->tag
));
735 DPRINTF(ncq_tfs
->drive
->port_no
, "NCQ transfer tag %d finished\n",
738 bdrv_acct_done(ncq_tfs
->drive
->port
.ifs
[0].bs
, &ncq_tfs
->acct
);
739 qemu_sglist_destroy(&ncq_tfs
->sglist
);
743 static void process_ncq_command(AHCIState
*s
, int port
, uint8_t *cmd_fis
,
746 NCQFrame
*ncq_fis
= (NCQFrame
*)cmd_fis
;
747 uint8_t tag
= ncq_fis
->tag
>> 3;
748 NCQTransferState
*ncq_tfs
= &s
->dev
[port
].ncq_tfs
[tag
];
751 /* error - already in use */
752 fprintf(stderr
, "%s: tag %d already used\n", __FUNCTION__
, tag
);
757 ncq_tfs
->drive
= &s
->dev
[port
];
758 ncq_tfs
->slot
= slot
;
759 ncq_tfs
->lba
= ((uint64_t)ncq_fis
->lba5
<< 40) |
760 ((uint64_t)ncq_fis
->lba4
<< 32) |
761 ((uint64_t)ncq_fis
->lba3
<< 24) |
762 ((uint64_t)ncq_fis
->lba2
<< 16) |
763 ((uint64_t)ncq_fis
->lba1
<< 8) |
764 (uint64_t)ncq_fis
->lba0
;
766 /* Note: We calculate the sector count, but don't currently rely on it.
767 * The total size of the DMA buffer tells us the transfer size instead. */
768 ncq_tfs
->sector_count
= ((uint16_t)ncq_fis
->sector_count_high
<< 8) |
769 ncq_fis
->sector_count_low
;
771 DPRINTF(port
, "NCQ transfer LBA from %"PRId64
" to %"PRId64
", "
772 "drive max %"PRId64
"\n",
773 ncq_tfs
->lba
, ncq_tfs
->lba
+ ncq_tfs
->sector_count
- 2,
774 s
->dev
[port
].port
.ifs
[0].nb_sectors
- 1);
776 ahci_populate_sglist(&s
->dev
[port
], &ncq_tfs
->sglist
, 0);
779 switch(ncq_fis
->command
) {
780 case READ_FPDMA_QUEUED
:
781 DPRINTF(port
, "NCQ reading %d sectors from LBA %"PRId64
", "
783 ncq_tfs
->sector_count
-1, ncq_tfs
->lba
, ncq_tfs
->tag
);
785 DPRINTF(port
, "tag %d aio read %"PRId64
"\n",
786 ncq_tfs
->tag
, ncq_tfs
->lba
);
788 dma_acct_start(ncq_tfs
->drive
->port
.ifs
[0].bs
, &ncq_tfs
->acct
,
789 &ncq_tfs
->sglist
, BDRV_ACCT_READ
);
790 ncq_tfs
->aiocb
= dma_bdrv_read(ncq_tfs
->drive
->port
.ifs
[0].bs
,
791 &ncq_tfs
->sglist
, ncq_tfs
->lba
,
794 case WRITE_FPDMA_QUEUED
:
795 DPRINTF(port
, "NCQ writing %d sectors to LBA %"PRId64
", tag %d\n",
796 ncq_tfs
->sector_count
-1, ncq_tfs
->lba
, ncq_tfs
->tag
);
798 DPRINTF(port
, "tag %d aio write %"PRId64
"\n",
799 ncq_tfs
->tag
, ncq_tfs
->lba
);
801 dma_acct_start(ncq_tfs
->drive
->port
.ifs
[0].bs
, &ncq_tfs
->acct
,
802 &ncq_tfs
->sglist
, BDRV_ACCT_WRITE
);
803 ncq_tfs
->aiocb
= dma_bdrv_write(ncq_tfs
->drive
->port
.ifs
[0].bs
,
804 &ncq_tfs
->sglist
, ncq_tfs
->lba
,
808 DPRINTF(port
, "error: tried to process non-NCQ command as NCQ\n");
809 qemu_sglist_destroy(&ncq_tfs
->sglist
);
814 static int handle_cmd(AHCIState
*s
, int port
, int slot
)
823 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
824 /* Engine currently busy, try again later */
825 DPRINTF(port
, "engine busy\n");
829 cmd
= &((AHCICmdHdr
*)s
->dev
[port
].lst
)[slot
];
831 if (!s
->dev
[port
].lst
) {
832 DPRINTF(port
, "error: lst not given but cmd handled");
836 /* remember current slot handle for later */
837 s
->dev
[port
].cur_cmd
= cmd
;
839 opts
= le32_to_cpu(cmd
->opts
);
840 tbl_addr
= le64_to_cpu(cmd
->tbl_addr
);
843 cmd_fis
= dma_memory_map(s
->as
, tbl_addr
, &cmd_len
,
844 DMA_DIRECTION_FROM_DEVICE
);
847 DPRINTF(port
, "error: guest passed us an invalid cmd fis\n");
851 /* The device we are working for */
852 ide_state
= &s
->dev
[port
].port
.ifs
[0];
854 if (!ide_state
->bs
) {
855 DPRINTF(port
, "error: guest accessed unused port");
859 debug_print_fis(cmd_fis
, 0x90);
860 //debug_print_fis(cmd_fis, (opts & AHCI_CMD_HDR_CMD_FIS_LEN) * 4);
862 switch (cmd_fis
[0]) {
863 case SATA_FIS_TYPE_REGISTER_H2D
:
866 DPRINTF(port
, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
867 "cmd_fis[2]=%02x\n", cmd_fis
[0], cmd_fis
[1],
873 switch (cmd_fis
[1]) {
874 case SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER
:
879 DPRINTF(port
, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
880 "cmd_fis[2]=%02x\n", cmd_fis
[0], cmd_fis
[1],
886 switch (s
->dev
[port
].port_state
) {
888 if (cmd_fis
[15] & ATA_SRST
) {
889 s
->dev
[port
].port_state
= STATE_RESET
;
893 if (!(cmd_fis
[15] & ATA_SRST
)) {
894 ahci_reset_port(s
, port
);
899 if (cmd_fis
[1] == SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER
) {
901 /* Check for NCQ command */
902 if ((cmd_fis
[2] == READ_FPDMA_QUEUED
) ||
903 (cmd_fis
[2] == WRITE_FPDMA_QUEUED
)) {
904 process_ncq_command(s
, port
, cmd_fis
, slot
);
908 /* Decompose the FIS */
909 ide_state
->nsector
= (int64_t)((cmd_fis
[13] << 8) | cmd_fis
[12]);
910 ide_state
->feature
= cmd_fis
[3];
911 if (!ide_state
->nsector
) {
912 ide_state
->nsector
= 256;
915 if (ide_state
->drive_kind
!= IDE_CD
) {
917 * We set the sector depending on the sector defined in the FIS.
918 * Unfortunately, the spec isn't exactly obvious on this one.
920 * Apparently LBA48 commands set fis bytes 10,9,8,6,5,4 to the
921 * 48 bit sector number. ATA_CMD_READ_DMA_EXT is an example for
924 * Non-LBA48 commands however use 7[lower 4 bits],6,5,4 to define a
925 * 28-bit sector number. ATA_CMD_READ_DMA is an example for such
928 * Since the spec doesn't explicitly state what each field should
929 * do, I simply assume non-used fields as reserved and OR everything
930 * together, independent of the command.
932 ide_set_sector(ide_state
, ((uint64_t)cmd_fis
[10] << 40)
933 | ((uint64_t)cmd_fis
[9] << 32)
934 /* This is used for LBA48 commands */
935 | ((uint64_t)cmd_fis
[8] << 24)
936 /* This is used for non-LBA48 commands */
937 | ((uint64_t)(cmd_fis
[7] & 0xf) << 24)
938 | ((uint64_t)cmd_fis
[6] << 16)
939 | ((uint64_t)cmd_fis
[5] << 8)
943 /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
944 * table to ide_state->io_buffer
946 if (opts
& AHCI_CMD_ATAPI
) {
947 memcpy(ide_state
->io_buffer
, &cmd_fis
[AHCI_COMMAND_TABLE_ACMD
], 0x10);
948 ide_state
->lcyl
= 0x14;
949 ide_state
->hcyl
= 0xeb;
950 debug_print_fis(ide_state
->io_buffer
, 0x10);
951 ide_state
->feature
= IDE_FEATURE_DMA
;
952 s
->dev
[port
].done_atapi_packet
= false;
953 /* XXX send PIO setup FIS */
956 ide_state
->error
= 0;
958 /* Reset transferred byte counter */
961 /* We're ready to process the command in FIS byte 2. */
962 ide_exec_cmd(&s
->dev
[port
].port
, cmd_fis
[2]);
964 if ((s
->dev
[port
].port
.ifs
[0].status
& (READY_STAT
|DRQ_STAT
|BUSY_STAT
)) ==
966 ahci_write_fis_d2h(&s
->dev
[port
], cmd_fis
);
971 dma_memory_unmap(s
->as
, cmd_fis
, cmd_len
, DMA_DIRECTION_FROM_DEVICE
,
974 if (s
->dev
[port
].port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
)) {
975 /* async command, complete later */
976 s
->dev
[port
].busy_slot
= slot
;
980 /* done handling the command */
984 /* DMA dev <-> ram */
985 static int ahci_start_transfer(IDEDMA
*dma
)
987 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
988 IDEState
*s
= &ad
->port
.ifs
[0];
989 uint32_t size
= (uint32_t)(s
->data_end
- s
->data_ptr
);
990 /* write == ram -> device */
991 uint32_t opts
= le32_to_cpu(ad
->cur_cmd
->opts
);
992 int is_write
= opts
& AHCI_CMD_WRITE
;
993 int is_atapi
= opts
& AHCI_CMD_ATAPI
;
996 if (is_atapi
&& !ad
->done_atapi_packet
) {
997 /* already prepopulated iobuffer */
998 ad
->done_atapi_packet
= true;
1002 if (!ahci_populate_sglist(ad
, &s
->sg
, 0)) {
1006 DPRINTF(ad
->port_no
, "%sing %d bytes on %s w/%s sglist\n",
1007 is_write
? "writ" : "read", size
, is_atapi
? "atapi" : "ata",
1008 has_sglist
? "" : "o");
1010 if (has_sglist
&& size
) {
1012 dma_buf_write(s
->data_ptr
, size
, &s
->sg
);
1014 dma_buf_read(s
->data_ptr
, size
, &s
->sg
);
1018 /* update number of transferred bytes */
1019 ad
->cur_cmd
->status
= cpu_to_le32(le32_to_cpu(ad
->cur_cmd
->status
) + size
);
1022 /* declare that we processed everything */
1023 s
->data_ptr
= s
->data_end
;
1026 qemu_sglist_destroy(&s
->sg
);
1029 s
->end_transfer_func(s
);
1031 if (!(s
->status
& DRQ_STAT
)) {
1033 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_STAT_DSS
);
1039 static void ahci_start_dma(IDEDMA
*dma
, IDEState
*s
,
1040 BlockDriverCompletionFunc
*dma_cb
)
1043 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1045 DPRINTF(ad
->port_no
, "\n");
1046 s
->io_buffer_offset
= 0;
1050 static int ahci_dma_prepare_buf(IDEDMA
*dma
, int is_write
)
1052 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1053 IDEState
*s
= &ad
->port
.ifs
[0];
1055 ahci_populate_sglist(ad
, &s
->sg
, 0);
1056 s
->io_buffer_size
= s
->sg
.size
;
1058 DPRINTF(ad
->port_no
, "len=%#x\n", s
->io_buffer_size
);
1059 return s
->io_buffer_size
!= 0;
1062 static int ahci_dma_rw_buf(IDEDMA
*dma
, int is_write
)
1064 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1065 IDEState
*s
= &ad
->port
.ifs
[0];
1066 uint8_t *p
= s
->io_buffer
+ s
->io_buffer_index
;
1067 int l
= s
->io_buffer_size
- s
->io_buffer_index
;
1069 if (ahci_populate_sglist(ad
, &s
->sg
, s
->io_buffer_offset
)) {
1074 dma_buf_read(p
, l
, &s
->sg
);
1076 dma_buf_write(p
, l
, &s
->sg
);
1079 /* free sglist that was created in ahci_populate_sglist() */
1080 qemu_sglist_destroy(&s
->sg
);
1082 /* update number of transferred bytes */
1083 ad
->cur_cmd
->status
= cpu_to_le32(le32_to_cpu(ad
->cur_cmd
->status
) + l
);
1084 s
->io_buffer_index
+= l
;
1085 s
->io_buffer_offset
+= l
;
1087 DPRINTF(ad
->port_no
, "len=%#x\n", l
);
1092 static int ahci_dma_set_unit(IDEDMA
*dma
, int unit
)
1094 /* only a single unit per link */
1098 static int ahci_dma_add_status(IDEDMA
*dma
, int status
)
1100 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1101 DPRINTF(ad
->port_no
, "set status: %x\n", status
);
1103 if (status
& BM_STATUS_INT
) {
1104 ahci_trigger_irq(ad
->hba
, ad
, PORT_IRQ_STAT_DSS
);
1110 static int ahci_dma_set_inactive(IDEDMA
*dma
)
1115 static int ahci_async_cmd_done(IDEDMA
*dma
)
1117 AHCIDevice
*ad
= DO_UPCAST(AHCIDevice
, dma
, dma
);
1119 DPRINTF(ad
->port_no
, "async cmd done\n");
1121 /* update d2h status */
1122 ahci_write_fis_d2h(ad
, NULL
);
1124 if (!ad
->check_bh
) {
1125 /* maybe we still have something to process, check later */
1126 ad
->check_bh
= qemu_bh_new(ahci_check_cmd_bh
, ad
);
1127 qemu_bh_schedule(ad
->check_bh
);
1133 static void ahci_irq_set(void *opaque
, int n
, int level
)
1137 static void ahci_dma_restart_cb(void *opaque
, int running
, RunState state
)
1141 static int ahci_dma_reset(IDEDMA
*dma
)
1146 static const IDEDMAOps ahci_dma_ops
= {
1147 .start_dma
= ahci_start_dma
,
1148 .start_transfer
= ahci_start_transfer
,
1149 .prepare_buf
= ahci_dma_prepare_buf
,
1150 .rw_buf
= ahci_dma_rw_buf
,
1151 .set_unit
= ahci_dma_set_unit
,
1152 .add_status
= ahci_dma_add_status
,
1153 .set_inactive
= ahci_dma_set_inactive
,
1154 .async_cmd_done
= ahci_async_cmd_done
,
1155 .restart_cb
= ahci_dma_restart_cb
,
1156 .reset
= ahci_dma_reset
,
1159 void ahci_init(AHCIState
*s
, DeviceState
*qdev
, AddressSpace
*as
, int ports
)
1166 s
->dev
= g_malloc0(sizeof(AHCIDevice
) * ports
);
1168 /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
1169 memory_region_init_io(&s
->mem
, OBJECT(qdev
), &ahci_mem_ops
, s
,
1170 "ahci", AHCI_MEM_BAR_SIZE
);
1171 memory_region_init_io(&s
->idp
, OBJECT(qdev
), &ahci_idp_ops
, s
,
1174 irqs
= qemu_allocate_irqs(ahci_irq_set
, s
, s
->ports
);
1176 for (i
= 0; i
< s
->ports
; i
++) {
1177 AHCIDevice
*ad
= &s
->dev
[i
];
1179 ide_bus_new(&ad
->port
, sizeof(ad
->port
), qdev
, i
, 1);
1180 ide_init2(&ad
->port
, irqs
[i
]);
1184 ad
->port
.dma
= &ad
->dma
;
1185 ad
->port
.dma
->ops
= &ahci_dma_ops
;
1189 void ahci_uninit(AHCIState
*s
)
1191 memory_region_destroy(&s
->mem
);
1192 memory_region_destroy(&s
->idp
);
1196 void ahci_reset(AHCIState
*s
)
1201 s
->control_regs
.irqstatus
= 0;
1203 * The implementation of this bit is dependent upon the value of the
1204 * CAP.SAM bit. If CAP.SAM is '0', then GHC.AE shall be read-write and
1205 * shall have a reset value of '0'. If CAP.SAM is '1', then AE shall be
1206 * read-only and shall have a reset value of '1'.
1208 * We set HOST_CAP_AHCI so we must enable AHCI at reset.
1210 s
->control_regs
.ghc
= HOST_CTL_AHCI_EN
;
1212 for (i
= 0; i
< s
->ports
; i
++) {
1213 pr
= &s
->dev
[i
].port_regs
;
1217 pr
->cmd
= PORT_CMD_SPIN_UP
| PORT_CMD_POWER_ON
;
1218 ahci_reset_port(s
, i
);
1222 static const VMStateDescription vmstate_ahci_device
= {
1223 .name
= "ahci port",
1225 .fields
= (VMStateField
[]) {
1226 VMSTATE_IDE_BUS(port
, AHCIDevice
),
1227 VMSTATE_UINT32(port_state
, AHCIDevice
),
1228 VMSTATE_UINT32(finished
, AHCIDevice
),
1229 VMSTATE_UINT32(port_regs
.lst_addr
, AHCIDevice
),
1230 VMSTATE_UINT32(port_regs
.lst_addr_hi
, AHCIDevice
),
1231 VMSTATE_UINT32(port_regs
.fis_addr
, AHCIDevice
),
1232 VMSTATE_UINT32(port_regs
.fis_addr_hi
, AHCIDevice
),
1233 VMSTATE_UINT32(port_regs
.irq_stat
, AHCIDevice
),
1234 VMSTATE_UINT32(port_regs
.irq_mask
, AHCIDevice
),
1235 VMSTATE_UINT32(port_regs
.cmd
, AHCIDevice
),
1236 VMSTATE_UINT32(port_regs
.tfdata
, AHCIDevice
),
1237 VMSTATE_UINT32(port_regs
.sig
, AHCIDevice
),
1238 VMSTATE_UINT32(port_regs
.scr_stat
, AHCIDevice
),
1239 VMSTATE_UINT32(port_regs
.scr_ctl
, AHCIDevice
),
1240 VMSTATE_UINT32(port_regs
.scr_err
, AHCIDevice
),
1241 VMSTATE_UINT32(port_regs
.scr_act
, AHCIDevice
),
1242 VMSTATE_UINT32(port_regs
.cmd_issue
, AHCIDevice
),
1243 VMSTATE_BOOL(done_atapi_packet
, AHCIDevice
),
1244 VMSTATE_INT32(busy_slot
, AHCIDevice
),
1245 VMSTATE_BOOL(init_d2h_sent
, AHCIDevice
),
1246 VMSTATE_END_OF_LIST()
1250 static int ahci_state_post_load(void *opaque
, int version_id
)
1253 struct AHCIDevice
*ad
;
1254 AHCIState
*s
= opaque
;
1256 for (i
= 0; i
< s
->ports
; i
++) {
1258 AHCIPortRegs
*pr
= &ad
->port_regs
;
1261 ((uint64_t)pr
->lst_addr_hi
<< 32) | pr
->lst_addr
, 1024);
1262 map_page(&ad
->res_fis
,
1263 ((uint64_t)pr
->fis_addr_hi
<< 32) | pr
->fis_addr
, 256);
1265 * All pending i/o should be flushed out on a migrate. However,
1266 * we might not have cleared the busy_slot since this is done
1267 * in a bh. Also, issue i/o against any slots that are pending.
1269 if ((ad
->busy_slot
!= -1) &&
1270 !(ad
->port
.ifs
[0].status
& (BUSY_STAT
|DRQ_STAT
))) {
1271 pr
->cmd_issue
&= ~(1 << ad
->busy_slot
);
1280 const VMStateDescription vmstate_ahci
= {
1283 .post_load
= ahci_state_post_load
,
1284 .fields
= (VMStateField
[]) {
1285 VMSTATE_STRUCT_VARRAY_POINTER_INT32(dev
, AHCIState
, ports
,
1286 vmstate_ahci_device
, AHCIDevice
),
1287 VMSTATE_UINT32(control_regs
.cap
, AHCIState
),
1288 VMSTATE_UINT32(control_regs
.ghc
, AHCIState
),
1289 VMSTATE_UINT32(control_regs
.irqstatus
, AHCIState
),
1290 VMSTATE_UINT32(control_regs
.impl
, AHCIState
),
1291 VMSTATE_UINT32(control_regs
.version
, AHCIState
),
1292 VMSTATE_UINT32(idp_index
, AHCIState
),
1293 VMSTATE_INT32(ports
, AHCIState
),
1294 VMSTATE_END_OF_LIST()
1298 #define TYPE_SYSBUS_AHCI "sysbus-ahci"
1299 #define SYSBUS_AHCI(obj) OBJECT_CHECK(SysbusAHCIState, (obj), TYPE_SYSBUS_AHCI)
1301 typedef struct SysbusAHCIState
{
1303 SysBusDevice parent_obj
;
1310 static const VMStateDescription vmstate_sysbus_ahci
= {
1311 .name
= "sysbus-ahci",
1312 .unmigratable
= 1, /* Still buggy under I/O load */
1313 .fields
= (VMStateField
[]) {
1314 VMSTATE_AHCI(ahci
, AHCIPCIState
),
1315 VMSTATE_END_OF_LIST()
1319 static void sysbus_ahci_reset(DeviceState
*dev
)
1321 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1323 ahci_reset(&s
->ahci
);
1326 static void sysbus_ahci_realize(DeviceState
*dev
, Error
**errp
)
1328 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1329 SysbusAHCIState
*s
= SYSBUS_AHCI(dev
);
1331 ahci_init(&s
->ahci
, dev
, NULL
, s
->num_ports
);
1333 sysbus_init_mmio(sbd
, &s
->ahci
.mem
);
1334 sysbus_init_irq(sbd
, &s
->ahci
.irq
);
1337 static Property sysbus_ahci_properties
[] = {
1338 DEFINE_PROP_UINT32("num-ports", SysbusAHCIState
, num_ports
, 1),
1339 DEFINE_PROP_END_OF_LIST(),
1342 static void sysbus_ahci_class_init(ObjectClass
*klass
, void *data
)
1344 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1346 dc
->realize
= sysbus_ahci_realize
;
1347 dc
->vmsd
= &vmstate_sysbus_ahci
;
1348 dc
->props
= sysbus_ahci_properties
;
1349 dc
->reset
= sysbus_ahci_reset
;
1350 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1353 static const TypeInfo sysbus_ahci_info
= {
1354 .name
= TYPE_SYSBUS_AHCI
,
1355 .parent
= TYPE_SYS_BUS_DEVICE
,
1356 .instance_size
= sizeof(SysbusAHCIState
),
1357 .class_init
= sysbus_ahci_class_init
,
1360 static void sysbus_ahci_register_types(void)
1362 type_register_static(&sysbus_ahci_info
);
1365 type_init(sysbus_ahci_register_types
)