grml...
[seabios.git] / src / ahci.c
index 1f119bb4fdfaefb80eb69dd78513a8f5804a5c78..9b3ce2f391f56bc4d6e8f282454574e146e01f9b 100644 (file)
@@ -24,7 +24,6 @@
 /****************************************************************
  * these bits must run in both 16bit and 32bit modes
  ****************************************************************/
-u8 *ahci_buf_fl VAR16VISIBLE;
 
 // prepare sata command fis
 static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
@@ -269,7 +268,7 @@ ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
     // Use a word aligned buffer for AHCI I/O
     int rc;
     struct disk_op_s localop = *op;
-    u8 *alignedbuf_fl = GET_GLOBAL(ahci_buf_fl);
+    u8 *alignedbuf_fl = GET_GLOBAL(bounce_buf_fl);
     u8 *position = op->buf_fl;
 
     localop.buf_fl = alignedbuf_fl;
@@ -384,7 +383,7 @@ ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
 static struct ahci_port_s*
 ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
 {
-    struct ahci_port_s *port = malloc_fseg(sizeof(*port));
+    struct ahci_port_s *port = malloc_tmp(sizeof(*port));
 
     if (!port) {
         warn_noalloc();
@@ -408,10 +407,16 @@ ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
     return port;
 }
 
-static void ahci_port_realloc(struct ahci_port_s *port)
+static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
 {
+    struct ahci_port_s *tmp;
     u32 cmd;
 
+    tmp = malloc_fseg(sizeof(*port));
+    *tmp = *port;
+    free(port);
+    port = tmp;
+
     ahci_port_reset(port->ctrl, port->pnr);
 
     free(port->list);
@@ -427,6 +432,8 @@ static void ahci_port_realloc(struct ahci_port_s *port)
     cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
     cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
     ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
+
+    return port;
 }
 
 static void ahci_port_release(struct ahci_port_s *port)
@@ -532,80 +539,78 @@ static int ahci_port_init(struct ahci_port_s *port)
             adjsize >>= 10;
             adjprefix = 'G';
         }
-        char *desc = znprintf(MAXDESCSIZE
+        port->desc = znprintf(MAXDESCSIZE
                               , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
                               , port->pnr
                               , ata_extract_model(model, MAXMODEL, buffer)
                               , ata_extract_version(buffer)
                               , (u32)adjsize, adjprefix);
-        dprintf(1, "%s\n", desc);
-
-        // Register with bcv system.
-        boot_add_hd(&port->drive, desc, -1);
+        port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
     } else {
         // found cdrom (atapi)
         port->drive.blksize = CDROM_SECTOR_SIZE;
         port->drive.sectors = (u64)-1;
         u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
-        char *desc = znprintf(MAXDESCSIZE
-                              , "DVD/CD [AHCI/%d: %s ATAPI-%d %s]"
+        if (!iscd) {
+            dprintf(1, "AHCI/%d: atapi device is'nt a cdrom\n", port->pnr);
+            return -1;
+        }
+        port->desc = znprintf(MAXDESCSIZE
+                              , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
                               , port->pnr
                               , ata_extract_model(model, MAXMODEL, buffer)
-                              , ata_extract_version(buffer)
-                              , (iscd ? "DVD/CD" : "Device"));
-        dprintf(1, "%s\n", desc);
-
-        // fill cdidmap
-        if (iscd)
-            boot_add_cd(&port->drive, desc, -1);
+                              , ata_extract_version(buffer));
+        port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
     }
     return 0;
 }
 
 // Detect any drives attached to a given controller.
 static void
-ahci_detect(void *data)
+ahci_port_detect(void *data)
 {
-    struct ahci_ctrl_s *ctrl = data;
-    struct ahci_port_s *port;
-    u32 pnr, max;
+    struct ahci_port_s *port = data;
     int rc;
 
-    max = ctrl->caps & 0x1f;
-    for (pnr = 0; pnr <= max; pnr++) {
-        if (!(ctrl->ports & (1 << pnr)))
-            continue;
-        dprintf(2, "AHCI/%d: probing\n", pnr);
-        ahci_port_reset(ctrl, pnr);
-        port = ahci_port_alloc(ctrl, pnr);
-        if (port == NULL)
-            continue;
-        rc = ahci_port_init(port);
-        if (rc < 0)
-            ahci_port_release(port);
-        else
-            ahci_port_realloc(port);
+    dprintf(2, "AHCI/%d: probing\n", port->pnr);
+    ahci_port_reset(port->ctrl, port->pnr);
+    rc = ahci_port_init(port);
+    if (rc < 0)
+        ahci_port_release(port);
+    else {
+        port = ahci_port_realloc(port);
+        dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
+        if (!port->atapi) {
+            // Register with bcv system.
+            boot_add_hd(&port->drive, port->desc, port->prio);
+        } else {
+            // fill cdidmap
+            boot_add_cd(&port->drive, port->desc, port->prio);
+        }
     }
 }
 
 // Initialize an ata controller and detect its drives.
 static void
-ahci_init_controller(int bdf)
+ahci_init_controller(struct pci_device *pci)
 {
     struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
-    u32 val;
+    struct ahci_port_s *port;
+    u16 bdf = pci->bdf;
+    u32 val, pnr, max;
 
     if (!ctrl) {
         warn_noalloc();
         return;
     }
 
-    ahci_buf_fl = malloc_low(DISK_SECTOR_SIZE);
-    if (!ahci_buf_fl) {
+    if (bounce_buf_init() < 0) {
         warn_noalloc();
+        free(ctrl);
         return;
     }
 
+    ctrl->pci_tmp = pci;
     ctrl->pci_bdf = bdf;
     ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
     ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
@@ -623,7 +628,15 @@ ahci_init_controller(int bdf)
     dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
             ctrl->caps, ctrl->ports);
 
-    run_thread(ahci_detect, ctrl);
+    max = ctrl->caps & 0x1f;
+    for (pnr = 0; pnr <= max; pnr++) {
+        if (!(ctrl->ports & (1 << pnr)))
+            continue;
+        port = ahci_port_alloc(ctrl, pnr);
+        if (port == NULL)
+            continue;
+        run_thread(ahci_port_detect, port);
+    }
 }
 
 // Locate and init ahci controllers.
@@ -637,7 +650,7 @@ ahci_init(void)
             continue;
         if (pci->prog_if != 1 /* AHCI rev 1 */)
             continue;
-        ahci_init_controller(pci->bdf);
+        ahci_init_controller(pci);
     }
 }