These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / hw / ata.c
1 // Low level ATA disk access
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
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
24
25 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
26
27
28 /****************************************************************
29  * Helper functions
30  ****************************************************************/
31
32 // Wait for the specified ide state
33 static inline int
34 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
35 {
36     u32 end = timer_calc(timeout);
37     for (;;) {
38         u8 status = inb(base+ATA_CB_STAT);
39         if ((status & mask) == flags)
40             return status;
41         if (timer_check(end)) {
42             warn_timeout();
43             return -1;
44         }
45         yield();
46     }
47 }
48
49 // Wait for the device to be not-busy.
50 static int
51 await_not_bsy(u16 base)
52 {
53     return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
54 }
55
56 // Wait for the device to be ready.
57 static int
58 await_rdy(u16 base)
59 {
60     return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
61 }
62
63 // Wait for ide state - pauses for one ata cycle first.
64 static inline int
65 pause_await_not_bsy(u16 iobase1, u16 iobase2)
66 {
67     // Wait one PIO transfer cycle.
68     inb(iobase2 + ATA_CB_ASTAT);
69
70     return await_not_bsy(iobase1);
71 }
72
73 // Wait for ide state - pause for 400ns first.
74 static inline int
75 ndelay_await_not_bsy(u16 iobase1)
76 {
77     ndelay(400);
78     return await_not_bsy(iobase1);
79 }
80
81 // Reset a drive
82 static void
83 ata_reset(struct atadrive_s *adrive_gf)
84 {
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);
89
90     dprintf(6, "ata_reset drive=%p\n", &adrive_gf->drive);
91     // Pulse SRST
92     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
93     udelay(5);
94     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
95     msleep(2);
96
97     // wait for device to become not busy.
98     int status = await_not_bsy(iobase1);
99     if (status < 0)
100         goto done;
101     if (slave) {
102         // Change device.
103         u32 end = timer_calc(IDE_TIMEOUT);
104         for (;;) {
105             outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
106             status = ndelay_await_not_bsy(iobase1);
107             if (status < 0)
108                 goto done;
109             if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
110                 break;
111             // Change drive request failed to take effect - retry.
112             if (timer_check(end)) {
113                 warn_timeout();
114                 goto done;
115             }
116         }
117     } else {
118         // QEMU doesn't reset dh on reset, so set it explicitly.
119         outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
120     }
121
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);
126
127 done:
128     // Enable interrupts
129     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
130
131     dprintf(6, "ata_reset exit status=%x\n", status);
132 }
133
134 // Check for drive RDY for 16bit interface command.
135 static int
136 isready(struct atadrive_s *adrive_gf)
137 {
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;
145 }
146
147
148 /****************************************************************
149  * ATA send command
150  ****************************************************************/
151
152 struct ata_pio_command {
153     u8 feature;
154     u8 sector_count;
155     u8 lba_low;
156     u8 lba_mid;
157     u8 lba_high;
158     u8 device;
159     u8 command;
160
161     u8 feature2;
162     u8 sector_count2;
163     u8 lba_low2;
164     u8 lba_mid2;
165     u8 lba_high2;
166 };
167
168 // Send an ata command to the drive.
169 static int
170 send_cmd(struct atadrive_s *adrive_gf, struct ata_pio_command *cmd)
171 {
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);
175
176     // Select device
177     int status = await_not_bsy(iobase1);
178     if (status < 0)
179         return status;
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);
187         if (status < 0)
188             return status;
189     }
190
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);
198     }
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);
205
206     return 0;
207 }
208
209 // Wait for data after calling 'send_cmd'.
210 static int
211 ata_wait_data(u16 iobase1)
212 {
213     int status = ndelay_await_not_bsy(iobase1);
214     if (status < 0)
215         return status;
216
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));
220         return -4;
221     }
222     if (!(status & ATA_CB_STAT_DRQ)) {
223         dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
224         return -5;
225     }
226
227     return 0;
228 }
229
230 // Send an ata command that does not transfer any further data.
231 int
232 ata_cmd_nondata(struct atadrive_s *adrive_gf, struct ata_pio_command *cmd)
233 {
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);
237
238     // Disable interrupts
239     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
240
241     int ret = send_cmd(adrive_gf, cmd);
242     if (ret)
243         goto fail;
244     ret = ndelay_await_not_bsy(iobase1);
245     if (ret < 0)
246         goto fail;
247
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));
251         ret = -4;
252         goto fail;
253     }
254     if (ret & ATA_CB_STAT_DRQ) {
255         dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
256         ret = -5;
257         goto fail;
258     }
259
260 fail:
261     // Enable interrupts
262     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
263
264     return ret;
265 }
266
267
268 /****************************************************************
269  * ATA PIO transfers
270  ****************************************************************/
271
272 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
273 // 'op->drive_gf'.
274 static int
275 ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
276 {
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);
279
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;
287     int status;
288     for (;;) {
289         if (iswrite) {
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);
294             else
295                 outsw_fl(iobase1, buf_fl, blocksize / 2);
296         } else {
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);
301             else
302                 insw_fl(iobase1, buf_fl, blocksize / 2);
303         }
304         buf_fl += blocksize;
305
306         status = pause_await_not_bsy(iobase1, iobase2);
307         if (status < 0) {
308             // Error
309             op->count -= count;
310             return status;
311         }
312
313         count--;
314         if (!count)
315             break;
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"
319                     , status);
320             op->count -= count;
321             return -6;
322         }
323     }
324
325     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
326                | ATA_CB_STAT_ERR);
327     if (!iswrite)
328         status &= ~ATA_CB_STAT_DF;
329     if (status != 0) {
330         dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
331         return -7;
332     }
333
334     return 0;
335 }
336
337
338 /****************************************************************
339  * ATA DMA transfers
340  ****************************************************************/
341
342 #define BM_CMD    0
343 #define  BM_CMD_MEMWRITE  0x08
344 #define  BM_CMD_START     0x01
345 #define BM_STATUS 2
346 #define  BM_STATUS_IRQ    0x04
347 #define  BM_STATUS_ERROR  0x02
348 #define  BM_STATUS_ACTIVE 0x01
349 #define BM_TABLE  4
350
351 struct sff_dma_prd {
352     u32 buf_fl;
353     u32 count;
354 };
355
356 // Check if DMA available and setup transfer if so.
357 static int
358 ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
359 {
360     ASSERT16();
361     if (! CONFIG_ATA_DMA)
362         return -1;
363     u32 dest = (u32)op->buf_fl;
364     if (dest & 1)
365         // Need minimum alignment of 1.
366         return -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);
371     if (! iomaster)
372         return -1;
373     u32 bytes = op->count * blocksize;
374     if (! bytes)
375         return -1;
376
377     // Build PRD dma structure.
378     struct sff_dma_prd *dma = MAKE_FLATPTR(SEG_LOW, ExtraStack);
379     struct sff_dma_prd *origdma = dma;
380     while (bytes) {
381         if (dma >= &origdma[16])
382             // Too many descriptors..
383             return -1;
384         u32 count = bytes;
385         u32 max = 0x10000 - (dest & 0xffff);
386         if (count > max)
387             count = max;
388
389         SET_LOWFLAT(dma->buf_fl, dest);
390         bytes -= count;
391         if (!bytes)
392             // Last descriptor.
393             count |= 1<<31;
394         dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
395         dest += count;
396         SET_LOWFLAT(dma->count, count);
397         dma++;
398     }
399
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);
405
406     return 0;
407 }
408
409 // Transfer data using DMA.
410 static int
411 ata_dma_transfer(struct disk_op_s *op)
412 {
413     if (! CONFIG_ATA_DMA)
414         return -1;
415     dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_gf, op->buf_fl);
416
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);
421
422     // Start bus-master controller.
423     u8 oldcmd = inb(iomaster + BM_CMD);
424     outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
425
426     u32 end = timer_calc(IDE_TIMEOUT);
427     u8 status;
428     for (;;) {
429         status = inb(iomaster + BM_STATUS);
430         if (status & BM_STATUS_IRQ)
431             break;
432         // Transfer in progress
433         if (timer_check(end)) {
434             // Timeout.
435             warn_timeout();
436             break;
437         }
438         yield();
439     }
440     outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
441
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);
445
446     if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
447         && idestatus >= 0x00
448         && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
449                          | ATA_CB_STAT_ERR)) == 0x00)
450         // Success.
451         return 0;
452
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));
455     return -1;
456 }
457
458
459 /****************************************************************
460  * ATA hard drive functions
461  ****************************************************************/
462
463 // Transfer data to harddrive using PIO protocol.
464 static int
465 ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
466 {
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);
472
473     // Disable interrupts
474     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
475
476     int ret = send_cmd(adrive_gf, cmd);
477     if (ret)
478         goto fail;
479     ret = ata_wait_data(iobase1);
480     if (ret)
481         goto fail;
482     ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
483
484 fail:
485     // Enable interrupts
486     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
487     return ret;
488 }
489
490 // Transfer data to harddrive using DMA protocol.
491 static int
492 ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
493 {
494     if (! CONFIG_ATA_DMA)
495         return -1;
496     struct atadrive_s *adrive_gf = container_of(
497         op->drive_gf, struct atadrive_s, drive);
498     int ret = send_cmd(adrive_gf, cmd);
499     if (ret)
500         return ret;
501     return ata_dma_transfer(op);
502 }
503
504 // Read/write count blocks from a harddrive.
505 static int
506 ata_readwrite(struct disk_op_s *op, int iswrite)
507 {
508     u64 lba = op->lba;
509
510     int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
511
512     struct ata_pio_command cmd;
513     memset(&cmd, 0, sizeof(cmd));
514
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;
520         lba &= 0xffffff;
521
522         if (usepio)
523             cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
524                            : ATA_CMD_READ_SECTORS_EXT);
525         else
526             cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
527                            : ATA_CMD_READ_DMA_EXT);
528     } else {
529         if (usepio)
530             cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
531                            : ATA_CMD_READ_SECTORS);
532         else
533             cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
534                            : ATA_CMD_READ_DMA);
535     }
536
537     cmd.sector_count = op->count;
538     cmd.lba_low = lba;
539     cmd.lba_mid = lba >> 8;
540     cmd.lba_high = lba >> 16;
541     cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
542
543     int ret;
544     if (usepio)
545         ret = ata_pio_cmd_data(op, iswrite, &cmd);
546     else
547         ret = ata_dma_cmd_data(op, &cmd);
548     if (ret)
549         return DISK_RET_EBADTRACK;
550     return DISK_RET_SUCCESS;
551 }
552
553 // 16bit command demuxer for ATA harddrives.
554 int
555 ata_process_op(struct disk_op_s *op)
556 {
557     if (!CONFIG_ATA)
558         return 0;
559
560     struct atadrive_s *adrive_gf = container_of(
561         op->drive_gf, struct atadrive_s, drive);
562     switch (op->command) {
563     case CMD_READ:
564         return ata_readwrite(op, 0);
565     case CMD_WRITE:
566         return ata_readwrite(op, 1);
567     case CMD_RESET:
568         ata_reset(adrive_gf);
569         return DISK_RET_SUCCESS;
570     case CMD_ISREADY:
571         return isready(adrive_gf);
572     default:
573         return default_process_op(op);
574     }
575 }
576
577
578 /****************************************************************
579  * ATAPI functions
580  ****************************************************************/
581
582 #define CDROM_CDB_SIZE 12
583
584 // Low-level atapi command transmit function.
585 int
586 ata_atapi_process_op(struct disk_op_s *op)
587 {
588     if (! CONFIG_ATA)
589         return 0;
590
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));
595     if (blocksize < 0)
596         return default_process_op(op);
597
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);
603
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;
609
610     // Disable interrupts
611     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
612
613     int ret = send_cmd(adrive_gf, &cmd);
614     if (ret)
615         goto fail;
616     ret = ata_wait_data(iobase1);
617     if (ret)
618         goto fail;
619
620     // Send command to device
621     outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
622
623     int status = pause_await_not_bsy(iobase1, iobase2);
624     if (status < 0) {
625         ret = status;
626         goto fail;
627     }
628
629     if (status & ATA_CB_STAT_ERR) {
630         u8 err = inb(iobase1 + ATA_CB_ERR);
631         // skip "Not Ready"
632         if (err != 0x20)
633             dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
634                     , status, err);
635         ret = -2;
636         goto fail;
637     }
638     if (blocksize) {
639         if (!(status & ATA_CB_STAT_DRQ)) {
640             dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
641             ret = -3;
642             goto fail;
643         }
644
645         ret = ata_pio_transfer(op, 0, blocksize);
646     }
647
648 fail:
649     // Enable interrupts
650     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
651     if (ret)
652         return DISK_RET_EBADTRACK;
653     return DISK_RET_SUCCESS;
654 }
655
656
657 /****************************************************************
658  * ATA detect and init
659  ****************************************************************/
660
661 // Send an identify device or identify device packet command.
662 static int
663 send_ata_identity(struct atadrive_s *adrive, u16 *buffer, int command)
664 {
665     memset(buffer, 0, DISK_SECTOR_SIZE);
666
667     struct disk_op_s dop;
668     memset(&dop, 0, sizeof(dop));
669     dop.drive_gf = &adrive->drive;
670     dop.count = 1;
671     dop.lba = 1;
672     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
673
674     struct ata_pio_command cmd;
675     memset(&cmd, 0, sizeof(cmd));
676     cmd.command = command;
677
678     return ata_pio_cmd_data(&dop, 0, &cmd);
679 }
680
681 // Extract the ATA/ATAPI version info.
682 int
683 ata_extract_version(u16 *buffer)
684 {
685     // Extract ATA/ATAPI version.
686     u16 ataversion = buffer[80];
687     u8 version;
688     for (version=15; version>0; version--)
689         if (ataversion & (1<<version))
690             break;
691     return version;
692 }
693
694 #define MAXMODEL 40
695
696 // Extract the ATA/ATAPI model info.
697 char *
698 ata_extract_model(char *model, u32 size, u16 *buffer)
699 {
700     // Read model name
701     int i;
702     for (i=0; i<size/2; i++)
703         *(u16*)&model[i*2] = be16_to_cpu(buffer[27+i]);
704     model[size] = 0x00;
705     nullTrailingSpace(model);
706     return model;
707 }
708
709 // Common init code between ata and atapi
710 static struct atadrive_s *
711 init_atadrive(struct atadrive_s *dummy, u16 *buffer)
712 {
713     struct atadrive_s *adrive = malloc_fseg(sizeof(*adrive));
714     if (!adrive) {
715         warn_noalloc();
716         return NULL;
717     }
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;
723     return adrive;
724 }
725
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)
729 {
730     // Send an IDENTIFY_DEVICE_PACKET command to device
731     int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
732     if (ret)
733         return NULL;
734
735     // Success - setup as ATAPI.
736     struct atadrive_s *adrive = init_atadrive(dummy, buffer);
737     if (!adrive)
738         return NULL;
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);
751
752     // fill cdidmap
753     if (iscd) {
754         int prio = bootprio_find_ata_device(adrive->chan_gf->pci_tmp,
755                                             adrive->chan_gf->chanid,
756                                             adrive->slave);
757         boot_add_cd(&adrive->drive, desc, prio);
758     }
759
760     return adrive;
761 }
762
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)
766 {
767     // Send an IDENTIFY_DEVICE command to device
768     int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
769     if (ret)
770         return NULL;
771
772     // Success - setup as ATA.
773     struct atadrive_s *adrive = init_atadrive(dummy, buffer);
774     if (!adrive)
775         return NULL;
776     adrive->drive.type = DTYPE_ATA;
777     adrive->drive.blksize = DISK_SECTOR_SIZE;
778
779     adrive->drive.pchs.cylinder = buffer[1];
780     adrive->drive.pchs.head = buffer[3];
781     adrive->drive.pchs.sector = buffer[6];
782
783     u64 sectors;
784     if (buffer[83] & (1 << 10)) // word 83 - lba48 support
785         sectors = *(u64*)&buffer[100]; // word 100-103
786     else
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)) {
792         adjsize >>= 10;
793         adjprefix = 'G';
794     }
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);
803
804     int prio = bootprio_find_ata_device(adrive->chan_gf->pci_tmp,
805                                         adrive->chan_gf->chanid,
806                                         adrive->slave);
807     // Register with bcv system.
808     boot_add_hd(&adrive->drive, desc, prio);
809
810     return adrive;
811 }
812
813 static u32 SpinupEnd;
814
815 // Wait for non-busy status and check for "floating bus" condition.
816 static int
817 powerup_await_non_bsy(u16 base)
818 {
819     u8 orstatus = 0;
820     u8 status;
821     for (;;) {
822         status = inb(base+ATA_CB_STAT);
823         if (!(status & ATA_CB_STAT_BSY))
824             break;
825         orstatus |= status;
826         if (orstatus == 0xff) {
827             dprintf(4, "powerup IDE floating\n");
828             return orstatus;
829         }
830         if (timer_check(SpinupEnd)) {
831             warn_timeout();
832             return -1;
833         }
834         yield();
835     }
836     dprintf(6, "powerup iobase=%x st=%x\n", base, status);
837     return status;
838 }
839
840 // Detect any drives attached to a given controller.
841 static void
842 ata_detect(void *data)
843 {
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;
848     // Device detection
849     int didreset = 0;
850     u8 slave;
851     for (slave=0; slave<=1; slave++) {
852         // Wait for not-bsy.
853         u16 iobase1 = chan_gf->iobase1;
854         int status = powerup_await_non_bsy(iobase1);
855         if (status < 0)
856             continue;
857         u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
858         outb(newdh, iobase1+ATA_CB_DH);
859         ndelay(400);
860         status = powerup_await_non_bsy(iobase1);
861         if (status < 0)
862             continue;
863
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)
874             continue;
875
876         // Prepare new drive.
877         dummy.slave = slave;
878
879         // reset the channel
880         if (!didreset) {
881             ata_reset(&dummy);
882             didreset = 1;
883         }
884
885         // check for ATAPI
886         u16 buffer[256];
887         struct atadrive_s *adrive = init_drive_atapi(&dummy, buffer);
888         if (!adrive) {
889             // Didn't find an ATAPI drive - look for ATA drive.
890             u8 st = inb(iobase1+ATA_CB_STAT);
891             if (!st)
892                 // Status not set - can't be a valid drive.
893                 continue;
894
895             // Wait for RDY.
896             int ret = await_rdy(iobase1);
897             if (ret < 0)
898                 continue;
899
900             // check for ATA.
901             adrive = init_drive_ata(&dummy, buffer);
902             if (!adrive)
903                 // No ATA drive found
904                 continue;
905         }
906
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
912             // detection.
913             break;
914     }
915 }
916
917 // Initialize an ata controller and detect its drives.
918 static void
919 init_controller(struct pci_device *pci, int chanid, int irq
920                 , u32 port1, u32 port2, u32 master)
921 {
922     static int ataid = 0;
923     struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
924     if (!chan_gf) {
925         warn_noalloc();
926         return;
927     }
928     chan_gf->ataid = ataid++;
929     chan_gf->chanid = chanid;
930     chan_gf->irq = irq;
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);
939 }
940
941 #define IRQ_ATA1 14
942 #define IRQ_ATA2 15
943
944 // Handle controllers on an ATA PCI device.
945 static void
946 init_pciata(struct pci_device *pci, u8 prog_if)
947 {
948     pci->have_driver = 1;
949     u16 bdf = pci->bdf;
950     u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
951     int master = 0;
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);
958         }
959     }
960
961     u32 port1, port2, irq;
962     if (prog_if & 1) {
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);
967         irq = pciirq;
968     } else {
969         port1 = PORT_ATA1_CMD_BASE;
970         port2 = PORT_ATA1_CTRL_BASE;
971         irq = IRQ_ATA1;
972     }
973     init_controller(pci, 0, irq, port1, port2, master);
974
975     if (prog_if & 4) {
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);
980         irq = pciirq;
981     } else {
982         port1 = PORT_ATA2_CMD_BASE;
983         port2 = PORT_ATA2_CTRL_BASE;
984         irq = IRQ_ATA2;
985     }
986     init_controller(pci, 1, irq, port1, port2, master ? master + 8 : 0);
987 }
988
989 static void
990 found_genericata(struct pci_device *pci, void *arg)
991 {
992     init_pciata(pci, pci->prog_if);
993 }
994
995 static void
996 found_compatibleahci(struct pci_device *pci, void *arg)
997 {
998     if (CONFIG_AHCI)
999         // Already handled directly via native ahci interface.
1000         return;
1001     init_pciata(pci, 0x8f);
1002 }
1003
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),
1008     PCI_DEVICE_END,
1009 };
1010
1011 // Locate and init ata controllers.
1012 static void
1013 ata_scan(void)
1014 {
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);
1022         return;
1023     }
1024
1025     // Scan PCI bus for ATA adapters
1026     struct pci_device *pci;
1027     foreachpci(pci) {
1028         pci_init_device(pci_ata_tbl, pci, NULL);
1029     }
1030 }
1031
1032 void
1033 ata_setup(void)
1034 {
1035     ASSERT32FLAT();
1036     if (!CONFIG_ATA)
1037         return;
1038
1039     dprintf(3, "init hard drives\n");
1040
1041     SpinupEnd = timer_calc(IDE_TIMEOUT);
1042     ata_scan();
1043
1044     SET_BDA(disk_control_byte, 0xc0);
1045
1046     enable_hwirq(14, FUNC16(entry_76));
1047 }