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