2 * Copyright (c) 2013 Zhixiang Yu <zcore@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/linker_set.h>
36 #include <sys/ioctl.h>
39 #include <sys/endian.h>
51 #include <pthread_np.h>
60 #define MAX_PORTS 6 /* Intel ICH8 AHCI supports 6 ports */
62 #define PxSIG_ATA 0x00000101 /* ATA drive */
63 #define PxSIG_ATAPI 0xeb140101 /* ATAPI drive */
66 FIS_TYPE_REGH2D = 0x27, /* Register FIS - host to device */
67 FIS_TYPE_REGD2H = 0x34, /* Register FIS - device to host */
68 FIS_TYPE_DMAACT = 0x39, /* DMA activate FIS - device to host */
69 FIS_TYPE_DMASETUP = 0x41, /* DMA setup FIS - bidirectional */
70 FIS_TYPE_DATA = 0x46, /* Data FIS - bidirectional */
71 FIS_TYPE_BIST = 0x58, /* BIST activate FIS - bidirectional */
72 FIS_TYPE_PIOSETUP = 0x5F, /* PIO setup FIS - device to host */
73 FIS_TYPE_SETDEVBITS = 0xA1, /* Set dev bits FIS - device to host */
79 #define TEST_UNIT_READY 0x00
80 #define REQUEST_SENSE 0x03
82 #define START_STOP_UNIT 0x1B
83 #define PREVENT_ALLOW 0x1E
84 #define READ_CAPACITY 0x25
86 #define POSITION_TO_ELEMENT 0x2B
88 #define GET_EVENT_STATUS_NOTIFICATION 0x4A
89 #define MODE_SENSE_10 0x5A
90 #define REPORT_LUNS 0xA0
95 * SCSI mode page codes
97 #define MODEPAGE_RW_ERROR_RECOVERY 0x01
98 #define MODEPAGE_CD_CAPABILITIES 0x2A
103 #define ATA_SF_ENAB_SATA_SF 0x10
104 #define ATA_SATA_SF_AN 0x05
105 #define ATA_SF_DIS_SATA_SF 0x90
112 #define DPRINTF(format, arg...) do{fprintf(dbg, format, ##arg);fflush(dbg);}while(0)
114 #define DPRINTF(format, arg...)
116 #define WPRINTF(format, arg...) printf(format, ##arg)
119 struct blockif_req io_req;
120 struct ahci_port *io_pr;
121 STAILQ_ENTRY(ahci_ioreq) io_flist;
122 TAILQ_ENTRY(ahci_ioreq) io_blist;
131 struct blockif_ctxt *bctx;
132 struct pci_ahci_softc *pr_sc;
141 uint8_t err_cfis[20];
168 struct ahci_ioreq *ioreq;
170 STAILQ_HEAD(ahci_fhead, ahci_ioreq) iofhd;
171 TAILQ_HEAD(ahci_bhead, ahci_ioreq) iobhd;
174 struct ahci_cmd_hdr {
179 uint32_t reserved[4];
182 struct ahci_prdt_entry {
185 #define DBCMASK 0x3fffff
189 struct pci_ahci_softc {
190 struct pci_devinst *asc_pi;
205 struct ahci_port port[MAX_PORTS];
207 #define ahci_ctx(sc) ((sc)->asc_pi->pi_vmctx)
209 static void ahci_handle_port(struct ahci_port *p);
211 static inline void lba_to_msf(uint8_t *buf, int lba)
214 buf[0] = (lba / 75) / 60;
215 buf[1] = (lba / 75) % 60;
220 * generate HBA intr depending on whether or not ports within
221 * the controller have an interrupt pending.
224 ahci_generate_intr(struct pci_ahci_softc *sc)
226 struct pci_devinst *pi;
231 for (i = 0; i < sc->ports; i++) {
232 struct ahci_port *pr;
238 DPRINTF("%s %x\n", __func__, sc->is);
240 if (sc->is && (sc->ghc & AHCI_GHC_IE)) {
241 if (pci_msi_enabled(pi)) {
243 * Generate an MSI interrupt on every edge
245 pci_generate_msi(pi, 0);
246 } else if (!sc->lintr) {
248 * Only generate a pin-based interrupt if one wasn't
252 pci_lintr_assert(pi);
254 } else if (sc->lintr) {
256 * No interrupts: deassert pin-based signal if it had
259 pci_lintr_deassert(pi);
265 ahci_write_fis(struct ahci_port *p, enum sata_fis_type ft, uint8_t *fis)
267 int offset, len, irq;
269 if (p->rfis == NULL || !(p->cmd & AHCI_P_CMD_FRE))
273 case FIS_TYPE_REGD2H:
276 irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_DHR : 0;
278 case FIS_TYPE_SETDEVBITS:
281 irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_SDB : 0;
283 case FIS_TYPE_PIOSETUP:
286 irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_PS : 0;
289 WPRINTF("unsupported fis type %d\n", ft);
292 if (fis[2] & ATA_S_ERROR) {
294 irq |= AHCI_P_IX_TFE;
296 memcpy(p->rfis + offset, fis, len);
299 ahci_generate_intr(p->pr_sc);
304 ahci_write_fis_piosetup(struct ahci_port *p)
308 memset(fis, 0, sizeof(fis));
309 fis[0] = FIS_TYPE_PIOSETUP;
310 ahci_write_fis(p, FIS_TYPE_PIOSETUP, fis);
314 ahci_write_fis_sdb(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
319 error = (tfd >> 8) & 0xff;
321 memset(fis, 0, sizeof(fis));
322 fis[0] = FIS_TYPE_SETDEVBITS;
326 if (fis[2] & ATA_S_ERROR) {
327 p->err_cfis[0] = slot;
328 p->err_cfis[2] = tfd;
329 p->err_cfis[3] = error;
330 memcpy(&p->err_cfis[4], cfis + 4, 16);
332 *(uint32_t *)(fis + 4) = (1 << slot);
333 p->sact &= ~(1 << slot);
337 ahci_write_fis(p, FIS_TYPE_SETDEVBITS, fis);
341 ahci_write_fis_d2h(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
346 error = (tfd >> 8) & 0xff;
347 memset(fis, 0, sizeof(fis));
348 fis[0] = FIS_TYPE_REGD2H;
362 if (fis[2] & ATA_S_ERROR) {
363 p->err_cfis[0] = 0x80;
364 p->err_cfis[2] = tfd & 0xff;
365 p->err_cfis[3] = error;
366 memcpy(&p->err_cfis[4], cfis + 4, 16);
368 p->ci &= ~(1 << slot);
370 ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
374 ahci_write_fis_d2h_ncq(struct ahci_port *p, int slot)
378 p->tfd = ATA_S_READY | ATA_S_DSC;
379 memset(fis, 0, sizeof(fis));
380 fis[0] = FIS_TYPE_REGD2H;
381 fis[1] = 0; /* No interrupt */
382 fis[2] = p->tfd; /* Status */
383 fis[3] = 0; /* No error */
384 p->ci &= ~(1 << slot);
385 ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
389 ahci_write_reset_fis_d2h(struct ahci_port *p)
393 memset(fis, 0, sizeof(fis));
394 fis[0] = FIS_TYPE_REGD2H;
402 ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
406 ahci_check_stopped(struct ahci_port *p)
409 * If we are no longer processing the command list and nothing
410 * is in-flight, clear the running bit, the current command
411 * slot, the command issue and active bits.
413 if (!(p->cmd & AHCI_P_CMD_ST)) {
414 if (p->pending == 0) {
416 p->cmd &= ~(AHCI_P_CMD_CR | AHCI_P_CMD_CCS_MASK);
425 ahci_port_stop(struct ahci_port *p)
427 struct ahci_ioreq *aior;
433 assert(pthread_mutex_isowned_np(&p->pr_sc->mtx));
435 TAILQ_FOREACH(aior, &p->iobhd, io_blist) {
437 * Try to cancel the outstanding blockif request.
439 error = blockif_cancel(p->bctx, &aior->io_req);
445 if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
446 cfis[2] == ATA_READ_FPDMA_QUEUED ||
447 cfis[2] == ATA_SEND_FPDMA_QUEUED)
451 p->sact &= ~(1 << slot);
453 p->ci &= ~(1 << slot);
456 * This command is now done.
458 p->pending &= ~(1 << slot);
461 * Delete the blockif request from the busy list
463 TAILQ_REMOVE(&p->iobhd, aior, io_blist);
466 * Move the blockif request back to the free list
468 STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
471 ahci_check_stopped(p);
475 ahci_port_reset(struct ahci_port *pr)
479 pr->xfermode = ATA_UDMA6;
480 pr->mult_sectors = 128;
483 pr->ssts = ATA_SS_DET_NO_DEVICE;
484 pr->sig = 0xFFFFFFFF;
488 pr->ssts = ATA_SS_DET_PHY_ONLINE | ATA_SS_IPM_ACTIVE;
489 if (pr->sctl & ATA_SC_SPD_MASK)
490 pr->ssts |= (pr->sctl & ATA_SC_SPD_MASK);
492 pr->ssts |= ATA_SS_SPD_GEN3;
493 pr->tfd = (1 << 8) | ATA_S_DSC | ATA_S_DMA;
496 pr->tfd |= ATA_S_READY;
498 pr->sig = PxSIG_ATAPI;
499 ahci_write_reset_fis_d2h(pr);
503 ahci_reset(struct pci_ahci_softc *sc)
507 sc->ghc = AHCI_GHC_AE;
511 pci_lintr_deassert(sc->asc_pi);
515 for (i = 0; i < sc->ports; i++) {
518 sc->port[i].cmd = (AHCI_P_CMD_SUD | AHCI_P_CMD_POD);
519 if (sc->port[i].bctx)
520 sc->port[i].cmd |= AHCI_P_CMD_CPS;
521 sc->port[i].sctl = 0;
522 ahci_port_reset(&sc->port[i]);
527 ata_string(uint8_t *dest, const char *src, int len)
531 for (i = 0; i < len; i++) {
533 dest[i ^ 1] = *src++;
540 atapi_string(uint8_t *dest, const char *src, int len)
544 for (i = 0; i < len; i++) {
553 * Build up the iovec based on the PRDT, 'done' and 'len'.
556 ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior,
557 struct ahci_prdt_entry *prdt, uint16_t prdtl)
559 struct blockif_req *breq = &aior->io_req;
560 int i, j, skip, todo, left, extra;
563 /* Copy part of PRDT between 'done' and 'len' bytes into the iov. */
565 left = aior->len - aior->done;
567 for (i = 0, j = 0; i < prdtl && j < BLOCKIF_IOV_MAX && left > 0;
569 dbcsz = (prdt->dbc & DBCMASK) + 1;
570 /* Skip already done part of the PRDT */
578 breq->br_iov[j].iov_base = paddr_guest2host(ahci_ctx(p->pr_sc),
579 prdt->dba + skip, dbcsz);
580 breq->br_iov[j].iov_len = dbcsz;
587 /* If we got limited by IOV length, round I/O down to sector size. */
588 if (j == BLOCKIF_IOV_MAX) {
589 extra = todo % blockif_sectsz(p->bctx);
593 if (breq->br_iov[j - 1].iov_len > extra) {
594 breq->br_iov[j - 1].iov_len -= extra;
597 extra -= breq->br_iov[j - 1].iov_len;
603 breq->br_resid = todo;
605 aior->more = (aior->done < aior->len && i < prdtl);
609 ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
611 struct ahci_ioreq *aior;
612 struct blockif_req *breq;
613 struct ahci_prdt_entry *prdt;
614 struct ahci_cmd_hdr *hdr;
617 int err, first, ncq, readop;
619 prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
620 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
625 if (cfis[2] == ATA_WRITE || cfis[2] == ATA_WRITE48 ||
626 cfis[2] == ATA_WRITE_MUL || cfis[2] == ATA_WRITE_MUL48 ||
627 cfis[2] == ATA_WRITE_DMA || cfis[2] == ATA_WRITE_DMA48 ||
628 cfis[2] == ATA_WRITE_FPDMA_QUEUED)
631 if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
632 cfis[2] == ATA_READ_FPDMA_QUEUED) {
633 lba = ((uint64_t)cfis[10] << 40) |
634 ((uint64_t)cfis[9] << 32) |
635 ((uint64_t)cfis[8] << 24) |
636 ((uint64_t)cfis[6] << 16) |
637 ((uint64_t)cfis[5] << 8) |
639 len = cfis[11] << 8 | cfis[3];
643 } else if (cfis[2] == ATA_READ48 || cfis[2] == ATA_WRITE48 ||
644 cfis[2] == ATA_READ_MUL48 || cfis[2] == ATA_WRITE_MUL48 ||
645 cfis[2] == ATA_READ_DMA48 || cfis[2] == ATA_WRITE_DMA48) {
646 lba = ((uint64_t)cfis[10] << 40) |
647 ((uint64_t)cfis[9] << 32) |
648 ((uint64_t)cfis[8] << 24) |
649 ((uint64_t)cfis[6] << 16) |
650 ((uint64_t)cfis[5] << 8) |
652 len = cfis[13] << 8 | cfis[12];
656 lba = ((cfis[7] & 0xf) << 24) | (cfis[6] << 16) |
657 (cfis[5] << 8) | cfis[4];
662 lba *= blockif_sectsz(p->bctx);
663 len *= blockif_sectsz(p->bctx);
665 /* Pull request off free list */
666 aior = STAILQ_FIRST(&p->iofhd);
667 assert(aior != NULL);
668 STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
674 breq = &aior->io_req;
675 breq->br_offset = lba + done;
676 ahci_build_iov(p, aior, prdt, hdr->prdtl);
678 /* Mark this command in-flight. */
679 p->pending |= 1 << slot;
681 /* Stuff request onto busy list. */
682 TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
685 ahci_write_fis_d2h_ncq(p, slot);
688 err = blockif_read(p->bctx, breq);
690 err = blockif_write(p->bctx, breq);
695 ahci_handle_flush(struct ahci_port *p, int slot, uint8_t *cfis)
697 struct ahci_ioreq *aior;
698 struct blockif_req *breq;
702 * Pull request off free list
704 aior = STAILQ_FIRST(&p->iofhd);
705 assert(aior != NULL);
706 STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
712 breq = &aior->io_req;
715 * Mark this command in-flight.
717 p->pending |= 1 << slot;
720 * Stuff request onto busy list
722 TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
724 err = blockif_flush(p->bctx, breq);
729 read_prdt(struct ahci_port *p, int slot, uint8_t *cfis,
732 struct ahci_cmd_hdr *hdr;
733 struct ahci_prdt_entry *prdt;
737 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
740 prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
741 for (i = 0; i < hdr->prdtl && len; i++) {
746 dbcsz = (prdt->dbc & DBCMASK) + 1;
747 ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
748 sublen = len < dbcsz ? len : dbcsz;
749 memcpy(to, ptr, sublen);
757 ahci_handle_dsm_trim(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
759 struct ahci_ioreq *aior;
760 struct blockif_req *breq;
768 if (cfis[2] == ATA_DATA_SET_MANAGEMENT) {
769 len = (uint16_t)cfis[13] << 8 | cfis[12];
772 } else { /* ATA_SEND_FPDMA_QUEUED */
773 len = (uint16_t)cfis[11] << 8 | cfis[3];
777 read_prdt(p, slot, cfis, buf, sizeof(buf));
781 elba = ((uint64_t)entry[5] << 40) |
782 ((uint64_t)entry[4] << 32) |
783 ((uint64_t)entry[3] << 24) |
784 ((uint64_t)entry[2] << 16) |
785 ((uint64_t)entry[1] << 8) |
787 elen = (uint16_t)entry[7] << 8 | entry[6];
793 ahci_write_fis_d2h_ncq(p, slot);
794 ahci_write_fis_sdb(p, slot, cfis,
795 ATA_S_READY | ATA_S_DSC);
797 ahci_write_fis_d2h(p, slot, cfis,
798 ATA_S_READY | ATA_S_DSC);
800 p->pending &= ~(1 << slot);
801 ahci_check_stopped(p);
810 * Pull request off free list
812 aior = STAILQ_FIRST(&p->iofhd);
813 assert(aior != NULL);
814 STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
819 aior->more = (len != done);
821 breq = &aior->io_req;
822 breq->br_offset = elba * blockif_sectsz(p->bctx);
823 breq->br_resid = elen * blockif_sectsz(p->bctx);
826 * Mark this command in-flight.
828 p->pending |= 1 << slot;
831 * Stuff request onto busy list
833 TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
836 ahci_write_fis_d2h_ncq(p, slot);
838 err = blockif_delete(p->bctx, breq);
843 write_prdt(struct ahci_port *p, int slot, uint8_t *cfis,
846 struct ahci_cmd_hdr *hdr;
847 struct ahci_prdt_entry *prdt;
851 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
854 prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
855 for (i = 0; i < hdr->prdtl && len; i++) {
860 dbcsz = (prdt->dbc & DBCMASK) + 1;
861 ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
862 sublen = len < dbcsz ? len : dbcsz;
863 memcpy(ptr, from, sublen);
868 hdr->prdbc = size - len;
872 ahci_checksum(uint8_t *buf, int size)
877 for (i = 0; i < size - 1; i++)
879 buf[size - 1] = 0x100 - sum;
883 ahci_handle_read_log(struct ahci_port *p, int slot, uint8_t *cfis)
885 struct ahci_cmd_hdr *hdr;
888 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
889 if (p->atapi || hdr->prdtl == 0 || cfis[4] != 0x10 ||
890 cfis[5] != 0 || cfis[9] != 0 || cfis[12] != 1 || cfis[13] != 0) {
891 ahci_write_fis_d2h(p, slot, cfis,
892 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
896 memset(buf, 0, sizeof(buf));
897 memcpy(buf, p->err_cfis, sizeof(p->err_cfis));
898 ahci_checksum(buf, sizeof(buf));
900 if (cfis[2] == ATA_READ_LOG_EXT)
901 ahci_write_fis_piosetup(p);
902 write_prdt(p, slot, cfis, (void *)buf, sizeof(buf));
903 ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
907 handle_identify(struct ahci_port *p, int slot, uint8_t *cfis)
909 struct ahci_cmd_hdr *hdr;
911 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
912 if (p->atapi || hdr->prdtl == 0) {
913 ahci_write_fis_d2h(p, slot, cfis,
914 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
918 int sectsz, psectsz, psectoff, candelete, ro;
922 ro = blockif_is_ro(p->bctx);
923 candelete = blockif_candelete(p->bctx);
924 sectsz = blockif_sectsz(p->bctx);
925 sectors = blockif_size(p->bctx) / sectsz;
926 blockif_chs(p->bctx, &cyl, &heads, &sech);
927 blockif_psectsz(p->bctx, &psectsz, &psectoff);
928 memset(buf, 0, sizeof(buf));
933 ata_string((uint8_t *)(buf+10), p->ident, 20);
934 ata_string((uint8_t *)(buf+23), "001", 8);
935 ata_string((uint8_t *)(buf+27), "BHYVE SATA DISK", 40);
936 buf[47] = (0x8000 | 128);
938 buf[49] = (1 << 8 | 1 << 9 | 1 << 11);
940 buf[53] = (1 << 1 | 1 << 2);
942 buf[59] = (0x100 | p->mult_sectors);
943 if (sectors <= 0x0fffffff) {
945 buf[61] = (sectors >> 16);
951 if (p->xfermode & ATA_WDMA0)
952 buf[63] |= (1 << ((p->xfermode & 7) + 8));
960 buf[76] = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3 |
962 buf[77] = (ATA_SUPPORT_RCVSND_FPDMA_QUEUED |
963 (p->ssts & ATA_SS_SPD_MASK) >> 3);
966 buf[82] = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE|
967 ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
968 buf[83] = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
969 ATA_SUPPORT_FLUSHCACHE48 | 1 << 14);
971 buf[85] = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE|
972 ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
973 buf[86] = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
974 ATA_SUPPORT_FLUSHCACHE48 | 1 << 15);
977 if (p->xfermode & ATA_UDMA0)
978 buf[88] |= (1 << ((p->xfermode & 7) + 8));
980 buf[101] = (sectors >> 16);
981 buf[102] = (sectors >> 32);
982 buf[103] = (sectors >> 48);
983 if (candelete && !ro) {
984 buf[69] |= ATA_SUPPORT_RZAT | ATA_SUPPORT_DRAT;
986 buf[169] = ATA_SUPPORT_DSM_TRIM;
990 if (psectsz > sectsz) {
992 buf[106] |= ffsl(psectsz / sectsz) - 1;
993 buf[209] |= (psectoff / sectsz);
997 buf[117] = sectsz / 2;
998 buf[118] = ((sectsz / 2) >> 16);
1000 buf[119] = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
1001 buf[120] = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
1004 ahci_checksum((uint8_t *)buf, sizeof(buf));
1005 ahci_write_fis_piosetup(p);
1006 write_prdt(p, slot, cfis, (void *)buf, sizeof(buf));
1007 ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
1012 handle_atapi_identify(struct ahci_port *p, int slot, uint8_t *cfis)
1015 ahci_write_fis_d2h(p, slot, cfis,
1016 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1020 memset(buf, 0, sizeof(buf));
1021 buf[0] = (2 << 14 | 5 << 8 | 1 << 7 | 2 << 5);
1022 ata_string((uint8_t *)(buf+10), p->ident, 20);
1023 ata_string((uint8_t *)(buf+23), "001", 8);
1024 ata_string((uint8_t *)(buf+27), "BHYVE SATA DVD ROM", 40);
1025 buf[49] = (1 << 9 | 1 << 8);
1026 buf[50] = (1 << 14 | 1);
1027 buf[53] = (1 << 2 | 1 << 1);
1030 if (p->xfermode & ATA_WDMA0)
1031 buf[63] |= (1 << ((p->xfermode & 7) + 8));
1037 buf[76] = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3);
1038 buf[77] = ((p->ssts & ATA_SS_SPD_MASK) >> 3);
1041 buf[82] = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
1042 ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
1043 buf[83] = (1 << 14);
1044 buf[84] = (1 << 14);
1045 buf[85] = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
1046 ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
1047 buf[87] = (1 << 14);
1049 if (p->xfermode & ATA_UDMA0)
1050 buf[88] |= (1 << ((p->xfermode & 7) + 8));
1053 ahci_checksum((uint8_t *)buf, sizeof(buf));
1054 ahci_write_fis_piosetup(p);
1055 write_prdt(p, slot, cfis, (void *)buf, sizeof(buf));
1056 ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
1061 atapi_inquiry(struct ahci_port *p, int slot, uint8_t *cfis)
1070 if (acmd[1] & 1) { /* VPD */
1071 if (acmd[2] == 0) { /* Supported VPD pages */
1079 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1081 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1082 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1083 ahci_write_fis_d2h(p, slot, cfis, tfd);
1095 atapi_string(buf + 8, "BHYVE", 8);
1096 atapi_string(buf + 16, "BHYVE DVD-ROM", 16);
1097 atapi_string(buf + 32, "001", 4);
1103 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1104 write_prdt(p, slot, cfis, buf, len);
1105 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1109 atapi_read_capacity(struct ahci_port *p, int slot, uint8_t *cfis)
1114 sectors = blockif_size(p->bctx) / 2048;
1115 be32enc(buf, sectors - 1);
1116 be32enc(buf + 4, 2048);
1117 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1118 write_prdt(p, slot, cfis, buf, sizeof(buf));
1119 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1123 atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis)
1131 len = be16dec(acmd + 7);
1132 format = acmd[9] >> 6;
1138 uint8_t start_track, buf[20], *bp;
1140 msf = (acmd[1] >> 1) & 1;
1141 start_track = acmd[6];
1142 if (start_track > 1 && start_track != 0xaa) {
1144 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1146 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1147 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1148 ahci_write_fis_d2h(p, slot, cfis, tfd);
1154 if (start_track <= 1) {
1174 sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1178 lba_to_msf(bp, sectors);
1181 be32enc(bp, sectors);
1185 be16enc(buf, size - 2);
1188 write_prdt(p, slot, cfis, buf, len);
1189 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1190 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1197 memset(buf, 0, sizeof(buf));
1201 if (len > sizeof(buf))
1203 write_prdt(p, slot, cfis, buf, len);
1204 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1205 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1212 uint8_t start_track, *bp, buf[50];
1214 msf = (acmd[1] >> 1) & 1;
1215 start_track = acmd[6];
1251 sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1255 lba_to_msf(bp, sectors);
1258 be32enc(bp, sectors);
1281 be16enc(buf, size - 2);
1284 write_prdt(p, slot, cfis, buf, len);
1285 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1286 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1293 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1295 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1296 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1297 ahci_write_fis_d2h(p, slot, cfis, tfd);
1304 atapi_report_luns(struct ahci_port *p, int slot, uint8_t *cfis)
1308 memset(buf, 0, sizeof(buf));
1311 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1312 write_prdt(p, slot, cfis, buf, sizeof(buf));
1313 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1317 atapi_read(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
1319 struct ahci_ioreq *aior;
1320 struct ahci_cmd_hdr *hdr;
1321 struct ahci_prdt_entry *prdt;
1322 struct blockif_req *breq;
1323 struct pci_ahci_softc *sc;
1331 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1332 prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1334 lba = be32dec(acmd + 2);
1335 if (acmd[0] == READ_10)
1336 len = be16dec(acmd + 7);
1338 len = be32dec(acmd + 6);
1340 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1341 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1347 * Pull request off free list
1349 aior = STAILQ_FIRST(&p->iofhd);
1350 assert(aior != NULL);
1351 STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
1356 breq = &aior->io_req;
1357 breq->br_offset = lba + done;
1358 ahci_build_iov(p, aior, prdt, hdr->prdtl);
1360 /* Mark this command in-flight. */
1361 p->pending |= 1 << slot;
1363 /* Stuff request onto busy list. */
1364 TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
1366 err = blockif_read(p->bctx, breq);
1371 atapi_request_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1379 if (len > sizeof(buf))
1381 memset(buf, 0, len);
1382 buf[0] = 0x70 | (1 << 7);
1383 buf[2] = p->sense_key;
1386 write_prdt(p, slot, cfis, buf, len);
1387 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1388 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1392 atapi_start_stop_unit(struct ahci_port *p, int slot, uint8_t *cfis)
1394 uint8_t *acmd = cfis + 0x40;
1397 switch (acmd[4] & 3) {
1401 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1402 tfd = ATA_S_READY | ATA_S_DSC;
1405 /* TODO eject media */
1406 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1407 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1409 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1412 ahci_write_fis_d2h(p, slot, cfis, tfd);
1416 atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1424 len = be16dec(acmd + 7);
1426 code = acmd[2] & 0x3f;
1431 case MODEPAGE_RW_ERROR_RECOVERY:
1435 if (len > sizeof(buf))
1438 memset(buf, 0, sizeof(buf));
1439 be16enc(buf, 16 - 2);
1444 write_prdt(p, slot, cfis, buf, len);
1445 tfd = ATA_S_READY | ATA_S_DSC;
1448 case MODEPAGE_CD_CAPABILITIES:
1452 if (len > sizeof(buf))
1455 memset(buf, 0, sizeof(buf));
1456 be16enc(buf, 30 - 2);
1462 be16enc(&buf[18], 2);
1463 be16enc(&buf[20], 512);
1464 write_prdt(p, slot, cfis, buf, len);
1465 tfd = ATA_S_READY | ATA_S_DSC;
1474 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1476 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1481 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1483 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1486 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1487 ahci_write_fis_d2h(p, slot, cfis, tfd);
1491 atapi_get_event_status_notification(struct ahci_port *p, int slot,
1499 /* we don't support asynchronous operation */
1500 if (!(acmd[1] & 1)) {
1501 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1503 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1508 len = be16dec(acmd + 7);
1509 if (len > sizeof(buf))
1512 memset(buf, 0, sizeof(buf));
1513 be16enc(buf, 8 - 2);
1517 write_prdt(p, slot, cfis, buf, len);
1518 tfd = ATA_S_READY | ATA_S_DSC;
1520 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1521 ahci_write_fis_d2h(p, slot, cfis, tfd);
1525 handle_packet_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1535 for (i = 0; i < 16; i++)
1536 DPRINTF("%02x ", acmd[i]);
1542 case TEST_UNIT_READY:
1543 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1544 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1547 atapi_inquiry(p, slot, cfis);
1550 atapi_read_capacity(p, slot, cfis);
1554 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1555 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1558 atapi_read_toc(p, slot, cfis);
1561 atapi_report_luns(p, slot, cfis);
1565 atapi_read(p, slot, cfis, 0);
1568 atapi_request_sense(p, slot, cfis);
1570 case START_STOP_UNIT:
1571 atapi_start_stop_unit(p, slot, cfis);
1574 atapi_mode_sense(p, slot, cfis);
1576 case GET_EVENT_STATUS_NOTIFICATION:
1577 atapi_get_event_status_notification(p, slot, cfis);
1580 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1581 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1583 ahci_write_fis_d2h(p, slot, cfis, (p->sense_key << 12) |
1584 ATA_S_READY | ATA_S_ERROR);
1590 ahci_handle_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1593 p->tfd |= ATA_S_BUSY;
1595 case ATA_ATA_IDENTIFY:
1596 handle_identify(p, slot, cfis);
1598 case ATA_SETFEATURES:
1601 case ATA_SF_ENAB_SATA_SF:
1603 case ATA_SATA_SF_AN:
1604 p->tfd = ATA_S_DSC | ATA_S_READY;
1607 p->tfd = ATA_S_ERROR | ATA_S_READY;
1608 p->tfd |= (ATA_ERROR_ABORT << 8);
1612 case ATA_SF_ENAB_WCACHE:
1613 case ATA_SF_DIS_WCACHE:
1614 case ATA_SF_ENAB_RCACHE:
1615 case ATA_SF_DIS_RCACHE:
1616 p->tfd = ATA_S_DSC | ATA_S_READY;
1618 case ATA_SF_SETXFER:
1620 switch (cfis[12] & 0xf8) {
1626 p->xfermode = (cfis[12] & 0x7);
1629 p->tfd = ATA_S_DSC | ATA_S_READY;
1633 p->tfd = ATA_S_ERROR | ATA_S_READY;
1634 p->tfd |= (ATA_ERROR_ABORT << 8);
1637 ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1641 if (cfis[12] != 0 &&
1642 (cfis[12] > 128 || (cfis[12] & (cfis[12] - 1)))) {
1643 p->tfd = ATA_S_ERROR | ATA_S_READY;
1644 p->tfd |= (ATA_ERROR_ABORT << 8);
1646 p->mult_sectors = cfis[12];
1647 p->tfd = ATA_S_DSC | ATA_S_READY;
1649 ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1657 case ATA_READ_MUL48:
1658 case ATA_WRITE_MUL48:
1661 case ATA_READ_DMA48:
1662 case ATA_WRITE_DMA48:
1663 case ATA_READ_FPDMA_QUEUED:
1664 case ATA_WRITE_FPDMA_QUEUED:
1665 ahci_handle_rw(p, slot, cfis, 0);
1667 case ATA_FLUSHCACHE:
1668 case ATA_FLUSHCACHE48:
1669 ahci_handle_flush(p, slot, cfis);
1671 case ATA_DATA_SET_MANAGEMENT:
1672 if (cfis[11] == 0 && cfis[3] == ATA_DSM_TRIM &&
1673 cfis[13] == 0 && cfis[12] == 1) {
1674 ahci_handle_dsm_trim(p, slot, cfis, 0);
1677 ahci_write_fis_d2h(p, slot, cfis,
1678 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1680 case ATA_SEND_FPDMA_QUEUED:
1681 if ((cfis[13] & 0x1f) == ATA_SFPDMA_DSM &&
1682 cfis[17] == 0 && cfis[16] == ATA_DSM_TRIM &&
1683 cfis[11] == 0 && cfis[3] == 1) {
1684 ahci_handle_dsm_trim(p, slot, cfis, 0);
1687 ahci_write_fis_d2h(p, slot, cfis,
1688 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1690 case ATA_READ_LOG_EXT:
1691 case ATA_READ_LOG_DMA_EXT:
1692 ahci_handle_read_log(p, slot, cfis);
1694 case ATA_SECURITY_FREEZE_LOCK:
1697 ahci_write_fis_d2h(p, slot, cfis,
1698 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1700 case ATA_CHECK_POWER_MODE:
1701 cfis[12] = 0xff; /* always on */
1702 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1704 case ATA_STANDBY_CMD:
1705 case ATA_STANDBY_IMMEDIATE:
1707 case ATA_IDLE_IMMEDIATE:
1709 case ATA_READ_VERIFY:
1710 case ATA_READ_VERIFY48:
1711 ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1713 case ATA_ATAPI_IDENTIFY:
1714 handle_atapi_identify(p, slot, cfis);
1716 case ATA_PACKET_CMD:
1718 ahci_write_fis_d2h(p, slot, cfis,
1719 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1721 handle_packet_cmd(p, slot, cfis);
1724 WPRINTF("Unsupported cmd:%02x\n", cfis[2]);
1725 ahci_write_fis_d2h(p, slot, cfis,
1726 (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1732 ahci_handle_slot(struct ahci_port *p, int slot)
1734 struct ahci_cmd_hdr *hdr;
1736 struct ahci_prdt_entry *prdt;
1738 struct pci_ahci_softc *sc;
1745 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1747 cfl = (hdr->flags & 0x1f) * 4;
1749 cfis = paddr_guest2host(ahci_ctx(sc), hdr->ctba,
1750 0x80 + hdr->prdtl * sizeof(struct ahci_prdt_entry));
1752 prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1755 for (i = 0; i < cfl; i++) {
1758 DPRINTF("%02x ", cfis[i]);
1762 for (i = 0; i < hdr->prdtl; i++) {
1763 DPRINTF("%d@%08"PRIx64"\n", prdt->dbc & 0x3fffff, prdt->dba);
1768 if (cfis[0] != FIS_TYPE_REGH2D) {
1769 WPRINTF("Not a H2D FIS:%02x\n", cfis[0]);
1773 if (cfis[1] & 0x80) {
1774 ahci_handle_cmd(p, slot, cfis);
1776 if (cfis[15] & (1 << 2))
1778 else if (p->reset) {
1782 p->ci &= ~(1 << slot);
1787 ahci_handle_port(struct ahci_port *p)
1790 if (!(p->cmd & AHCI_P_CMD_ST))
1794 * Search for any new commands to issue ignoring those that
1795 * are already in-flight. Stop if device is busy or in error.
1797 for (; (p->ci & ~p->pending) != 0; p->ccs = ((p->ccs + 1) & 31)) {
1798 if ((p->tfd & (ATA_S_BUSY | ATA_S_DRQ)) != 0)
1800 if (p->waitforclear)
1802 if ((p->ci & ~p->pending & (1 << p->ccs)) != 0) {
1803 p->cmd &= ~AHCI_P_CMD_CCS_MASK;
1804 p->cmd |= p->ccs << AHCI_P_CMD_CCS_SHIFT;
1805 ahci_handle_slot(p, p->ccs);
1811 * blockif callback routine - this runs in the context of the blockif
1812 * i/o thread, so the mutex needs to be acquired.
1815 ata_ioreq_cb(struct blockif_req *br, int err)
1817 struct ahci_cmd_hdr *hdr;
1818 struct ahci_ioreq *aior;
1819 struct ahci_port *p;
1820 struct pci_ahci_softc *sc;
1825 DPRINTF("%s %d\n", __func__, err);
1828 aior = br->br_param;
1833 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1835 if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
1836 cfis[2] == ATA_READ_FPDMA_QUEUED ||
1837 cfis[2] == ATA_SEND_FPDMA_QUEUED)
1839 if (cfis[2] == ATA_DATA_SET_MANAGEMENT ||
1840 (cfis[2] == ATA_SEND_FPDMA_QUEUED &&
1841 (cfis[13] & 0x1f) == ATA_SFPDMA_DSM))
1844 pthread_mutex_lock(&sc->mtx);
1847 * Delete the blockif request from the busy list
1849 TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1852 * Move the blockif request back to the free list
1854 STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1857 hdr->prdbc = aior->done;
1859 if (!err && aior->more) {
1861 ahci_handle_dsm_trim(p, slot, cfis, aior->done);
1863 ahci_handle_rw(p, slot, cfis, aior->done);
1868 tfd = ATA_S_READY | ATA_S_DSC;
1870 tfd = (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR;
1872 ahci_write_fis_sdb(p, slot, cfis, tfd);
1874 ahci_write_fis_d2h(p, slot, cfis, tfd);
1877 * This command is now complete.
1879 p->pending &= ~(1 << slot);
1881 ahci_check_stopped(p);
1882 ahci_handle_port(p);
1884 pthread_mutex_unlock(&sc->mtx);
1885 DPRINTF("%s exit\n", __func__);
1889 atapi_ioreq_cb(struct blockif_req *br, int err)
1891 struct ahci_cmd_hdr *hdr;
1892 struct ahci_ioreq *aior;
1893 struct ahci_port *p;
1894 struct pci_ahci_softc *sc;
1899 DPRINTF("%s %d\n", __func__, err);
1901 aior = br->br_param;
1906 hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + aior->slot * AHCI_CL_SIZE);
1908 pthread_mutex_lock(&sc->mtx);
1911 * Delete the blockif request from the busy list
1913 TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1916 * Move the blockif request back to the free list
1918 STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1921 hdr->prdbc = aior->done;
1923 if (!err && aior->more) {
1924 atapi_read(p, slot, cfis, aior->done);
1929 tfd = ATA_S_READY | ATA_S_DSC;
1931 p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1933 tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1935 cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1936 ahci_write_fis_d2h(p, slot, cfis, tfd);
1939 * This command is now complete.
1941 p->pending &= ~(1 << slot);
1943 ahci_check_stopped(p);
1944 ahci_handle_port(p);
1946 pthread_mutex_unlock(&sc->mtx);
1947 DPRINTF("%s exit\n", __func__);
1951 pci_ahci_ioreq_init(struct ahci_port *pr)
1953 struct ahci_ioreq *vr;
1956 pr->ioqsz = blockif_queuesz(pr->bctx);
1957 pr->ioreq = calloc(pr->ioqsz, sizeof(struct ahci_ioreq));
1958 STAILQ_INIT(&pr->iofhd);
1961 * Add all i/o request entries to the free queue
1963 for (i = 0; i < pr->ioqsz; i++) {
1967 vr->io_req.br_callback = ata_ioreq_cb;
1969 vr->io_req.br_callback = atapi_ioreq_cb;
1970 vr->io_req.br_param = vr;
1971 STAILQ_INSERT_TAIL(&pr->iofhd, vr, io_flist);
1974 TAILQ_INIT(&pr->iobhd);
1978 pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
1980 int port = (offset - AHCI_OFFSET) / AHCI_STEP;
1981 offset = (offset - AHCI_OFFSET) % AHCI_STEP;
1982 struct ahci_port *p = &sc->port[port];
1984 DPRINTF("pci_ahci_port %d: write offset 0x%"PRIx64" value 0x%"PRIx64"\n",
1985 port, offset, value);
2004 p->ie = value & 0xFDC000FF;
2005 ahci_generate_intr(sc);
2009 p->cmd &= ~(AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
2010 AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
2011 AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
2012 AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK);
2013 p->cmd |= (AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
2014 AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
2015 AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
2016 AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK) & value;
2018 if (!(value & AHCI_P_CMD_ST)) {
2023 p->cmd |= AHCI_P_CMD_CR;
2024 clb = (uint64_t)p->clbu << 32 | p->clb;
2025 p->cmd_lst = paddr_guest2host(ahci_ctx(sc), clb,
2026 AHCI_CL_SIZE * AHCI_MAX_SLOTS);
2029 if (value & AHCI_P_CMD_FRE) {
2032 p->cmd |= AHCI_P_CMD_FR;
2033 fb = (uint64_t)p->fbu << 32 | p->fb;
2034 /* we don't support FBSCP, so rfis size is 256Bytes */
2035 p->rfis = paddr_guest2host(ahci_ctx(sc), fb, 256);
2037 p->cmd &= ~AHCI_P_CMD_FR;
2040 if (value & AHCI_P_CMD_CLO) {
2041 p->tfd &= ~(ATA_S_BUSY | ATA_S_DRQ);
2042 p->cmd &= ~AHCI_P_CMD_CLO;
2045 if (value & AHCI_P_CMD_ICC_MASK) {
2046 p->cmd &= ~AHCI_P_CMD_ICC_MASK;
2049 ahci_handle_port(p);
2055 WPRINTF("pci_ahci_port: read only registers 0x%"PRIx64"\n", offset);
2059 if (!(p->cmd & AHCI_P_CMD_ST)) {
2060 if (value & ATA_SC_DET_RESET)
2072 ahci_handle_port(p);
2082 pci_ahci_host_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
2084 DPRINTF("pci_ahci_host: write offset 0x%"PRIx64" value 0x%"PRIx64"\n",
2092 DPRINTF("pci_ahci_host: read only registers 0x%"PRIx64"\n", offset);
2095 if (value & AHCI_GHC_HR)
2097 else if (value & AHCI_GHC_IE) {
2098 sc->ghc |= AHCI_GHC_IE;
2099 ahci_generate_intr(sc);
2104 ahci_generate_intr(sc);
2112 pci_ahci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
2113 int baridx, uint64_t offset, int size, uint64_t value)
2115 struct pci_ahci_softc *sc = pi->pi_arg;
2117 assert(baridx == 5);
2118 assert((offset % 4) == 0 && size == 4);
2120 pthread_mutex_lock(&sc->mtx);
2122 if (offset < AHCI_OFFSET)
2123 pci_ahci_host_write(sc, offset, value);
2124 else if (offset < AHCI_OFFSET + sc->ports * AHCI_STEP)
2125 pci_ahci_port_write(sc, offset, value);
2127 WPRINTF("pci_ahci: unknown i/o write offset 0x%"PRIx64"\n", offset);
2129 pthread_mutex_unlock(&sc->mtx);
2133 pci_ahci_host_read(struct pci_ahci_softc *sc, uint64_t offset)
2149 uint32_t *p = &sc->cap;
2150 p += (offset - AHCI_CAP) / sizeof(uint32_t);
2158 DPRINTF("pci_ahci_host: read offset 0x%"PRIx64" value 0x%x\n",
2165 pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset)
2168 int port = (offset - AHCI_OFFSET) / AHCI_STEP;
2169 offset = (offset - AHCI_OFFSET) % AHCI_STEP;
2189 uint32_t *p= &sc->port[port].clb;
2190 p += (offset - AHCI_P_CLB) / sizeof(uint32_t);
2199 DPRINTF("pci_ahci_port %d: read offset 0x%"PRIx64" value 0x%x\n",
2200 port, offset, value);
2206 pci_ahci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
2207 uint64_t regoff, int size)
2209 struct pci_ahci_softc *sc = pi->pi_arg;
2213 assert(baridx == 5);
2214 assert(size == 1 || size == 2 || size == 4);
2215 assert((regoff & (size - 1)) == 0);
2217 pthread_mutex_lock(&sc->mtx);
2219 offset = regoff & ~0x3; /* round down to a multiple of 4 bytes */
2220 if (offset < AHCI_OFFSET)
2221 value = pci_ahci_host_read(sc, offset);
2222 else if (offset < AHCI_OFFSET + sc->ports * AHCI_STEP)
2223 value = pci_ahci_port_read(sc, offset);
2226 WPRINTF("pci_ahci: unknown i/o read offset 0x%"PRIx64"\n",
2229 value >>= 8 * (regoff & 0x3);
2231 pthread_mutex_unlock(&sc->mtx);
2237 pci_ahci_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts, int atapi)
2239 char bident[sizeof("XX:X:X")];
2240 struct blockif_ctxt *bctxt;
2241 struct pci_ahci_softc *sc;
2249 fprintf(stderr, "pci_ahci: backing device required\n");
2254 dbg = fopen("/tmp/log", "w+");
2257 sc = calloc(1, sizeof(struct pci_ahci_softc));
2260 sc->ports = MAX_PORTS;
2263 * Only use port 0 for a backing device. All other ports will be
2266 sc->port[0].atapi = atapi;
2269 * Attempt to open the backing image. Use the PCI
2270 * slot/func for the identifier string.
2272 snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func);
2273 bctxt = blockif_open(opts, bident);
2274 if (bctxt == NULL) {
2278 sc->port[0].bctx = bctxt;
2279 sc->port[0].pr_sc = sc;
2282 * Create an identifier for the backing file. Use parts of the
2283 * md5 sum of the filename
2286 MD5Update(&mdctx, opts, strlen(opts));
2287 MD5Final(digest, &mdctx);
2288 sprintf(sc->port[0].ident, "BHYVE-%02X%02X-%02X%02X-%02X%02X",
2289 digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]);
2292 * Allocate blockif request structures and add them
2295 pci_ahci_ioreq_init(&sc->port[0]);
2297 pthread_mutex_init(&sc->mtx, NULL);
2299 /* Intel ICH8 AHCI */
2300 slots = sc->port[0].ioqsz;
2304 sc->cap = AHCI_CAP_64BIT | AHCI_CAP_SNCQ | AHCI_CAP_SSNTF |
2305 AHCI_CAP_SMPS | AHCI_CAP_SSS | AHCI_CAP_SALP |
2306 AHCI_CAP_SAL | AHCI_CAP_SCLO | (0x3 << AHCI_CAP_ISS_SHIFT)|
2307 AHCI_CAP_PMD | AHCI_CAP_SSC | AHCI_CAP_PSC |
2308 (slots << AHCI_CAP_NCS_SHIFT) | AHCI_CAP_SXS | (sc->ports - 1);
2310 /* Only port 0 implemented */
2313 sc->cap2 = AHCI_CAP2_APST;
2316 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x2821);
2317 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x8086);
2318 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
2319 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_STORAGE_SATA);
2320 pci_set_cfgdata8(pi, PCIR_PROGIF, PCIP_STORAGE_SATA_AHCI_1_0);
2321 pci_emul_add_msicap(pi, 1);
2322 pci_emul_alloc_bar(pi, 5, PCIBAR_MEM32,
2323 AHCI_OFFSET + sc->ports * AHCI_STEP);
2325 pci_lintr_request(pi);
2329 if (sc->port[0].bctx != NULL)
2330 blockif_close(sc->port[0].bctx);
2338 pci_ahci_hd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
2341 return (pci_ahci_init(ctx, pi, opts, 0));
2345 pci_ahci_atapi_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
2348 return (pci_ahci_init(ctx, pi, opts, 1));
2352 * Use separate emulation names to distinguish drive and atapi devices
2354 struct pci_devemu pci_de_ahci_hd = {
2355 .pe_emu = "ahci-hd",
2356 .pe_init = pci_ahci_hd_init,
2357 .pe_barwrite = pci_ahci_write,
2358 .pe_barread = pci_ahci_read
2360 PCI_EMUL_SET(pci_de_ahci_hd);
2362 struct pci_devemu pci_de_ahci_cd = {
2363 .pe_emu = "ahci-cd",
2364 .pe_init = pci_ahci_atapi_init,
2365 .pe_barwrite = pci_ahci_write,
2366 .pe_barread = pci_ahci_read
2368 PCI_EMUL_SET(pci_de_ahci_cd);