feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer
[lunaix-os.git] / lunaix-os / hal / ahci / ahci.c
index 805976013d18e742adda46aa08e62bb311a2be1a..33dc87e3a05287e859baebcdd06c1f6b4e337aeb 100644 (file)
@@ -31,6 +31,7 @@
 #define HBA_CLB_SIZE 1024
 
 #define HBA_MY_IE (HBA_PxINTR_DHR | HBA_PxINTR_TFE | HBA_PxINTR_OF)
+#define AHCI_DEVCLASS DEVCLASS(DEVIF_PCI, DEVFN_STORAGE, DEV_SATA)
 
 // #define DO_HBA_FULL_RESET
 
@@ -43,6 +44,8 @@ static char sata_ifs[][20] = { "Not detected",
                                "SATA II (3.0Gbps)",
                                "SATA III (6.0Gbps)" };
 
+static struct devclass ahci_class = AHCI_DEVCLASS;
+
 extern void
 ahci_fsexport(struct block_dev* bdev, void* fs_node);
 
@@ -61,9 +64,6 @@ achi_register_ops(struct hba_port* port);
 void
 ahci_register_device(struct hba_device* hbadev);
 
-void*
-ahci_driver_init(struct pci_device* ahci_dev);
-
 void
 __hba_reset_port(hba_reg_t* port_reg)
 {
@@ -80,9 +80,11 @@ __hba_reset_port(hba_reg_t* port_reg)
     port_reg[HBA_RPxSCTL] &= ~0xf;
 }
 
-void*
-ahci_driver_init(struct pci_device* ahci_dev)
+int
+ahci_driver_init(struct device_def* def, struct device* dev)
 {
+    struct pci_device* ahci_dev = container_of(dev, struct pci_device, dev);
+
     struct pci_base_addr* bar6 = &ahci_dev->bar[5];
     assert_msg(bar6->type & BAR_TYPE_MMIO, "AHCI: BAR#6 is not MMIO.");
 
@@ -95,6 +97,7 @@ ahci_driver_init(struct pci_device* ahci_dev)
 
     int iv = isrm_ivexalloc(__ahci_hba_isr);
     pci_setup_msi(ahci_dev, iv);
+    isrm_set_payload(iv, (ptr_t)&ahcis);
 
     struct ahci_driver* ahci_drv = vzalloc(sizeof(*ahci_drv));
     struct ahci_hba* hba = &ahci_drv->hba;
@@ -183,12 +186,12 @@ ahci_driver_init(struct pci_device* ahci_dev)
         port_regs[HBA_RPxCMD] |= HBA_PxCMD_ST;
 
         if (!ahci_init_device(port)) {
-            kprintf(KERROR "init fail: 0x%x@p%d\n", port->regs[HBA_RPxSIG], i);
+            kprintf(KERROR "init fail: 0x%x@p%d", port->regs[HBA_RPxSIG], i);
             continue;
         }
 
         struct hba_device* hbadev = port->device;
-        kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d\n",
+        kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d",
                 i,
                 hbadev->model,
                 hbadev->block_size,
@@ -197,9 +200,9 @@ ahci_driver_init(struct pci_device* ahci_dev)
         ahci_register_device(hbadev);
     }
 
-    return ahci_drv;
+    pci_bind_instance(ahci_dev, ahci_drv);
+    return 0;
 }
-EXPORT_PCI_DEVICE(pci_ahci, AHCI_HBA_CLASS, 0, 0, ahci_driver_init);
 
 void
 ahci_register_device(struct hba_device* hbadev)
@@ -209,6 +212,7 @@ ahci_register_device(struct hba_device* hbadev)
 
     bdev->end_lba = hbadev->max_lba;
     bdev->blk_size = hbadev->block_size;
+    bdev->class = &ahci_class;
 
     block_mount(bdev, ahci_fsexport);
 }
@@ -436,4 +440,13 @@ achi_register_ops(struct hba_port* port)
     } else {
         port->device->ops.submit = scsi_submit;
     }
-}
\ No newline at end of file
+}
+
+static struct pci_device_def ahcidef = {
+    .dev_class = AHCI_HBA_CLASS,
+    .ident_mask = PCI_MATCH_ANY,
+    .devdef = { .class = DEVCLASS(DEVIF_PCI, DEVFN_STORAGE, DEV_SATA),
+                .name = "Serial ATA Controller",
+                .bind = ahci_driver_init }
+};
+EXPORT_PCI_DEVICE(ahci, &ahcidef);
\ No newline at end of file