Quellcode durchsuchen

9.085u-mods: drunk, i'll not fix it

rain vor 5 Jahren
Ursprung
Commit
cd273bd3ff
8 geänderte Dateien mit 254 neuen und 271 gelöschten Zeilen
  1. 1 0
      LINES
  2. 3 0
      README.md
  3. 13 13
      include/arch/x86/paging.h
  4. 1 0
      include/kernel/mm.h
  5. 105 151
      include/modules/ahci.h
  6. 4 0
      kernel/irq.c
  7. 28 1
      kernel/mm.c
  8. 99 106
      modules/ahci.c

+ 1 - 0
LINES

@@ -38,4 +38,5 @@
 2021-8-15 8.413: 8413 lines, 57 files, rain
 2021-8-17 8.990: 8990 lines, 59 files, rain
 2021-8-18 9.106: 9106 lines, 59 files, rain
+2021-8-19 9.085: 9085 lines, 59 files, rain
 

+ 3 - 0
README.md

@@ -155,3 +155,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;get the time from cmos, and make it to unix time-stamp. 
 &nbsp;&nbsp;&nbsp;&Nbsp;and this version looks work. 
 
+9.085u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working... drunk, i'll not fix it. 
+

+ 13 - 13
include/arch/x86/paging.h

@@ -27,33 +27,33 @@
 #define _PAGING_H
 
 
-#define PAGE_PRESENT  0x0001
+#define PAGE_PRESENT  	0x0001
 
-#define PAGE_RDWR     0x0002
+#define PAGE_RDWR     	0x0002
 
-#define PAGE_USER     0x0004
+#define PAGE_USER     	0x0004
 
-#define PAGE_NWRBACK  0x0008
+#define PAGE_WTHROUGH	0x0008
 
-#define PAGE_NOCACHE  0x0010
+#define PAGE_CACHEDIS 	0x0010
 
-#define PAGE_ACCESSED 0x0020
+#define PAGE_ACCESSED 	0x0020
 
-#define PAGE_DIRTY    0x0040
+#define PAGE_DIRTY    	0x0040
 
-#define PAGE_PAT      0x0080
+#define PAGE_PAT      	0x0080
 
-#define PAGE_GLOBAL   0x0100
+#define PAGE_GLOBAL   	0x0100
 
 
 /* 0x83 */
-#define PATT_KERNEL (PAGE_PRESENT | PAGE_RDWR | PAGE_PAT)
+#define PATT_KERNEL 	(PAGE_PRESENT | PAGE_RDWR | PAGE_PAT)
 
 /* 0x07 */
-#define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER | PAGE_PAT)
+#define PATT_USER 	(PAGE_PRESENT | PAGE_RDWR | PAGE_USER | PAGE_PAT)
 
-#define PATT_DEVMM  (PAGE_PRESENT | PAGE_RDWR | PAGE_PAT | PAGE_NOCACHE)
+#define PATT_DEVMM 	(PAGE_PRESENT | PAGE_RDWR | PAGE_PAT | PAGE_WTHROUGH)
 
-#define PAGE_TABLE_BASE (0x0000000000000000)
+#define PAGE_TABLE_BASE 0
 
 #endif

+ 1 - 0
include/kernel/mm.h

@@ -141,6 +141,7 @@ extern struct mempool devpool;
 void *kmalloc(struct mempool *, __uint64_t);
 void kfree(struct mempool *, void *, __uint64_t);
 void *mapping(void *, __uint64_t);
+void *mm_vphy(void *);
 
 
 /* put memory info */

+ 105 - 151
include/modules/ahci.h

@@ -28,214 +28,168 @@
 #include <kernel/types.h>
 
 
-struct ahci_hba_portctl {
-	__uint32_t cmd_listl;
-	__uint32_t cmd_listh;
+/* i tried to keep this as simple as i can... */
 
-	__uint32_t fisl;
-	__uint32_t fish;
+/* pci-configure.bar5 points to this, and this points to another things */
 
-	__uint32_t int_status;
-	__uint32_t int_enable;
-
-	__uint32_t cmd;
-	__uint32_t rsv0;
-	__uint32_t tfd;
-
-#define SIG_ATA 	0x00000101
-#define SIG_ATAPI 	0xeb140101
-#define SIG_SEMB 	0xc33c0101
-#define SIG_PORTMUL 	0x96690101
+/* 
+ * PCI-config.bar5		-> 	AHCI HBA mem
+ * AHCI HBA mem 		-> 	AHCI HBA ports (max 32)
+ * AHCI_HBA ports 		-> 	command list (32 command headers)
+ * command list [32]		-> 	command headers 
+ * command headers		-> 	command table 
+ * command table 		-> 	prdts
+ * prdts [65536] (optional)	-> 	data buffer
+ * command table 		-> 	fis 
+ * fis				-> 	lba addresses and others
+ * */
 
-	__uint32_t sig;
 
-	__uint32_t status;
-	__uint32_t ctl;
-	__uint32_t error;
+struct ahci_hba_port {
+	__uint64_t cmd_list;
 
-	__uint32_t active;
+	/* received fis base */
+	__uint64_t rfis_base;
 
-	__uint32_t ci;
-	__uint32_t sntf;
-	__uint32_t fbs;
-	__uint32_t rsv1[11];
-	__uint32_t vendor[4];
-} __attribute__ ((packed));
+	__uint32_t int_status;
+	__uint32_t int_enable;
 
+	__uint32_t cmd;
 
-/* the pcihdr.bar5 points to this */
+	__uint32_t rsvd0[6];
 
-struct ahci_hba {
-	/* general host control */
+	/* these look like bitmaps */
+	__uint32_t sata_active;
+	__uint32_t cmd_issue;
 
-	__uint32_t cap0;
-	__uint32_t ghc;
-	__uint32_t int_status;
-	__uint32_t implted_ports;
-	__uint32_t version;
-	__uint32_t ccc_ctl, ccc_pts;
-	__uint32_t em_loc, em_ctl;
-	__uint32_t cap1;
-	__uint32_t bohc;
+	__uint32_t rsvd[17];
+} __attribute__ ((packed));
 
-	char rsvd[0x74];
 
-	/* 1-31 is optional */
-	struct ahci_hba_portctl portctl[32];
-} __attribute__ ((packed));
+struct ahci_hba_mem {
 
-extern volatile struct ahci_hba *hba;
+#define HBA_MEM_CAP_MASK 	0x1f
 
-#define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
+	__uint32_t cap;
 
+#define HBA_MEM_GHC_INT_ENABLE 	0x00000002
+#define HBA_MEM_GHC_AHCI_ENABLE	0x80000000
 
-#define FIS_HOSTTODEV	0x27
-#define FIS_DEVTOHOST 	0x34
-#define FIS_DMA_ACT 	0x39
-#define FIS_DMA_SETUP 	0x41
-#define FIS_DATA 	0x46
-#define FIS_BIST 	0x58
-#define FIS_PIO_SETUP 	0x5f
-#define FIS_DEV_BITS 	0xa1
+	__uint32_t global_ctl;
+	__uint32_t int_status;
 
-#define DEV_NODEV 	0x00
-#define DEV_SATA 	0x01
-#define DEV_ATAPI 	0x02
-#define DEV_PORTMUL 	0x03
-#define DEV_SEMB 	0x04
+	/* ports implemented */
+	__uint32_t ports_impled;
 
+	__uint32_t rsvd0[6];
 
-struct fis_h2d {
-	__uint8_t fis_type;
-	__uint8_t pmport : 4;
-	__uint8_t rsvd0 : 3;
-	/* cmd / ctrl */
-	__uint8_t cmd : 1;
-	__uint8_t command;
-	__uint8_t feature_low;
+	/* ? */
 
-	__uint8_t lba0, lba1, lba2;
-	__uint8_t dev;
+#define HBA_MEM_BHO_OWN_BIOS 	1
+#define HBA_MEM_BHO_OWN_OS	2
+#define HBA_MEM_BHO_OWN_CHANGED	8
+#define HBA_MEM_BHO_OWN_RWC 	8
 
-	__uint8_t lba3, lba4, lba5;
-	__uint8_t feature_high;
+	__uint32_t bios_handoff;
 
-	__uint8_t count_low;
-	__uint8_t count_high;
-	__uint8_t icc;
-	__uint8_t ctl;
+	__uint32_t rsvd1[53];
 
-	__uint32_t rsvd1;
+	struct ahci_hba_port ports[32];
 } __attribute__ ((packed));
 
-struct fis_d2h {
-	__uint8_t fis_type;
 
-	__uint8_t pmport : 4;
-	__uint8_t rsvd0 : 2;
-	__uint8_t irq : 1;
-	__uint8_t rsvd1 : 1;
+struct ahci_cmd_header {
 
-	__uint8_t status;
-	__uint8_t error;
+#define CMDHDR_CFG_CFISLEN_SHIFT 	0
+#define CMDHDR_CFG_CFISLEN_MASK 	((1 << 5) - 1)
 
-	__uint8_t lba0, lba1, lba2;
-	__uint8_t dev;
+	/* the mask for config */
+#define CMDHDR_CFG_ATAPI		0x20
+#define CMDHDR_CFG_WRITE 		0x40
 
-	__uint8_t lba3, lba4, lba5;
-	__uint8_t rsvd2;
+#define CMDHDR_CFG_PREFETCHABLE 	0x80
 
-	__uint8_t count_low, count_high;
-	__uint16_t rsvd3;
+#define CMDHDR_CFG_RESET 		0x100
+#define CMDHDR_CFG_BIST 		0x200
+#define CMDHDR_CFG_CLEAR_R_OK 		0x400
 
-	__uint32_t rsvd4;
-} __attribute__ ((packed));
+#define CMDHDR_CFG_PORTML_MASK 		0xf000
+#define CMDHDR_CFG_PORTML_SHIFT 	12
 
-struct fis_data {
-	__uint8_t fis_type;
-	__uint8_t pmport : 4;
-	__uint8_t rsvd0 : 4;
-
-	__uint16_t rsvd1;
+#define CMDHDR_CFG_PRDTLEN_MASK 	0xffff0000
+#define CMDHDR_CFG_PRDTLEN_SHIFT 	16
 
-	__uint32_t data[0];
-} __attribute__ ((packed));
+#define CMDHDR_CFG_INTR			0x800000000
 
+	/* config word and prdt length (word) */
+	__uint32_t config;
+	/* total data length we transferred */
+	__uint32_t transfered;
+	__uint64_t cmd_table;
 
-/* the command slot (in the command list) */
+	__uint32_t rsvd[4];
+} __attribute__((packed));
 
-struct cmd_header {
-	/* fis length in __uint32_t */
-	__uint8_t fis_len : 5;
-	__uint8_t atapi : 1;
-	__uint8_t wr : 1;
-	__uint8_t pre : 1;
+struct ahci_prdt_entry {
+	__uint64_t buf;
+	__uint32_t rsvd;
 
-	__uint8_t rst : 1;
-	__uint8_t bist : 1;
-	__uint8_t clear : 1;
-	__uint8_t rsvd0 : 1;
-	__uint8_t pmport : 4;
+	/* size (of this.buf) */
+#define PRDT_SIZE_MASK 		((1 << 22) - 1)
 
-	/* prdt length (in entries) */
-	__uint16_t prdt_len;
+	/* interrupt on completion */
+#define PRDT_SIZE_INTR 		(0x80000000u)
+	__uint32_t size;
+} __attribute__ ((packed));
 
-	__uint32_t bytes_transm;
+struct ahci_cmd_table {
+	__uint8_t cmd_fis[64];
+	__uint8_t atapi_cmd[16];
 
-	__uint32_t cmd_table_low;
-	__uint32_t cmd_table_high;
+	__uint32_t rsvd[0x0c];
 
-	__uint32_t rsvd1[4];
+	/* max 65536 */
+	/* TODO: support more prdt-entries */
+	struct ahci_prdt_entry prdts[1];
 } __attribute__ ((packed));
 
+struct fis_h2d {
+	__uint8_t fis_type;
 
-struct prdt_entry {
-	__uint32_t buf_low;
-	__uint32_t buf_high;
-
-	__uint32_t rsvd0;
+#define FISH2D_PORTMUL_MASK 	15
+#define FISH2D_PORTMUL_SHIFT 	0
 
-	__uint32_t nbytes : 22;
-	__uint32_t rsvd1 : 9;
-	__uint32_t irq : 1;
-} __attribute__ ((packed));
+#define FISH2D_CMD 		0x80
 
+	__uint8_t config;
 
-/* this is points from the cmd_header */
+	__uint8_t command;
 
-struct cmd_table {
-	__uint8_t cmd_fis[64];
-	__uint8_t atapi_cmd[16];
-	__uint8_t rsvd0[48];
+	__uint8_t lba0, lba1, lba2;
+	__uint8_t dev;
 
-	/* max 65536 */
-	struct prdt_entry prdts[65536];
-} __attribute__ ((packed));
+	__uint8_t lba3, lba4, lba5;
+	__uint8_t feature0;
 
+	__uint16_t nsectors;
+	__uint8_t icc;
 
-void do_ahci_intr(void);
-void set_base(volatile struct ahci_hba_portctl *, int);
-void ahci_read(volatile struct ahci_hba_portctl *, __uint64_t, __uint16_t, char *);
+	__uint8_t control;
 
-__uint64_t ahci_buffer;
+	__uint32_t rsvd;
+} __attribute__ ((packed));
 
+#define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
 
-#define HBA_PORT_CMD_ST 	0x0001
-#define HBA_PORT_CMD_FRE 	0x0010
-#define HBA_PORT_CMD_FR 	0x4000
-#define HBA_PORT_CMD_CR 	0x8000
+#define ATACMD_ID 	0xec
 
 #define ATACMD_RD 	0x20
-#define ATACMD_RD48		0x24
+#define ATACMD_RD48 	0x24
 
 #define ATACMD_WR 	0x30
-#define ATACMD_WR48	0x34
+#define ATACMD_WR48 	0x34
 
-#define ATACMD_RDDMA 	0xc8
-#define ATACMD_RDDMA48 	0x25
-
-#define ATACMD_WRDMA 	0xca
-#define ATACMD_WRDMA48 	0x35
-
-#define ATACMD_ID	0xec
+extern struct ahci_hba_mem *hba;
 
+int ahci_rw_sect(struct ahci_hba_port *, int, char *, __uint64_t);
+void do_ahci_intr(void);

+ 4 - 0
kernel/irq.c

@@ -51,6 +51,10 @@ void register_irq(int nr, void *entry, const void *handler) {
 	}
 
 	else {
+		/* enable slave-irq */
+		mask = inb(0x21) & ~(1 << 2);
+		outb(mask, 0x21);
+
 		mask = inb(0xa1) & ~(1 << (nr-8));
 		outb(mask, 0xa1);
 	}

+ 28 - 1
kernel/mm.c

@@ -356,9 +356,36 @@ void trace_mm(void) {
 
 	for (i = 0; i < 512; i++) {
 		if (pt[i] & ~0xff) 
-			printk(yellow, "%q -> %q: %b\n", i << PAGE_SHIFT, pt[i] & ~0xff, pt[i] & 0xff);
+			printk(yellow, "%q -> %q: %b\n", i << PAGE_SHIFT, 
+					pt[i] & ~0xff, pt[i] & 0xff);
 	}
 		
 
 	return;
 }
+
+
+/* 
+ * get the physical address of virtual address 'p'
+ * the `mapping()` got the vaddress of physical, and we will 
+ * get the physical (find in the page-table). 
+ * */
+
+void *mm_vphy(void *p) {
+	/* like mapping(), we just search in the first 1g */
+
+	__uint64_t addr = (__uint64_t) p & PAGE_MASK;
+
+#define PT_ADDR ((__uint64_t *) 0x2000)
+
+	__uint64_t i;
+
+	for (i = 0; i < 512; i++) 
+		if ((PT_ADDR[i] & ~0xff) == addr) 
+			return (void *) ((i << PAGE_SHIFT) + ((__uint64_t) p - addr));
+
+	return NULL;
+
+#undef PT_ADDR
+}
+

+ 99 - 106
modules/ahci.c

@@ -39,166 +39,159 @@
 #include <lib/string.h>
 
 
-volatile struct ahci_hba *hba;
-__uint64_t ahci_buffer;
+struct ahci_hba_mem *hba;
+static char buffer[512];
 
-build_irq(0x2a);
 
+build_irq(0x2e);
+build_irq(0x2f);
 
-static char buf[512] = {0};
-
-/* calls from pci_init() */
 
 errno_t ahci_init(struct pci_header0 *hdr) {
-	ahci_info("bar0: %q\n", hdr->bar0);
-	ahci_info("bar1: %q\n", hdr->bar1);
-	ahci_info("bar2: %q\n", hdr->bar2);
-	ahci_info("bar3: %q\n", hdr->bar3);
-	ahci_info("bar4: %q\n", hdr->bar4);
-	ahci_info("bar5: %q\n", hdr->bar5);
+	register_irq(14, irq0x2e_interrupt, do_ahci_intr);
+	register_irq(15, irq0x2f_interrupt, do_ahci_intr);
 
-	ahci_info("irq: %b\n", hdr->irqnr);
+	ahci_info("PCI BAR0: \t%q\n", hdr->bar0);
+	ahci_info("PCI BAR1: \t%q\n", hdr->bar1);
+	ahci_info("PCI BAR2: \t%q\n", hdr->bar2);
+	ahci_info("PCI BAR3: \t%q\n", hdr->bar3);
+	ahci_info("PCI BAR4: \t%q\n", hdr->bar4);
+	ahci_info("PCI BAR5: \t%q\n", hdr->bar5);
 
-	/* map the bar5 to memory */
-	hba = mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
+	hba = (struct ahci_hba_mem *) mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
 
-	ahci_info("hba mem maps to the %q. \n", hba);
+	ahci_info("AHCI HBA MEM maps to %q. \n", hba);
 
-	/* ahci enable, interrupt enable, hba reset */
-	hba->ghc = 0x8000000;
+	ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr);
+	ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK);
 
-	register_irq(0x20 + 0x0a, irq0x2a_interrupt, do_ahci_intr);
+	hba->global_ctl |= HBA_MEM_GHC_AHCI_ENABLE;
 
-	struct cmd_header *cmdhdr = kmalloc(&devpool, sizeof(struct cmd_header));
+	ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
 
-	if (!cmdhdr) 
-		panic("ahci_init: failed to allocate space for ahci command header. \n");
+	if (!(hba->bios_handoff & HBA_MEM_BHO_OWN_OS)) {
+		/* get the ownership */
 
-	hba->portctl[0].cmd_listl = (__uint32_t) ((__uint64_t) cmdhdr) & 0xffffffff;
-	hba->portctl[0].cmd_listh = (__uint32_t) ((__uint64_t) cmdhdr >> 32) & 0xffffffff;
+		hba->bios_handoff = (hba->bios_handoff & ~HBA_MEM_BHO_OWN_RWC) | 
+			HBA_MEM_BHO_OWN_OS;
 
-	ahci_info("current ahci list address: %q. \n", (__uint64_t) hba->portctl[0].cmd_listl);
+		int timeout = 4096;
 
-	struct cmd_table *cmdtbl = alloc_page_att(PATT_DEVMM);
+		while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--);
 
-	((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
-		->cmd_table_low = (__uint32_t) ((__uint64_t) cmdtbl) & 0xffffffff;
+		if (!timeout) {
+			hba->bios_handoff = HBA_MEM_BHO_OWN_OS;
+			hba->bios_handoff |= HBA_MEM_BHO_OWN_CHANGED;
+		}
 
+		ahci_info("got the ownership. \n");
+		ahci_info("current AHCI BIOS-handoff: %d\n", hba->bios_handoff);
+	}
 
-	((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
-		->cmd_table_high = (__uint32_t) ((__uint64_t) cmdtbl >> 32) & 0xffffffff;
+	else 
+		ahci_info("AHCI is already in the OS mode. \n");
 
-	ahci_info("current ahci command table address: %q. \n", 
-			(__uint64_t) ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
-			->cmd_table_low);
+	int i, nrdevs = 0;
 
-	ahci_info("reading the sector 1 from the ahci disk...\n");
+	for (i = 0; i < (hba->cap & HBA_MEM_CAP_MASK); i++) {
+		/* if (bitmap_read(ports_impled, i) == 0) return; */
+		if (!(hba->ports_impled & (1 << i))) 
+			continue;
 
-	ahci_read(hba->portctl, 0x0000000000000001, 1, buf);
+		ahci_info("found ahci device %d on port %d. ", nrdevs++, i);
 
-	return 0;
-}
+		struct ahci_cmd_header *cmdlist = kmalloc(&devpool, 
+				sizeof(struct ahci_cmd_header));
 
+		memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
 
-void do_ahci_intr(void) {
-	ahci_info("irq 0x0e: sata master disk. \n");
+		/* well, that needs a physical address, I think... */
+		hba->ports[i].cmd_list = (__uint64_t) mm_vphy(cmdlist);
 
-	ahci_info("disk data: \n");
+		struct ahci_cmd_table *cmdtable = kmalloc(&devpool, 
+				sizeof(struct ahci_cmd_table));
 
-	int i;
+		memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
 
-	for (i = 0; i < 512 >> 3; i++) 
-		ahci_info("%q", ((__uint64_t *) buf)[i]);
+		cmdlist->cmd_table = (__uint64_t) mm_vphy(cmdlist);
 
-	return;
-}
+	}
 
+	ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0);
 
-static int find_free_cmdslot(volatile struct ahci_hba_portctl *port) {
-	int i;
-	__uint32_t slot = (port->active | port->ci);
+	return 0;
+}
 
-	for (i = 0; i < 32; i++) {
-		if (!(slot & 1)) 
-		       return i;
 
-		slot >>= 1;	
-	}
+int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
+	ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n", 
+			port, lba, buffer, atacmd);
 
-	ahci_info("find_free_cmdslot: warning: can't find a free slot");
+	struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
 
-	return 0;
-}
+	ahci_info("ahci_rw: command list: %q. \n", cmdhdr);
 
+	memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
 
-void ahci_read(volatile struct ahci_hba_portctl *port, __uint64_t lba, __uint16_t nsectors, char *buf) {
-	int slot = find_free_cmdslot(port);
-	
-	ahci_info("ahci_read: free cmd slot: %d. \n", slot);
+	cmdhdr->config = 	(4 << CMDHDR_CFG_CFISLEN_SHIFT) | 
+				CMDHDR_CFG_CLEAR_R_OK | 
+				(0 << CMDHDR_CFG_PORTML_SHIFT) |
+				(1 << CMDHDR_CFG_PRDTLEN_SHIFT) |
+				((atacmd == ATACMD_WR || atacmd == ATACMD_WR48) ? 
+				CMDHDR_CFG_WRITE : 0);
 
-	port->int_status = -1;
+	cmdhdr->transfered = 0;
 
-	struct cmd_header *cmdhdr = (struct cmd_header *) (__uint64_t) port->cmd_listl;
-	ahci_info("ahci_read: command header address: %q. \n", cmdhdr);
+	struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
+	memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
 
-	cmdhdr += slot;
+	cmdhdr->cmd_table = (__uint64_t) mm_vphy(cmdtable);
 
-	cmdhdr->fis_len = sizeof(struct fis_h2d) / sizeof(int);
-	cmdhdr->wr = 0;
-	cmdhdr->atapi = 0;
-	cmdhdr->clear = 1;
-	cmdhdr->pre = 1;
-	/* the max size of a prdt is 8kb (16 sectors, >> 4) */
-	cmdhdr->prdt_len = ((nsectors - 1) >> 4) + 1;
+	struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
 
-	struct cmd_table *cmdtbl = (struct cmd_table *) (__uint64_t) cmdhdr->cmd_table_low;
-	ahci_info("ahci_read: command table address: %q. \n", cmdtbl);
+	memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
 
-	int i;
+	/* FIS_TYPE_H2D_FIS */
+	cmd_fis->fis_type = 0x27;
+	cmd_fis->config = 0 | FISH2D_CMD;
+	cmd_fis->command = atacmd;
 
-	/* setup first 1 --- (last-1), and setup the last one later */
+	cmd_fis->lba0 = lba & 0xff;
+	cmd_fis->lba1 = (lba >> 8) & 0xff;
+	cmd_fis->lba2 = (lba >> 16) & 0xff;
 
-	for (i = 0; i < cmdhdr->prdt_len - 1; i++) {
-		memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
+	cmd_fis->lba3 = (lba >> 24) & 0xff;
+	cmd_fis->lba4 = (lba >> 32) & 0xff;
+	cmd_fis->lba5 = (lba >> 40) & 0xff;
 
-		cmdtbl->prdts[i].buf_low = (__uint32_t) (__uint64_t) buf & 0xffffffff;
-		cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
-		/* max 8kb */
-		cmdtbl->prdts[i].nbytes = 8191;
-		cmdtbl->prdts[i].irq = 1;
+	cmd_fis->dev = 0xe0;
+	cmd_fis->nsectors = 1;
 
-		buf += 8192;
-	}
 
-	memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
+	memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d));
+	cmdtable->prdts[0].buf = (__uint64_t) buffer;
+	cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
 
-	cmdtbl->prdts[i].buf_low = (__uint32_t) ((__uint64_t) buf) & 0xffffffff;
-	cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
-	cmdtbl->prdts[i].nbytes = (nsectors << 9) % 8192 - 1;
-	cmdtbl->prdts[i].irq = 1;
+	port->int_enable = 1 << 5;
+	port->int_status = 1 << 5;
+	port->cmd_issue |= 1;
+	port->cmd |= 1;
 
-	struct fis_h2d *h2dfis = (struct fis_h2d *) &cmdtbl->cmd_fis;
+	/* wait it complete */
 
-	h2dfis->fis_type = FIS_HOSTTODEV;
-	h2dfis->cmd = 1;
-	h2dfis->command = ATACMD_RDDMA;
+	ahci_info("waiting device complete...\n");
 
-	h2dfis->lba0 = lba & 0xff;
-	h2dfis->lba1 = (lba >> 8) & 0xff;
-	h2dfis->lba2 = (lba >> 16) & 0xff;
-	h2dfis->lba3 = (lba >> 24) & 0xff;
-	h2dfis->lba4 = (lba >> 32) & 0xff;
-	h2dfis->lba5 = (lba >> 40) & 0xff;
+	while (!(port->int_status & (1 << 5)));
 
-	h2dfis->count_low = nsectors & 0xff;
-	h2dfis->count_high = (nsectors >> 8) & 0xff;
+	ahci_info("AHCI request completed. \n");
+	port->cmd &= ~1;
+
+	return 0;
+}
 
-	h2dfis->dev = 0xe0;
 
-	port->active = 1;
-	port->cmd |= 0x10000003;
-	port->ci = 1;
+void do_ahci_intr(void) {
+	ahci_info("AHCI interrupt. \n");
 
 	return;
 }
-