Adjust debug levels of device discovery.
[seabios.git] / src / 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 "types.h" // u8
9 #include "ioport.h" // inb
10 #include "util.h" // dprintf
11 #include "cmos.h" // inb_cmos
12 #include "pic.h" // enable_hwirq
13 #include "biosvar.h" // GET_EBDA
14 #include "pci.h" // foreachpci
15 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16 #include "pci_regs.h" // PCI_INTERRUPT_LINE
17 #include "boot.h" // add_bcv_hd
18 #include "disk.h" // struct ata_s
19 #include "ata.h" // ATA_CB_STAT
20 #include "blockcmd.h" // CDB_CMD_READ_10
21
22 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
23
24
25 /****************************************************************
26  * Helper functions
27  ****************************************************************/
28
29 // Wait for the specified ide state
30 static inline int
31 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
32 {
33     u64 end = calc_future_tsc(timeout);
34     for (;;) {
35         u8 status = inb(base+ATA_CB_STAT);
36         if ((status & mask) == flags)
37             return status;
38         if (check_time(end)) {
39             warn_timeout();
40             return -1;
41         }
42         yield();
43     }
44 }
45
46 // Wait for the device to be not-busy.
47 static int
48 await_not_bsy(u16 base)
49 {
50     return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
51 }
52
53 // Wait for the device to be ready.
54 static int
55 await_rdy(u16 base)
56 {
57     return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
58 }
59
60 // Wait for ide state - pauses for one ata cycle first.
61 static inline int
62 pause_await_not_bsy(u16 iobase1, u16 iobase2)
63 {
64     // Wait one PIO transfer cycle.
65     inb(iobase2 + ATA_CB_ASTAT);
66
67     return await_not_bsy(iobase1);
68 }
69
70 // Wait for ide state - pause for 400ns first.
71 static inline int
72 ndelay_await_not_bsy(u16 iobase1)
73 {
74     ndelay(400);
75     return await_not_bsy(iobase1);
76 }
77
78 // Reset a drive
79 static void
80 ata_reset(struct atadrive_s *adrive_g)
81 {
82     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
83     u8 slave = GET_GLOBAL(adrive_g->slave);
84     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
85     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
86
87     dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive);
88     // Pulse SRST
89     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
90     udelay(5);
91     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
92     msleep(2);
93
94     // wait for device to become not busy.
95     int status = await_not_bsy(iobase1);
96     if (status < 0)
97         goto done;
98     if (slave) {
99         // Change device.
100         u64 end = calc_future_tsc(IDE_TIMEOUT);
101         for (;;) {
102             outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
103             status = ndelay_await_not_bsy(iobase1);
104             if (status < 0)
105                 goto done;
106             if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
107                 break;
108             // Change drive request failed to take effect - retry.
109             if (check_time(end)) {
110                 warn_timeout();
111                 goto done;
112             }
113         }
114     } else {
115         // QEMU doesn't reset dh on reset, so set it explicitly.
116         outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
117     }
118
119     // On a user-reset request, wait for RDY if it is an ATA device.
120     u8 type=GET_GLOBAL(adrive_g->drive.type);
121     if (type == DTYPE_ATA)
122         status = await_rdy(iobase1);
123
124 done:
125     // Enable interrupts
126     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
127
128     dprintf(6, "ata_reset exit status=%x\n", status);
129 }
130
131 // Check for drive RDY for 16bit interface command.
132 static int
133 isready(struct atadrive_s *adrive_g)
134 {
135     // Read the status from controller
136     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
137     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
138     u8 status = inb(iobase1 + ATA_CB_STAT);
139     if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
140         return DISK_RET_SUCCESS;
141     return DISK_RET_ENOTREADY;
142 }
143
144 // Default 16bit command demuxer for ATA and ATAPI devices.
145 static int
146 process_ata_misc_op(struct disk_op_s *op)
147 {
148     if (!CONFIG_ATA)
149         return 0;
150
151     struct atadrive_s *adrive_g = container_of(
152         op->drive_g, struct atadrive_s, drive);
153     switch (op->command) {
154     case CMD_RESET:
155         ata_reset(adrive_g);
156         return DISK_RET_SUCCESS;
157     case CMD_ISREADY:
158         return isready(adrive_g);
159     case CMD_FORMAT:
160     case CMD_VERIFY:
161     case CMD_SEEK:
162         return DISK_RET_SUCCESS;
163     default:
164         op->count = 0;
165         return DISK_RET_EPARAM;
166     }
167 }
168
169
170 /****************************************************************
171  * ATA send command
172  ****************************************************************/
173
174 struct ata_pio_command {
175     u8 feature;
176     u8 sector_count;
177     u8 lba_low;
178     u8 lba_mid;
179     u8 lba_high;
180     u8 device;
181     u8 command;
182
183     u8 feature2;
184     u8 sector_count2;
185     u8 lba_low2;
186     u8 lba_mid2;
187     u8 lba_high2;
188 };
189
190 // Send an ata command to the drive.
191 static int
192 send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
193 {
194     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
195     u8 slave = GET_GLOBAL(adrive_g->slave);
196     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
197
198     // Select device
199     int status = await_not_bsy(iobase1);
200     if (status < 0)
201         return status;
202     u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
203                 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
204     u8 olddh = inb(iobase1 + ATA_CB_DH);
205     outb(newdh, iobase1 + ATA_CB_DH);
206     if ((olddh ^ newdh) & (1<<4)) {
207         // Was a device change - wait for device to become not busy.
208         status = ndelay_await_not_bsy(iobase1);
209         if (status < 0)
210             return status;
211     }
212
213     // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
214     if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
215         outb(cmd->feature2, iobase1 + ATA_CB_FR);
216         outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
217         outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
218         outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
219         outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
220     }
221     outb(cmd->feature, iobase1 + ATA_CB_FR);
222     outb(cmd->sector_count, iobase1 + ATA_CB_SC);
223     outb(cmd->lba_low, iobase1 + ATA_CB_SN);
224     outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
225     outb(cmd->lba_high, iobase1 + ATA_CB_CH);
226     outb(cmd->command, iobase1 + ATA_CB_CMD);
227
228     return 0;
229 }
230
231 // Wait for data after calling 'send_cmd'.
232 static int
233 ata_wait_data(u16 iobase1)
234 {
235     int status = ndelay_await_not_bsy(iobase1);
236     if (status < 0)
237         return status;
238
239     if (status & ATA_CB_STAT_ERR) {
240         dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
241                 , status, inb(iobase1 + ATA_CB_ERR));
242         return -4;
243     }
244     if (!(status & ATA_CB_STAT_DRQ)) {
245         dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
246         return -5;
247     }
248
249     return 0;
250 }
251
252 // Send an ata command that does not transfer any further data.
253 int
254 ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
255 {
256     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
257     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
258     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
259
260     // Disable interrupts
261     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
262
263     int ret = send_cmd(adrive_g, cmd);
264     if (ret)
265         goto fail;
266     ret = ndelay_await_not_bsy(iobase1);
267     if (ret < 0)
268         goto fail;
269
270     if (ret & ATA_CB_STAT_ERR) {
271         dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
272                 , ret, inb(iobase1 + ATA_CB_ERR));
273         ret = -4;
274         goto fail;
275     }
276     if (ret & ATA_CB_STAT_DRQ) {
277         dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
278         ret = -5;
279         goto fail;
280     }
281
282 fail:
283     // Enable interrupts
284     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
285
286     return ret;
287 }
288
289
290 /****************************************************************
291  * ATA PIO transfers
292  ****************************************************************/
293
294 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
295 // 'op->drive_g'.
296 static int
297 ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
298 {
299     dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
300             , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
301
302     struct atadrive_s *adrive_g = container_of(
303         op->drive_g, struct atadrive_s, drive);
304     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
305     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
306     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
307     int count = op->count;
308     void *buf_fl = op->buf_fl;
309     int status;
310     for (;;) {
311         if (iswrite) {
312             // Write data to controller
313             dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
314             if (CONFIG_ATA_PIO32)
315                 outsl_fl(iobase1, buf_fl, blocksize / 4);
316             else
317                 outsw_fl(iobase1, buf_fl, blocksize / 2);
318         } else {
319             // Read data from controller
320             dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
321             if (CONFIG_ATA_PIO32)
322                 insl_fl(iobase1, buf_fl, blocksize / 4);
323             else
324                 insw_fl(iobase1, buf_fl, blocksize / 2);
325         }
326         buf_fl += blocksize;
327
328         status = pause_await_not_bsy(iobase1, iobase2);
329         if (status < 0) {
330             // Error
331             op->count -= count;
332             return status;
333         }
334
335         count--;
336         if (!count)
337             break;
338         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
339         if (status != ATA_CB_STAT_DRQ) {
340             dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
341                     , status);
342             op->count -= count;
343             return -6;
344         }
345     }
346
347     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
348                | ATA_CB_STAT_ERR);
349     if (!iswrite)
350         status &= ~ATA_CB_STAT_DF;
351     if (status != 0) {
352         dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
353         return -7;
354     }
355
356     return 0;
357 }
358
359
360 /****************************************************************
361  * ATA DMA transfers
362  ****************************************************************/
363
364 #define BM_CMD    0
365 #define  BM_CMD_MEMWRITE  0x08
366 #define  BM_CMD_START     0x01
367 #define BM_STATUS 2
368 #define  BM_STATUS_IRQ    0x04
369 #define  BM_STATUS_ERROR  0x02
370 #define  BM_STATUS_ACTIVE 0x01
371 #define BM_TABLE  4
372
373 struct sff_dma_prd {
374     u32 buf_fl;
375     u32 count;
376 };
377
378 // Check if DMA available and setup transfer if so.
379 static int
380 ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
381 {
382     if (! CONFIG_ATA_DMA)
383         return -1;
384     u32 dest = (u32)op->buf_fl;
385     if (dest & 1)
386         // Need minimum alignment of 1.
387         return -1;
388     struct atadrive_s *adrive_g = container_of(
389         op->drive_g, struct atadrive_s, drive);
390     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
391     u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
392     if (! iomaster)
393         return -1;
394     u32 bytes = op->count * blocksize;
395     if (! bytes)
396         return -1;
397
398     // Build PRD dma structure.
399     struct sff_dma_prd *dma = MAKE_FLATPTR(
400         get_ebda_seg()
401         , (void*)offsetof(struct extended_bios_data_area_s, extra_stack));
402     struct sff_dma_prd *origdma = dma;
403     while (bytes) {
404         if (dma >= &origdma[16])
405             // Too many descriptors..
406             return -1;
407         u32 count = bytes;
408         if (count > 0x10000)
409             count = 0x10000;
410         u32 max = 0x10000 - (dest & 0xffff);
411         if (count > max)
412             count = max;
413
414         SET_FLATPTR(dma->buf_fl, dest);
415         bytes -= count;
416         if (!bytes)
417             // Last descriptor.
418             count |= 1<<31;
419         dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
420         dest += count;
421         SET_FLATPTR(dma->count, count);
422         dma++;
423     }
424
425     // Program bus-master controller.
426     outl((u32)origdma, iomaster + BM_TABLE);
427     u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
428     outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
429     outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
430
431     return 0;
432 }
433
434 // Transfer data using DMA.
435 static int
436 ata_dma_transfer(struct disk_op_s *op)
437 {
438     if (! CONFIG_ATA_DMA)
439         return -1;
440     dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl);
441
442     struct atadrive_s *adrive_g = container_of(
443         op->drive_g, struct atadrive_s, drive);
444     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
445     u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
446
447     // Start bus-master controller.
448     u8 oldcmd = inb(iomaster + BM_CMD);
449     outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
450
451     u64 end = calc_future_tsc(IDE_TIMEOUT);
452     u8 status;
453     for (;;) {
454         status = inb(iomaster + BM_STATUS);
455         if (status & BM_STATUS_IRQ)
456             break;
457         // Transfer in progress
458         if (check_time(end)) {
459             // Timeout.
460             warn_timeout();
461             break;
462         }
463         yield();
464     }
465     outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
466
467     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
468     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
469     int idestatus = pause_await_not_bsy(iobase1, iobase2);
470
471     if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
472         && idestatus >= 0x00
473         && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
474                          | ATA_CB_STAT_ERR)) == 0x00)
475         // Success.
476         return 0;
477
478     dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
479             , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
480     op->count = 0;
481     return -1;
482 }
483
484
485 /****************************************************************
486  * ATA hard drive functions
487  ****************************************************************/
488
489 // Transfer data to harddrive using PIO protocol.
490 static int
491 ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
492 {
493     struct atadrive_s *adrive_g = container_of(
494         op->drive_g, struct atadrive_s, drive);
495     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
496     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
497     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
498
499     // Disable interrupts
500     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
501
502     int ret = send_cmd(adrive_g, cmd);
503     if (ret)
504         goto fail;
505     ret = ata_wait_data(iobase1);
506     if (ret)
507         goto fail;
508     ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
509
510 fail:
511     // Enable interrupts
512     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
513     return ret;
514 }
515
516 // Transfer data to harddrive using DMA protocol.
517 static int
518 ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
519 {
520     if (! CONFIG_ATA_DMA)
521         return -1;
522     struct atadrive_s *adrive_g = container_of(
523         op->drive_g, struct atadrive_s, drive);
524     int ret = send_cmd(adrive_g, cmd);
525     if (ret)
526         return ret;
527     return ata_dma_transfer(op);
528 }
529
530 // Read/write count blocks from a harddrive.
531 static int
532 ata_readwrite(struct disk_op_s *op, int iswrite)
533 {
534     u64 lba = op->lba;
535
536     int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
537
538     struct ata_pio_command cmd;
539     memset(&cmd, 0, sizeof(cmd));
540
541     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
542         cmd.sector_count2 = op->count >> 8;
543         cmd.lba_low2 = lba >> 24;
544         cmd.lba_mid2 = lba >> 32;
545         cmd.lba_high2 = lba >> 40;
546         lba &= 0xffffff;
547
548         if (usepio)
549             cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
550                            : ATA_CMD_READ_SECTORS_EXT);
551         else
552             cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
553                            : ATA_CMD_READ_DMA_EXT);
554     } else {
555         if (usepio)
556             cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
557                            : ATA_CMD_READ_SECTORS);
558         else
559             cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
560                            : ATA_CMD_READ_DMA);
561     }
562
563     cmd.sector_count = op->count;
564     cmd.lba_low = lba;
565     cmd.lba_mid = lba >> 8;
566     cmd.lba_high = lba >> 16;
567     cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
568
569     int ret;
570     if (usepio)
571         ret = ata_pio_cmd_data(op, iswrite, &cmd);
572     else
573         ret = ata_dma_cmd_data(op, &cmd);
574     if (ret)
575         return DISK_RET_EBADTRACK;
576     return DISK_RET_SUCCESS;
577 }
578
579 // 16bit command demuxer for ATA harddrives.
580 int
581 process_ata_op(struct disk_op_s *op)
582 {
583     if (!CONFIG_ATA)
584         return 0;
585
586     switch (op->command) {
587     case CMD_READ:
588         return ata_readwrite(op, 0);
589     case CMD_WRITE:
590         return ata_readwrite(op, 1);
591     default:
592         return process_ata_misc_op(op);
593     }
594 }
595
596
597 /****************************************************************
598  * ATAPI functions
599  ****************************************************************/
600
601 #define CDROM_CDB_SIZE 12
602
603 // Low-level atapi command transmit function.
604 int
605 atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
606 {
607     struct atadrive_s *adrive_g = container_of(
608         op->drive_g, struct atadrive_s, drive);
609     struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
610     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
611     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
612
613     struct ata_pio_command cmd;
614     memset(&cmd, 0, sizeof(cmd));
615     cmd.lba_mid = blocksize;
616     cmd.lba_high = blocksize >> 8;
617     cmd.command = ATA_CMD_PACKET;
618
619     // Disable interrupts
620     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
621
622     int ret = send_cmd(adrive_g, &cmd);
623     if (ret)
624         goto fail;
625     ret = ata_wait_data(iobase1);
626     if (ret)
627         goto fail;
628
629     // Send command to device
630     outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
631
632     int status = pause_await_not_bsy(iobase1, iobase2);
633     if (status < 0) {
634         ret = status;
635         goto fail;
636     }
637
638     if (status & ATA_CB_STAT_ERR) {
639         u8 err = inb(iobase1 + ATA_CB_ERR);
640         // skip "Not Ready"
641         if (err != 0x20)
642             dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
643                     , status, err);
644         ret = -2;
645         goto fail;
646     }
647     if (!(status & ATA_CB_STAT_DRQ)) {
648         dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
649         ret = -3;
650         goto fail;
651     }
652
653     ret = ata_pio_transfer(op, 0, blocksize);
654
655 fail:
656     // Enable interrupts
657     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
658     if (ret)
659         return DISK_RET_EBADTRACK;
660     return DISK_RET_SUCCESS;
661 }
662
663 // 16bit command demuxer for ATAPI cdroms.
664 int
665 process_atapi_op(struct disk_op_s *op)
666 {
667     switch (op->command) {
668     case CMD_READ:
669         return cdb_read(op);
670     case CMD_FORMAT:
671     case CMD_WRITE:
672         return DISK_RET_EWRITEPROTECT;
673     default:
674         return process_ata_misc_op(op);
675     }
676 }
677
678
679 /****************************************************************
680  * ATA detect and init
681  ****************************************************************/
682
683 // Send an identify device or identify device packet command.
684 static int
685 send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command)
686 {
687     memset(buffer, 0, DISK_SECTOR_SIZE);
688
689     struct disk_op_s dop;
690     memset(&dop, 0, sizeof(dop));
691     dop.drive_g = &adrive_g->drive;
692     dop.count = 1;
693     dop.lba = 1;
694     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
695
696     struct ata_pio_command cmd;
697     memset(&cmd, 0, sizeof(cmd));
698     cmd.command = command;
699
700     return ata_pio_cmd_data(&dop, 0, &cmd);
701 }
702
703 // Extract the ATA/ATAPI version info.
704 static int
705 extract_version(u16 *buffer)
706 {
707     // Extract ATA/ATAPI version.
708     u16 ataversion = buffer[80];
709     u8 version;
710     for (version=15; version>0; version--)
711         if (ataversion & (1<<version))
712             break;
713     return version;
714 }
715
716 #define MAXMODEL 40
717
718 // Extract the ATA/ATAPI model info.
719 static char *
720 extract_model(char *model, u16 *buffer)
721 {
722     // Read model name
723     int i;
724     for (i=0; i<MAXMODEL/2; i++)
725         *(u16*)&model[i*2] = ntohs(buffer[27+i]);
726     model[MAXMODEL] = 0x00;
727
728     // Trim trailing spaces from model name.
729     for (i=MAXMODEL-1; i>0 && model[i] == 0x20; i--)
730         model[i] = 0x00;
731
732     return model;
733 }
734
735 // Common init code between ata and atapi
736 static struct atadrive_s *
737 init_atadrive(struct atadrive_s *dummy, u16 *buffer)
738 {
739     char *desc = malloc_tmp(MAXDESCSIZE);
740     struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g));
741     if (!adrive_g || !desc) {
742         warn_noalloc();
743         free(desc);
744         free(adrive_g);
745         return NULL;
746     }
747     memset(adrive_g, 0, sizeof(*adrive_g));
748     adrive_g->drive.desc = desc;
749     adrive_g->chan_gf = dummy->chan_gf;
750     adrive_g->slave = dummy->slave;
751     adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave;
752     adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
753     return adrive_g;
754 }
755
756 // Detect if the given drive is an atapi - initialize it if so.
757 static struct atadrive_s *
758 init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
759 {
760     // Send an IDENTIFY_DEVICE_PACKET command to device
761     int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
762     if (ret)
763         return NULL;
764
765     // Success - setup as ATAPI.
766     struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
767     if (!adrive_g)
768         return NULL;
769     adrive_g->drive.type = DTYPE_ATAPI;
770     adrive_g->drive.blksize = CDROM_SECTOR_SIZE;
771     adrive_g->drive.sectors = (u64)-1;
772     u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
773     char model[MAXMODEL+1];
774     snprintf(adrive_g->drive.desc, MAXDESCSIZE, "ata%d-%d: %s ATAPI-%d %s"
775              , adrive_g->chan_gf->chanid, adrive_g->slave
776              , extract_model(model, buffer), extract_version(buffer)
777              , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
778     dprintf(1, "%s\n", adrive_g->drive.desc);
779
780     // fill cdidmap
781     if (iscd)
782         map_cd_drive(&adrive_g->drive);
783
784     return adrive_g;
785 }
786
787 // Detect if the given drive is a regular ata drive - initialize it if so.
788 static struct atadrive_s *
789 init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
790 {
791     // Send an IDENTIFY_DEVICE command to device
792     int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
793     if (ret)
794         return NULL;
795
796     // Success - setup as ATA.
797     struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
798     if (!adrive_g)
799         return NULL;
800     adrive_g->drive.type = DTYPE_ATA;
801     adrive_g->drive.blksize = DISK_SECTOR_SIZE;
802
803     adrive_g->drive.pchs.cylinders = buffer[1];
804     adrive_g->drive.pchs.heads = buffer[3];
805     adrive_g->drive.pchs.spt = buffer[6];
806
807     u64 sectors;
808     if (buffer[83] & (1 << 10)) // word 83 - lba48 support
809         sectors = *(u64*)&buffer[100]; // word 100-103
810     else
811         sectors = *(u32*)&buffer[60]; // word 60 and word 61
812     adrive_g->drive.sectors = sectors;
813     u64 adjsize = sectors >> 11;
814     char adjprefix = 'M';
815     if (adjsize >= (1 << 16)) {
816         adjsize >>= 10;
817         adjprefix = 'G';
818     }
819     char model[MAXMODEL+1];
820     snprintf(adrive_g->drive.desc, MAXDESCSIZE
821              , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
822              , adrive_g->chan_gf->chanid, adrive_g->slave
823              , extract_model(model, buffer), extract_version(buffer)
824              , (u32)adjsize, adjprefix);
825     dprintf(1, "%s\n", adrive_g->drive.desc);
826
827     // Setup disk geometry translation.
828     setup_translation(&adrive_g->drive);
829
830     // Register with bcv system.
831     add_bcv_internal(&adrive_g->drive);
832
833     return adrive_g;
834 }
835
836 static u64 SpinupEnd;
837
838 // Wait for non-busy status and check for "floating bus" condition.
839 static int
840 powerup_await_non_bsy(u16 base)
841 {
842     u8 orstatus = 0;
843     u8 status;
844     for (;;) {
845         status = inb(base+ATA_CB_STAT);
846         if (!(status & ATA_CB_STAT_BSY))
847             break;
848         orstatus |= status;
849         if (orstatus == 0xff) {
850             dprintf(4, "powerup IDE floating\n");
851             return orstatus;
852         }
853         if (check_time(SpinupEnd)) {
854             warn_timeout();
855             return -1;
856         }
857         yield();
858     }
859     dprintf(6, "powerup iobase=%x st=%x\n", base, status);
860     return status;
861 }
862
863 // Detect any drives attached to a given controller.
864 static void
865 ata_detect(void *data)
866 {
867     struct ata_channel_s *chan_gf = data;
868     struct atadrive_s dummy;
869     memset(&dummy, 0, sizeof(dummy));
870     dummy.chan_gf = chan_gf;
871     // Device detection
872     int didreset = 0;
873     u8 slave;
874     for (slave=0; slave<=1; slave++) {
875         // Wait for not-bsy.
876         u16 iobase1 = chan_gf->iobase1;
877         int status = powerup_await_non_bsy(iobase1);
878         if (status < 0)
879             continue;
880         u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
881         outb(newdh, iobase1+ATA_CB_DH);
882         ndelay(400);
883         status = powerup_await_non_bsy(iobase1);
884         if (status < 0)
885             continue;
886
887         // Check if ioport registers look valid.
888         outb(newdh, iobase1+ATA_CB_DH);
889         u8 dh = inb(iobase1+ATA_CB_DH);
890         outb(0x55, iobase1+ATA_CB_SC);
891         outb(0xaa, iobase1+ATA_CB_SN);
892         u8 sc = inb(iobase1+ATA_CB_SC);
893         u8 sn = inb(iobase1+ATA_CB_SN);
894         dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
895                 , chan_gf->chanid, slave, sc, sn, dh);
896         if (sc != 0x55 || sn != 0xaa || dh != newdh)
897             continue;
898
899         // Prepare new drive.
900         dummy.slave = slave;
901
902         // reset the channel
903         if (!didreset) {
904             ata_reset(&dummy);
905             didreset = 1;
906         }
907
908         // check for ATAPI
909         u16 buffer[256];
910         struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer);
911         if (!adrive_g) {
912             // Didn't find an ATAPI drive - look for ATA drive.
913             u8 st = inb(iobase1+ATA_CB_STAT);
914             if (!st)
915                 // Status not set - can't be a valid drive.
916                 continue;
917
918             // Wait for RDY.
919             int ret = await_rdy(iobase1);
920             if (ret < 0)
921                 continue;
922
923             // check for ATA.
924             adrive_g = init_drive_ata(&dummy, buffer);
925             if (!adrive_g)
926                 // No ATA drive found
927                 continue;
928         }
929
930         u16 resetresult = buffer[93];
931         dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
932         if (!slave && (resetresult & 0xdf61) == 0x4041)
933             // resetresult looks valid and device 0 is responding to
934             // device 1 requests - device 1 must not be present - skip
935             // detection.
936             break;
937     }
938 }
939
940 // Initialize an ata controller and detect its drives.
941 static void
942 init_controller(int chanid, int bdf, int irq, u32 port1, u32 port2, u32 master)
943 {
944     struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
945     if (!chan_gf) {
946         warn_noalloc();
947         return;
948     }
949     chan_gf->chanid = chanid;
950     chan_gf->irq = irq;
951     chan_gf->pci_bdf = bdf;
952     chan_gf->iobase1 = port1;
953     chan_gf->iobase2 = port2;
954     chan_gf->iomaster = master;
955     dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
956             , chanid, port1, port2, master, irq, bdf);
957     run_thread(ata_detect, chan_gf);
958 }
959
960 #define IRQ_ATA1 14
961 #define IRQ_ATA2 15
962
963 // Locate and init ata controllers.
964 static void
965 ata_init(void)
966 {
967     // Scan PCI bus for ATA adapters
968     int count=0, pcicount=0;
969     int bdf, max;
970     foreachpci(bdf, max) {
971         pcicount++;
972         if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
973             continue;
974
975         u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
976         u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
977         int master = 0;
978         if (CONFIG_ATA_DMA && prog_if & 0x80) {
979             // Check for bus-mastering.
980             u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
981             if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
982                 master = bar & PCI_BASE_ADDRESS_IO_MASK;
983                 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
984             }
985         }
986
987         u32 port1, port2, irq;
988         if (prog_if & 1) {
989             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
990             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
991             irq = pciirq;
992         } else {
993             port1 = PORT_ATA1_CMD_BASE;
994             port2 = PORT_ATA1_CTRL_BASE;
995             irq = IRQ_ATA1;
996         }
997         init_controller(count, bdf, irq, port1, port2, master);
998         count++;
999
1000         if (prog_if & 4) {
1001             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
1002             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
1003             irq = pciirq;
1004         } else {
1005             port1 = PORT_ATA2_CMD_BASE;
1006             port2 = PORT_ATA2_CTRL_BASE;
1007             irq = IRQ_ATA2;
1008         }
1009         init_controller(count, bdf, irq, port1, port2, master ? master + 8 : 0);
1010         count++;
1011     }
1012
1013     if (!CONFIG_COREBOOT && !pcicount) {
1014         // No PCI devices found - probably a QEMU "-M isapc" machine.
1015         // Try using ISA ports for ATA controllers.
1016         init_controller(0, -1, IRQ_ATA1
1017                         , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
1018         init_controller(1, -1, IRQ_ATA2
1019                         , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
1020     }
1021 }
1022
1023 void
1024 ata_setup(void)
1025 {
1026     ASSERT32FLAT();
1027     if (!CONFIG_ATA)
1028         return;
1029
1030     dprintf(3, "init hard drives\n");
1031
1032     SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
1033     ata_init();
1034
1035     SET_BDA(disk_control_byte, 0xc0);
1036
1037     enable_hwirq(14, entry_76);
1038 }