Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / ahci.c
1 // Low level AHCI disk access
2 //
3 // Copyright (C) 2010 Gerd Hoffmann <kraxel@redhat.com>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "ahci.h" // CDB_CMD_READ_10
8 #include "ata.h" // ATA_CB_STAT
9 #include "biosvar.h" // GET_GLOBAL
10 #include "blockcmd.h" // CDB_CMD_READ_10
11 #include "malloc.h" // free
12 #include "output.h" // dprintf
13 #include "pci.h" // foreachpci
14 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
15 #include "pci_regs.h" // PCI_INTERRUPT_LINE
16 #include "stacks.h" // yield
17 #include "std/disk.h" // DISK_RET_SUCCESS
18 #include "string.h" // memset
19 #include "util.h" // timer_calc
20 #include "x86.h" // inb
21
22 #define AHCI_REQUEST_TIMEOUT 32000 // 32 seconds max for IDE ops
23 #define AHCI_RESET_TIMEOUT     500 // 500 miliseconds
24 #define AHCI_LINK_TIMEOUT       10 // 10 miliseconds
25
26 // prepare sata command fis
27 static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
28 {
29     memset_fl(fis, 0, sizeof(*fis));
30     fis->command = command;
31 }
32
33 static void sata_prep_readwrite(struct sata_cmd_fis *fis,
34                                 struct disk_op_s *op, int iswrite)
35 {
36     u64 lba = op->lba;
37     u8 command;
38
39     memset_fl(fis, 0, sizeof(*fis));
40
41     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
42         fis->sector_count2 = op->count >> 8;
43         fis->lba_low2      = lba >> 24;
44         fis->lba_mid2      = lba >> 32;
45         fis->lba_high2     = lba >> 40;
46         lba &= 0xffffff;
47         command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
48                    : ATA_CMD_READ_DMA_EXT);
49     } else {
50         command = (iswrite ? ATA_CMD_WRITE_DMA
51                    : ATA_CMD_READ_DMA);
52     }
53     fis->feature      = 1; /* dma */
54     fis->command      = command;
55     fis->sector_count = op->count;
56     fis->lba_low      = lba;
57     fis->lba_mid      = lba >> 8;
58     fis->lba_high     = lba >> 16;
59     fis->device       = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
60 }
61
62 static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
63 {
64     memset_fl(fis, 0, sizeof(*fis));
65     fis->command  = ATA_CMD_PACKET;
66     fis->feature  = 1; /* dma */
67     fis->lba_mid  = blocksize;
68     fis->lba_high = blocksize >> 8;
69 }
70
71 // ahci register access helpers
72 static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
73 {
74     u32 addr = ctrl->iobase + reg;
75     return readl((void*)addr);
76 }
77
78 static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
79 {
80     u32 addr = ctrl->iobase + reg;
81     writel((void*)addr, val);
82 }
83
84 static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
85 {
86     u32 ctrl_reg = 0x100;
87     ctrl_reg += pnr * 0x80;
88     ctrl_reg += port_reg;
89     return ctrl_reg;
90 }
91
92 static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
93 {
94     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
95     return ahci_ctrl_readl(ctrl, ctrl_reg);
96 }
97
98 static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
99 {
100     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
101     ahci_ctrl_writel(ctrl, ctrl_reg, val);
102 }
103
104 // submit ahci command + wait for result
105 static int ahci_command(struct ahci_port_s *port_gf, int iswrite, int isatapi,
106                         void *buffer, u32 bsize)
107 {
108     u32 val, status, success, flags, intbits, error;
109     struct ahci_ctrl_s *ctrl = port_gf->ctrl;
110     struct ahci_cmd_s  *cmd  = port_gf->cmd;
111     struct ahci_fis_s  *fis  = port_gf->fis;
112     struct ahci_list_s *list = port_gf->list;
113     u32 pnr                  = port_gf->pnr;
114
115     cmd->fis.reg       = 0x27;
116     cmd->fis.pmp_type  = 1 << 7; /* cmd fis */
117     cmd->prdt[0].base  = (u32)buffer;
118     cmd->prdt[0].baseu = 0;
119     cmd->prdt[0].flags = bsize-1;
120
121     flags = ((1 << 16) | /* one prd entry */
122              (iswrite ? (1 << 6) : 0) |
123              (isatapi ? (1 << 5) : 0) |
124              (5 << 0)); /* fis length (dwords) */
125     list[0].flags  = flags;
126     list[0].bytes  = 0;
127     list[0].base   = (u32)(cmd);
128     list[0].baseu  = 0;
129
130     dprintf(8, "AHCI/%d: send cmd ...\n", pnr);
131     intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
132     if (intbits)
133         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
134     ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
135     ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
136
137     u32 end = timer_calc(AHCI_REQUEST_TIMEOUT);
138     do {
139         for (;;) {
140             intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
141             if (intbits) {
142                 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
143                 if (intbits & 0x02) {
144                     status = GET_LOWFLAT(fis->psfis[2]);
145                     error  = GET_LOWFLAT(fis->psfis[3]);
146                     break;
147                 }
148                 if (intbits & 0x01) {
149                     status = GET_LOWFLAT(fis->rfis[2]);
150                     error  = GET_LOWFLAT(fis->rfis[3]);
151                     break;
152                 }
153             }
154             if (timer_check(end)) {
155                 warn_timeout();
156                 return -1;
157             }
158             yield();
159         }
160         dprintf(8, "AHCI/%d: ... intbits 0x%x, status 0x%x ...\n",
161                 pnr, intbits, status);
162     } while (status & ATA_CB_STAT_BSY);
163
164     success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
165                                   ATA_CB_STAT_ERR)) &&
166                ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
167     if (success) {
168         dprintf(8, "AHCI/%d: ... finished, status 0x%x, OK\n", pnr,
169                 status);
170     } else {
171         dprintf(2, "AHCI/%d: ... finished, status 0x%x, ERROR 0x%x\n", pnr,
172                 status, error);
173
174         // non-queued error recovery (AHCI 1.3 section 6.2.2.1)
175         // Clears PxCMD.ST to 0 to reset the PxCI register
176         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
177         ahci_port_writel(ctrl, pnr, PORT_CMD, val & ~PORT_CMD_START);
178
179         // waits for PxCMD.CR to clear to 0
180         while (1) {
181             val = ahci_port_readl(ctrl, pnr, PORT_CMD);
182             if ((val & PORT_CMD_LIST_ON) == 0)
183                 break;
184             yield();
185         }
186
187         // Clears any error bits in PxSERR to enable capturing new errors
188         val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
189         ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
190
191         // Clears status bits in PxIS as appropriate
192         val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
193         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
194
195         // If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to 1, issue
196         // a COMRESET to the device to put it in an idle state
197         val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
198         if (val & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ)) {
199             dprintf(2, "AHCI/%d: issue comreset\n", pnr);
200             val = ahci_port_readl(ctrl, pnr, PORT_SCR_CTL);
201             // set Device Detection Initialization (DET) to 1 for 1 ms for comreset
202             ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val | 1);
203             mdelay (1);
204             ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val);
205         }
206
207         // Sets PxCMD.ST to 1 to enable issuing new commands
208         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
209         ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
210     }
211     return success ? 0 : -1;
212 }
213
214 #define CDROM_CDB_SIZE 12
215
216 int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
217 {
218     if (! CONFIG_AHCI)
219         return 0;
220
221     struct ahci_port_s *port_gf = container_of(
222         op->drive_gf, struct ahci_port_s, drive);
223     struct ahci_cmd_s *cmd = port_gf->cmd;
224     u8 *atapi = cdbcmd;
225     int i, rc;
226
227     sata_prep_atapi(&cmd->fis, blocksize);
228     for (i = 0; i < CDROM_CDB_SIZE; i++) {
229         cmd->atapi[i] = atapi[i];
230     }
231     rc = ahci_command(port_gf, 0, 1, op->buf_fl,
232                       op->count * blocksize);
233     if (rc < 0)
234         return DISK_RET_EBADTRACK;
235     return DISK_RET_SUCCESS;
236 }
237
238 // read/write count blocks from a harddrive, op->buf_fl must be word aligned
239 static int
240 ahci_disk_readwrite_aligned(struct disk_op_s *op, int iswrite)
241 {
242     struct ahci_port_s *port_gf = container_of(
243         op->drive_gf, struct ahci_port_s, drive);
244     struct ahci_cmd_s *cmd = port_gf->cmd;
245     int rc;
246
247     sata_prep_readwrite(&cmd->fis, op, iswrite);
248     rc = ahci_command(port_gf, iswrite, 0, op->buf_fl,
249                       op->count * DISK_SECTOR_SIZE);
250     dprintf(8, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
251             iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
252     if (rc < 0)
253         return DISK_RET_EBADTRACK;
254     return DISK_RET_SUCCESS;
255 }
256
257 // read/write count blocks from a harddrive.
258 static int
259 ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
260 {
261     // if caller's buffer is word aligned, use it directly
262     if (((u32) op->buf_fl & 1) == 0)
263         return ahci_disk_readwrite_aligned(op, iswrite);
264
265     // Use a word aligned buffer for AHCI I/O
266     int rc;
267     struct disk_op_s localop = *op;
268     u8 *alignedbuf_fl = bounce_buf_fl;
269     u8 *position = op->buf_fl;
270
271     localop.buf_fl = alignedbuf_fl;
272     localop.count = 1;
273
274     if (iswrite) {
275         u16 block;
276         for (block = 0; block < op->count; block++) {
277             memcpy_fl (alignedbuf_fl, position, DISK_SECTOR_SIZE);
278             rc = ahci_disk_readwrite_aligned (&localop, 1);
279             if (rc)
280                 return rc;
281             position += DISK_SECTOR_SIZE;
282             localop.lba++;
283         }
284     } else { // read
285         u16 block;
286         for (block = 0; block < op->count; block++) {
287             rc = ahci_disk_readwrite_aligned (&localop, 0);
288             if (rc)
289                 return rc;
290             memcpy_fl (position, alignedbuf_fl, DISK_SECTOR_SIZE);
291             position += DISK_SECTOR_SIZE;
292             localop.lba++;
293         }
294     }
295     return DISK_RET_SUCCESS;
296 }
297
298 // command demuxer
299 int VISIBLE32FLAT
300 process_ahci_op(struct disk_op_s *op)
301 {
302     if (!CONFIG_AHCI)
303         return 0;
304     switch (op->command) {
305     case CMD_READ:
306         return ahci_disk_readwrite(op, 0);
307     case CMD_WRITE:
308         return ahci_disk_readwrite(op, 1);
309     case CMD_FORMAT:
310     case CMD_RESET:
311     case CMD_ISREADY:
312     case CMD_VERIFY:
313     case CMD_SEEK:
314         return DISK_RET_SUCCESS;
315     default:
316         dprintf(1, "AHCI: unknown disk command %d\n", op->command);
317         return DISK_RET_EPARAM;
318     }
319 }
320
321 static void
322 ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
323 {
324     u32 val;
325
326     /* disable FIS + CMD */
327     u32 end = timer_calc(AHCI_RESET_TIMEOUT);
328     for (;;) {
329         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
330         if (!(val & (PORT_CMD_FIS_RX | PORT_CMD_START |
331                      PORT_CMD_FIS_ON | PORT_CMD_LIST_ON)))
332             break;
333         val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
334         ahci_port_writel(ctrl, pnr, PORT_CMD, val);
335         if (timer_check(end)) {
336             warn_timeout();
337             break;
338         }
339         yield();
340     }
341
342     /* disable + clear IRQs */
343     ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, 0);
344     val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
345     if (val)
346         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
347 }
348
349 static struct ahci_port_s*
350 ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
351 {
352     struct ahci_port_s *port = malloc_tmp(sizeof(*port));
353
354     if (!port) {
355         warn_noalloc();
356         return NULL;
357     }
358     port->pnr = pnr;
359     port->ctrl = ctrl;
360     port->list = memalign_tmp(1024, 1024);
361     port->fis = memalign_tmp(256, 256);
362     port->cmd = memalign_tmp(256, 256);
363     if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
364         warn_noalloc();
365         return NULL;
366     }
367     memset(port->list, 0, 1024);
368     memset(port->fis, 0, 256);
369     memset(port->cmd, 0, 256);
370
371     ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
372     ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
373     return port;
374 }
375
376 static void ahci_port_release(struct ahci_port_s *port)
377 {
378     ahci_port_reset(port->ctrl, port->pnr);
379     free(port->list);
380     free(port->fis);
381     free(port->cmd);
382     free(port);
383 }
384
385 static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
386 {
387     struct ahci_port_s *tmp;
388     u32 cmd;
389
390     tmp = malloc_fseg(sizeof(*port));
391     if (!tmp) {
392         warn_noalloc();
393         ahci_port_release(port);
394         return NULL;
395     }
396     *tmp = *port;
397     free(port);
398     port = tmp;
399
400     ahci_port_reset(port->ctrl, port->pnr);
401
402     free(port->list);
403     free(port->fis);
404     free(port->cmd);
405     port->list = memalign_high(1024, 1024);
406     port->fis = memalign_high(256, 256);
407     port->cmd = memalign_high(256, 256);
408
409     ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
410     ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
411
412     cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
413     cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
414     ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
415
416     return port;
417 }
418
419 #define MAXMODEL 40
420
421 /* See ahci spec chapter 10.1 "Software Initialization of HBA" */
422 static int ahci_port_setup(struct ahci_port_s *port)
423 {
424     struct ahci_ctrl_s *ctrl = port->ctrl;
425     u32 pnr = port->pnr;
426     char model[MAXMODEL+1];
427     u16 buffer[256];
428     u32 cmd, stat, err, tf;
429     int rc;
430
431     /* enable FIS recv */
432     cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
433     cmd |= PORT_CMD_FIS_RX;
434     ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
435
436     /* spin up */
437     cmd |= PORT_CMD_SPIN_UP;
438     ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
439     u32 end = timer_calc(AHCI_LINK_TIMEOUT);
440     for (;;) {
441         stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
442         if ((stat & 0x07) == 0x03) {
443             dprintf(2, "AHCI/%d: link up\n", port->pnr);
444             break;
445         }
446         if (timer_check(end)) {
447             dprintf(2, "AHCI/%d: link down\n", port->pnr);
448             return -1;
449         }
450         yield();
451     }
452
453     /* clear error status */
454     err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
455     if (err)
456         ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
457
458     /* wait for device becoming ready */
459     end = timer_calc(AHCI_REQUEST_TIMEOUT);
460     for (;;) {
461         tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
462         if (!(tf & (ATA_CB_STAT_BSY |
463                     ATA_CB_STAT_DRQ)))
464             break;
465         if (timer_check(end)) {
466             warn_timeout();
467             dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
468             return -1;
469         }
470         yield();
471     }
472
473     /* start device */
474     cmd |= PORT_CMD_START;
475     ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
476
477     sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
478     rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
479     if (rc == 0) {
480         port->atapi = 1;
481     } else {
482         port->atapi = 0;
483         sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
484         rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
485         if (rc < 0)
486             return -1;
487     }
488
489     port->drive.cntl_id = pnr;
490     port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
491
492     if (!port->atapi) {
493         // found disk (ata)
494         port->drive.type = DTYPE_AHCI;
495         port->drive.blksize = DISK_SECTOR_SIZE;
496         port->drive.pchs.cylinder = buffer[1];
497         port->drive.pchs.head = buffer[3];
498         port->drive.pchs.sector = buffer[6];
499
500         u64 sectors;
501         if (buffer[83] & (1 << 10)) // word 83 - lba48 support
502             sectors = *(u64*)&buffer[100]; // word 100-103
503         else
504             sectors = *(u32*)&buffer[60]; // word 60 and word 61
505         port->drive.sectors = sectors;
506         u64 adjsize = sectors >> 11;
507         char adjprefix = 'M';
508         if (adjsize >= (1 << 16)) {
509             adjsize >>= 10;
510             adjprefix = 'G';
511         }
512         port->desc = znprintf(MAXDESCSIZE
513                               , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
514                               , port->pnr
515                               , ata_extract_model(model, MAXMODEL, buffer)
516                               , ata_extract_version(buffer)
517                               , (u32)adjsize, adjprefix);
518         port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
519     } else {
520         // found cdrom (atapi)
521         port->drive.type = DTYPE_AHCI_ATAPI;
522         port->drive.blksize = CDROM_SECTOR_SIZE;
523         port->drive.sectors = (u64)-1;
524         u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
525         if (!iscd) {
526             dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
527             return -1;
528         }
529         port->desc = znprintf(MAXDESCSIZE
530                               , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
531                               , port->pnr
532                               , ata_extract_model(model, MAXMODEL, buffer)
533                               , ata_extract_version(buffer));
534         port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
535     }
536     return 0;
537 }
538
539 // Detect any drives attached to a given controller.
540 static void
541 ahci_port_detect(void *data)
542 {
543     struct ahci_port_s *port = data;
544     int rc;
545
546     dprintf(2, "AHCI/%d: probing\n", port->pnr);
547     ahci_port_reset(port->ctrl, port->pnr);
548     rc = ahci_port_setup(port);
549     if (rc < 0)
550         ahci_port_release(port);
551     else {
552         port = ahci_port_realloc(port);
553         if (port == NULL)
554             return;
555         dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
556         if (!port->atapi) {
557             // Register with bcv system.
558             boot_add_hd(&port->drive, port->desc, port->prio);
559         } else {
560             // fill cdidmap
561             boot_add_cd(&port->drive, port->desc, port->prio);
562         }
563     }
564 }
565
566 // Initialize an ata controller and detect its drives.
567 static void
568 ahci_controller_setup(struct pci_device *pci)
569 {
570     struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
571     struct ahci_port_s *port;
572     u16 bdf = pci->bdf;
573     u32 val, pnr, max;
574
575     if (!ctrl) {
576         warn_noalloc();
577         return;
578     }
579
580     if (create_bounce_buf() < 0) {
581         warn_noalloc();
582         free(ctrl);
583         return;
584     }
585
586     ctrl->pci_tmp = pci;
587     ctrl->pci_bdf = bdf;
588     ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
589     ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
590     dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
591             bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
592
593     pci_config_maskw(bdf, PCI_COMMAND, 0,
594                      PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
595
596     val = ahci_ctrl_readl(ctrl, HOST_CTL);
597     ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
598
599     ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
600     ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
601     dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
602             ctrl->caps, ctrl->ports);
603
604     max = 0x1f;
605     for (pnr = 0; pnr <= max; pnr++) {
606         if (!(ctrl->ports & (1 << pnr)))
607             continue;
608         port = ahci_port_alloc(ctrl, pnr);
609         if (port == NULL)
610             continue;
611         run_thread(ahci_port_detect, port);
612     }
613 }
614
615 // Locate and init ahci controllers.
616 static void
617 ahci_scan(void)
618 {
619     // Scan PCI bus for ATA adapters
620     struct pci_device *pci;
621     foreachpci(pci) {
622         if (pci->class != PCI_CLASS_STORAGE_SATA)
623             continue;
624         if (pci->prog_if != 1 /* AHCI rev 1 */)
625             continue;
626         ahci_controller_setup(pci);
627     }
628 }
629
630 void
631 ahci_setup(void)
632 {
633     ASSERT32FLAT();
634     if (!CONFIG_AHCI)
635         return;
636
637     dprintf(3, "init ahci\n");
638     ahci_scan();
639 }