1 // Low level ATA disk access
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "ata.h" // ATA_CB_STAT
9 #include "biosvar.h" // GET_GLOBALFLAT
10 #include "block.h" // struct drive_s
11 #include "blockcmd.h" // CDB_CMD_READ_10
12 #include "byteorder.h" // be16_to_cpu
13 #include "malloc.h" // malloc_fseg
14 #include "output.h" // dprintf
15 #include "pci.h" // foreachpci
16 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
17 #include "pci_regs.h" // PCI_INTERRUPT_LINE
18 #include "pic.h" // enable_hwirq
19 #include "stacks.h" // yield
20 #include "std/disk.h" // DISK_RET_SUCCESS
21 #include "string.h" // memset
22 #include "util.h" // timer_calc
23 #include "x86.h" // inb
25 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
28 /****************************************************************
30 ****************************************************************/
32 // Wait for the specified ide state
34 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
36 u32 end = timer_calc(timeout);
38 u8 status = inb(base+ATA_CB_STAT);
39 if ((status & mask) == flags)
41 if (timer_check(end)) {
49 // Wait for the device to be not-busy.
51 await_not_bsy(u16 base)
53 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
56 // Wait for the device to be ready.
60 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
63 // Wait for ide state - pauses for one ata cycle first.
65 pause_await_not_bsy(u16 iobase1, u16 iobase2)
67 // Wait one PIO transfer cycle.
68 inb(iobase2 + ATA_CB_ASTAT);
70 return await_not_bsy(iobase1);
73 // Wait for ide state - pause for 400ns first.
75 ndelay_await_not_bsy(u16 iobase1)
78 return await_not_bsy(iobase1);
83 ata_reset(struct atadrive_s *adrive_gf)
85 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
86 u8 slave = GET_GLOBALFLAT(adrive_gf->slave);
87 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
88 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
90 dprintf(6, "ata_reset drive=%p\n", &adrive_gf->drive);
92 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
94 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
97 // wait for device to become not busy.
98 int status = await_not_bsy(iobase1);
103 u32 end = timer_calc(IDE_TIMEOUT);
105 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
106 status = ndelay_await_not_bsy(iobase1);
109 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
111 // Change drive request failed to take effect - retry.
112 if (timer_check(end)) {
118 // QEMU doesn't reset dh on reset, so set it explicitly.
119 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
122 // On a user-reset request, wait for RDY if it is an ATA device.
123 u8 type=GET_GLOBALFLAT(adrive_gf->drive.type);
124 if (type == DTYPE_ATA)
125 status = await_rdy(iobase1);
129 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
131 dprintf(6, "ata_reset exit status=%x\n", status);
134 // Check for drive RDY for 16bit interface command.
136 isready(struct atadrive_s *adrive_gf)
138 // Read the status from controller
139 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
140 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
141 u8 status = inb(iobase1 + ATA_CB_STAT);
142 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
143 return DISK_RET_SUCCESS;
144 return DISK_RET_ENOTREADY;
148 /****************************************************************
150 ****************************************************************/
152 struct ata_pio_command {
168 // Send an ata command to the drive.
170 send_cmd(struct atadrive_s *adrive_gf, struct ata_pio_command *cmd)
172 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
173 u8 slave = GET_GLOBALFLAT(adrive_gf->slave);
174 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
177 int status = await_not_bsy(iobase1);
180 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
181 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
182 u8 olddh = inb(iobase1 + ATA_CB_DH);
183 outb(newdh, iobase1 + ATA_CB_DH);
184 if ((olddh ^ newdh) & (1<<4)) {
185 // Was a device change - wait for device to become not busy.
186 status = ndelay_await_not_bsy(iobase1);
191 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
192 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
193 outb(cmd->feature2, iobase1 + ATA_CB_FR);
194 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
195 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
196 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
197 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
199 outb(cmd->feature, iobase1 + ATA_CB_FR);
200 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
201 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
202 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
203 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
204 outb(cmd->command, iobase1 + ATA_CB_CMD);
209 // Wait for data after calling 'send_cmd'.
211 ata_wait_data(u16 iobase1)
213 int status = ndelay_await_not_bsy(iobase1);
217 if (status & ATA_CB_STAT_ERR) {
218 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
219 , status, inb(iobase1 + ATA_CB_ERR));
222 if (!(status & ATA_CB_STAT_DRQ)) {
223 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
230 // Send an ata command that does not transfer any further data.
232 ata_cmd_nondata(struct atadrive_s *adrive_gf, struct ata_pio_command *cmd)
234 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
235 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
236 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
238 // Disable interrupts
239 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
241 int ret = send_cmd(adrive_gf, cmd);
244 ret = ndelay_await_not_bsy(iobase1);
248 if (ret & ATA_CB_STAT_ERR) {
249 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
250 , ret, inb(iobase1 + ATA_CB_ERR));
254 if (ret & ATA_CB_STAT_DRQ) {
255 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
262 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
268 /****************************************************************
270 ****************************************************************/
272 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
275 ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
277 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
278 , op->drive_gf, iswrite, op->count, blocksize, op->buf_fl);
280 struct atadrive_s *adrive_gf = container_of(
281 op->drive_gf, struct atadrive_s, drive);
282 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
283 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
284 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
285 int count = op->count;
286 void *buf_fl = op->buf_fl;
290 // Write data to controller
291 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_gf, buf_fl);
292 if (CONFIG_ATA_PIO32)
293 outsl_fl(iobase1, buf_fl, blocksize / 4);
295 outsw_fl(iobase1, buf_fl, blocksize / 2);
297 // Read data from controller
298 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_gf, buf_fl);
299 if (CONFIG_ATA_PIO32)
300 insl_fl(iobase1, buf_fl, blocksize / 4);
302 insw_fl(iobase1, buf_fl, blocksize / 2);
306 status = pause_await_not_bsy(iobase1, iobase2);
316 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
317 if (status != ATA_CB_STAT_DRQ) {
318 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
325 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
328 status &= ~ATA_CB_STAT_DF;
330 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
338 /****************************************************************
340 ****************************************************************/
343 #define BM_CMD_MEMWRITE 0x08
344 #define BM_CMD_START 0x01
346 #define BM_STATUS_IRQ 0x04
347 #define BM_STATUS_ERROR 0x02
348 #define BM_STATUS_ACTIVE 0x01
356 // Check if DMA available and setup transfer if so.
358 ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
361 if (! CONFIG_ATA_DMA)
363 u32 dest = (u32)op->buf_fl;
365 // Need minimum alignment of 1.
367 struct atadrive_s *adrive_gf = container_of(
368 op->drive_gf, struct atadrive_s, drive);
369 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
370 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
373 u32 bytes = op->count * blocksize;
377 // Build PRD dma structure.
378 struct sff_dma_prd *dma = MAKE_FLATPTR(SEG_LOW, ExtraStack);
379 struct sff_dma_prd *origdma = dma;
381 if (dma >= &origdma[16])
382 // Too many descriptors..
385 u32 max = 0x10000 - (dest & 0xffff);
389 SET_LOWFLAT(dma->buf_fl, dest);
394 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
396 SET_LOWFLAT(dma->count, count);
400 // Program bus-master controller.
401 outl((u32)origdma, iomaster + BM_TABLE);
402 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
403 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
404 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
409 // Transfer data using DMA.
411 ata_dma_transfer(struct disk_op_s *op)
413 if (! CONFIG_ATA_DMA)
415 dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_gf, op->buf_fl);
417 struct atadrive_s *adrive_gf = container_of(
418 op->drive_gf, struct atadrive_s, drive);
419 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
420 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
422 // Start bus-master controller.
423 u8 oldcmd = inb(iomaster + BM_CMD);
424 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
426 u32 end = timer_calc(IDE_TIMEOUT);
429 status = inb(iomaster + BM_STATUS);
430 if (status & BM_STATUS_IRQ)
432 // Transfer in progress
433 if (timer_check(end)) {
440 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
442 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
443 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
444 int idestatus = pause_await_not_bsy(iobase1, iobase2);
446 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
448 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
449 | ATA_CB_STAT_ERR)) == 0x00)
453 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
454 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
459 /****************************************************************
460 * ATA hard drive functions
461 ****************************************************************/
463 // Transfer data to harddrive using PIO protocol.
465 ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
467 struct atadrive_s *adrive_gf = container_of(
468 op->drive_gf, struct atadrive_s, drive);
469 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
470 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
471 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
473 // Disable interrupts
474 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
476 int ret = send_cmd(adrive_gf, cmd);
479 ret = ata_wait_data(iobase1);
482 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
486 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
490 // Transfer data to harddrive using DMA protocol.
492 ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
494 if (! CONFIG_ATA_DMA)
496 struct atadrive_s *adrive_gf = container_of(
497 op->drive_gf, struct atadrive_s, drive);
498 int ret = send_cmd(adrive_gf, cmd);
501 return ata_dma_transfer(op);
504 // Read/write count blocks from a harddrive.
506 ata_readwrite(struct disk_op_s *op, int iswrite)
510 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
512 struct ata_pio_command cmd;
513 memset(&cmd, 0, sizeof(cmd));
515 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
516 cmd.sector_count2 = op->count >> 8;
517 cmd.lba_low2 = lba >> 24;
518 cmd.lba_mid2 = lba >> 32;
519 cmd.lba_high2 = lba >> 40;
523 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
524 : ATA_CMD_READ_SECTORS_EXT);
526 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
527 : ATA_CMD_READ_DMA_EXT);
530 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
531 : ATA_CMD_READ_SECTORS);
533 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
537 cmd.sector_count = op->count;
539 cmd.lba_mid = lba >> 8;
540 cmd.lba_high = lba >> 16;
541 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
545 ret = ata_pio_cmd_data(op, iswrite, &cmd);
547 ret = ata_dma_cmd_data(op, &cmd);
549 return DISK_RET_EBADTRACK;
550 return DISK_RET_SUCCESS;
553 // 16bit command demuxer for ATA harddrives.
555 ata_process_op(struct disk_op_s *op)
560 struct atadrive_s *adrive_gf = container_of(
561 op->drive_gf, struct atadrive_s, drive);
562 switch (op->command) {
564 return ata_readwrite(op, 0);
566 return ata_readwrite(op, 1);
568 ata_reset(adrive_gf);
569 return DISK_RET_SUCCESS;
571 return isready(adrive_gf);
573 return default_process_op(op);
578 /****************************************************************
580 ****************************************************************/
582 #define CDROM_CDB_SIZE 12
584 // Low-level atapi command transmit function.
586 ata_atapi_process_op(struct disk_op_s *op)
591 if (op->command == CMD_WRITE || op->command == CMD_FORMAT)
592 return DISK_RET_EWRITEPROTECT;
593 u8 cdbcmd[CDROM_CDB_SIZE];
594 int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd));
596 return default_process_op(op);
598 struct atadrive_s *adrive_gf = container_of(
599 op->drive_gf, struct atadrive_s, drive);
600 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
601 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
602 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
604 struct ata_pio_command cmd;
605 memset(&cmd, 0, sizeof(cmd));
606 cmd.lba_mid = blocksize;
607 cmd.lba_high = blocksize >> 8;
608 cmd.command = ATA_CMD_PACKET;
610 // Disable interrupts
611 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
613 int ret = send_cmd(adrive_gf, &cmd);
616 ret = ata_wait_data(iobase1);
620 // Send command to device
621 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
623 int status = pause_await_not_bsy(iobase1, iobase2);
629 if (status & ATA_CB_STAT_ERR) {
630 u8 err = inb(iobase1 + ATA_CB_ERR);
633 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
639 if (!(status & ATA_CB_STAT_DRQ)) {
640 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
645 ret = ata_pio_transfer(op, 0, blocksize);
650 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
652 return DISK_RET_EBADTRACK;
653 return DISK_RET_SUCCESS;
657 /****************************************************************
658 * ATA detect and init
659 ****************************************************************/
661 // Send an identify device or identify device packet command.
663 send_ata_identity(struct atadrive_s *adrive, u16 *buffer, int command)
665 memset(buffer, 0, DISK_SECTOR_SIZE);
667 struct disk_op_s dop;
668 memset(&dop, 0, sizeof(dop));
669 dop.drive_gf = &adrive->drive;
672 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
674 struct ata_pio_command cmd;
675 memset(&cmd, 0, sizeof(cmd));
676 cmd.command = command;
678 return ata_pio_cmd_data(&dop, 0, &cmd);
681 // Extract the ATA/ATAPI version info.
683 ata_extract_version(u16 *buffer)
685 // Extract ATA/ATAPI version.
686 u16 ataversion = buffer[80];
688 for (version=15; version>0; version--)
689 if (ataversion & (1<<version))
696 // Extract the ATA/ATAPI model info.
698 ata_extract_model(char *model, u32 size, u16 *buffer)
702 for (i=0; i<size/2; i++)
703 *(u16*)&model[i*2] = be16_to_cpu(buffer[27+i]);
705 nullTrailingSpace(model);
709 // Common init code between ata and atapi
710 static struct atadrive_s *
711 init_atadrive(struct atadrive_s *dummy, u16 *buffer)
713 struct atadrive_s *adrive = malloc_fseg(sizeof(*adrive));
718 memset(adrive, 0, sizeof(*adrive));
719 adrive->chan_gf = dummy->chan_gf;
720 adrive->slave = dummy->slave;
721 adrive->drive.cntl_id = adrive->chan_gf->ataid * 2 + dummy->slave;
722 adrive->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
726 // Detect if the given drive is an atapi - initialize it if so.
727 static struct atadrive_s *
728 init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
730 // Send an IDENTIFY_DEVICE_PACKET command to device
731 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
735 // Success - setup as ATAPI.
736 struct atadrive_s *adrive = init_atadrive(dummy, buffer);
739 adrive->drive.type = DTYPE_ATA_ATAPI;
740 adrive->drive.blksize = CDROM_SECTOR_SIZE;
741 adrive->drive.sectors = (u64)-1;
742 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
743 char model[MAXMODEL+1];
744 char *desc = znprintf(MAXDESCSIZE
745 , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]"
746 , adrive->chan_gf->ataid, adrive->slave
747 , ata_extract_model(model, MAXMODEL, buffer)
748 , ata_extract_version(buffer)
749 , (iscd ? "DVD/CD" : "Device"));
750 dprintf(1, "%s\n", desc);
754 int prio = bootprio_find_ata_device(adrive->chan_gf->pci_tmp,
755 adrive->chan_gf->chanid,
757 boot_add_cd(&adrive->drive, desc, prio);
763 // Detect if the given drive is a regular ata drive - initialize it if so.
764 static struct atadrive_s *
765 init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
767 // Send an IDENTIFY_DEVICE command to device
768 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
772 // Success - setup as ATA.
773 struct atadrive_s *adrive = init_atadrive(dummy, buffer);
776 adrive->drive.type = DTYPE_ATA;
777 adrive->drive.blksize = DISK_SECTOR_SIZE;
779 adrive->drive.pchs.cylinder = buffer[1];
780 adrive->drive.pchs.head = buffer[3];
781 adrive->drive.pchs.sector = buffer[6];
784 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
785 sectors = *(u64*)&buffer[100]; // word 100-103
787 sectors = *(u32*)&buffer[60]; // word 60 and word 61
788 adrive->drive.sectors = sectors;
789 u64 adjsize = sectors >> 11;
790 char adjprefix = 'M';
791 if (adjsize >= (1 << 16)) {
795 char model[MAXMODEL+1];
796 char *desc = znprintf(MAXDESCSIZE
797 , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
798 , adrive->chan_gf->ataid, adrive->slave
799 , ata_extract_model(model, MAXMODEL, buffer)
800 , ata_extract_version(buffer)
801 , (u32)adjsize, adjprefix);
802 dprintf(1, "%s\n", desc);
804 int prio = bootprio_find_ata_device(adrive->chan_gf->pci_tmp,
805 adrive->chan_gf->chanid,
807 // Register with bcv system.
808 boot_add_hd(&adrive->drive, desc, prio);
813 static u32 SpinupEnd;
815 // Wait for non-busy status and check for "floating bus" condition.
817 powerup_await_non_bsy(u16 base)
822 status = inb(base+ATA_CB_STAT);
823 if (!(status & ATA_CB_STAT_BSY))
826 if (orstatus == 0xff) {
827 dprintf(4, "powerup IDE floating\n");
830 if (timer_check(SpinupEnd)) {
836 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
840 // Detect any drives attached to a given controller.
842 ata_detect(void *data)
844 struct ata_channel_s *chan_gf = data;
845 struct atadrive_s dummy;
846 memset(&dummy, 0, sizeof(dummy));
847 dummy.chan_gf = chan_gf;
851 for (slave=0; slave<=1; slave++) {
853 u16 iobase1 = chan_gf->iobase1;
854 int status = powerup_await_non_bsy(iobase1);
857 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
858 outb(newdh, iobase1+ATA_CB_DH);
860 status = powerup_await_non_bsy(iobase1);
864 // Check if ioport registers look valid.
865 outb(newdh, iobase1+ATA_CB_DH);
866 u8 dh = inb(iobase1+ATA_CB_DH);
867 outb(0x55, iobase1+ATA_CB_SC);
868 outb(0xaa, iobase1+ATA_CB_SN);
869 u8 sc = inb(iobase1+ATA_CB_SC);
870 u8 sn = inb(iobase1+ATA_CB_SN);
871 dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
872 , chan_gf->ataid, slave, sc, sn, dh);
873 if (sc != 0x55 || sn != 0xaa || dh != newdh)
876 // Prepare new drive.
887 struct atadrive_s *adrive = init_drive_atapi(&dummy, buffer);
889 // Didn't find an ATAPI drive - look for ATA drive.
890 u8 st = inb(iobase1+ATA_CB_STAT);
892 // Status not set - can't be a valid drive.
896 int ret = await_rdy(iobase1);
901 adrive = init_drive_ata(&dummy, buffer);
903 // No ATA drive found
907 u16 resetresult = buffer[93];
908 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
909 if (!slave && (resetresult & 0xdf61) == 0x4041)
910 // resetresult looks valid and device 0 is responding to
911 // device 1 requests - device 1 must not be present - skip
917 // Initialize an ata controller and detect its drives.
919 init_controller(struct pci_device *pci, int chanid, int irq
920 , u32 port1, u32 port2, u32 master)
922 static int ataid = 0;
923 struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
928 chan_gf->ataid = ataid++;
929 chan_gf->chanid = chanid;
931 chan_gf->pci_bdf = pci ? pci->bdf : -1;
932 chan_gf->pci_tmp = pci;
933 chan_gf->iobase1 = port1;
934 chan_gf->iobase2 = port2;
935 chan_gf->iomaster = master;
936 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
937 , ataid, port1, port2, master, irq, chan_gf->pci_bdf);
938 run_thread(ata_detect, chan_gf);
944 // Handle controllers on an ATA PCI device.
946 init_pciata(struct pci_device *pci, u8 prog_if)
948 pci->have_driver = 1;
950 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
952 if (CONFIG_ATA_DMA && prog_if & 0x80) {
953 // Check for bus-mastering.
954 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
955 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
956 master = bar & PCI_BASE_ADDRESS_IO_MASK;
957 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
961 u32 port1, port2, irq;
963 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
964 & PCI_BASE_ADDRESS_IO_MASK);
965 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1)
966 & PCI_BASE_ADDRESS_IO_MASK);
969 port1 = PORT_ATA1_CMD_BASE;
970 port2 = PORT_ATA1_CTRL_BASE;
973 init_controller(pci, 0, irq, port1, port2, master);
976 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2)
977 & PCI_BASE_ADDRESS_IO_MASK);
978 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3)
979 & PCI_BASE_ADDRESS_IO_MASK);
982 port1 = PORT_ATA2_CMD_BASE;
983 port2 = PORT_ATA2_CTRL_BASE;
986 init_controller(pci, 1, irq, port1, port2, master ? master + 8 : 0);
990 found_genericata(struct pci_device *pci, void *arg)
992 init_pciata(pci, pci->prog_if);
996 found_compatibleahci(struct pci_device *pci, void *arg)
999 // Already handled directly via native ahci interface.
1001 init_pciata(pci, 0x8f);
1004 static const struct pci_device_id pci_ata_tbl[] = {
1005 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE
1006 , found_genericata),
1007 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci),
1011 // Locate and init ata controllers.
1015 if (CONFIG_QEMU && hlist_empty(&PCIDevices)) {
1016 // No PCI devices found - probably a QEMU "-M isapc" machine.
1017 // Try using ISA ports for ATA controllers.
1018 init_controller(NULL, 0, IRQ_ATA1
1019 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
1020 init_controller(NULL, 1, IRQ_ATA2
1021 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
1025 // Scan PCI bus for ATA adapters
1026 struct pci_device *pci;
1028 pci_init_device(pci_ata_tbl, pci, NULL);
1039 dprintf(3, "init hard drives\n");
1041 SpinupEnd = timer_calc(IDE_TIMEOUT);
1044 SET_BDA(disk_control_byte, 0xc0);
1046 enable_hwirq(14, FUNC16(entry_76));