Replace irq_enable() regions with explicit calls to check for irqs.
[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
21 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
22
23 struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
24
25
26 /****************************************************************
27  * Helper functions
28  ****************************************************************/
29
30 // Wait for the specified ide state
31 static inline int
32 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
33 {
34     u64 end = calc_future_tsc(timeout);
35     for (;;) {
36         u8 status = inb(base+ATA_CB_STAT);
37         if ((status & mask) == flags)
38             return status;
39         if (check_time(end)) {
40             dprintf(1, "IDE time out\n");
41             return -1;
42         }
43         yield();
44     }
45 }
46
47 // Wait for the device to be not-busy.
48 static int
49 await_not_bsy(u16 base)
50 {
51     return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
52 }
53
54 // Wait for the device to be ready.
55 static int
56 await_rdy(u16 base)
57 {
58     return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
59 }
60
61 // Wait for ide state - pauses for one ata cycle first.
62 static inline int
63 pause_await_not_bsy(u16 iobase1, u16 iobase2)
64 {
65     // Wait one PIO transfer cycle.
66     inb(iobase2 + ATA_CB_ASTAT);
67
68     return await_not_bsy(iobase1);
69 }
70
71 // Wait for ide state - pause for 400ns first.
72 static inline int
73 ndelay_await_not_bsy(u16 iobase1)
74 {
75     ndelay(400);
76     return await_not_bsy(iobase1);
77 }
78
79 // Reset a drive
80 static void
81 ata_reset(struct drive_s *drive_g)
82 {
83     u8 ataid = GET_GLOBAL(drive_g->cntl_id);
84     u8 channel = ataid / 2;
85     u8 slave = ataid % 2;
86     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
87     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
88
89     dprintf(6, "ata_reset drive=%p\n", drive_g);
90     // Pulse SRST
91     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
92     udelay(5);
93     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
94     msleep(2);
95
96     // wait for device to become not busy.
97     int status = await_not_bsy(iobase1);
98     if (status < 0)
99         goto done;
100     if (slave) {
101         // Change device.
102         u64 end = calc_future_tsc(IDE_TIMEOUT);
103         for (;;) {
104             outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
105             status = ndelay_await_not_bsy(iobase1);
106             if (status < 0)
107                 goto done;
108             if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
109                 break;
110             // Change drive request failed to take effect - retry.
111             if (check_time(end)) {
112                 dprintf(1, "ata_reset slave time out\n");
113                 goto done;
114             }
115         }
116     } else {
117         // QEMU doesn't reset dh on reset, so set it explicitly.
118         outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
119     }
120
121     // On a user-reset request, wait for RDY if it is an ATA device.
122     u8 type=GET_GLOBAL(drive_g->type);
123     if (type == DTYPE_ATA)
124         status = await_rdy(iobase1);
125
126 done:
127     // Enable interrupts
128     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
129
130     dprintf(6, "ata_reset exit status=%x\n", status);
131 }
132
133 static int
134 isready(struct drive_s *drive_g)
135 {
136     // Read the status from controller
137     u8 ataid = GET_GLOBAL(drive_g->cntl_id);
138     u8 channel = ataid / 2;
139     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
140     u8 status = inb(iobase1 + ATA_CB_STAT);
141     if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
142         return DISK_RET_SUCCESS;
143     return DISK_RET_ENOTREADY;
144 }
145
146 static int
147 process_ata_misc_op(struct disk_op_s *op)
148 {
149     if (!CONFIG_ATA)
150         return 0;
151
152     switch (op->command) {
153     case CMD_RESET:
154         ata_reset(op->drive_g);
155         return DISK_RET_SUCCESS;
156     case CMD_ISREADY:
157         return isready(op->drive_g);
158     case CMD_FORMAT:
159     case CMD_VERIFY:
160     case CMD_SEEK:
161         return DISK_RET_SUCCESS;
162     default:
163         op->count = 0;
164         return DISK_RET_EPARAM;
165     }
166 }
167
168
169 /****************************************************************
170  * ATA send command
171  ****************************************************************/
172
173 struct ata_pio_command {
174     u8 feature;
175     u8 sector_count;
176     u8 lba_low;
177     u8 lba_mid;
178     u8 lba_high;
179     u8 device;
180     u8 command;
181
182     u8 sector_count2;
183     u8 lba_low2;
184     u8 lba_mid2;
185     u8 lba_high2;
186 };
187
188 // Send an ata command to the drive.
189 static int
190 send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
191 {
192     u8 ataid = GET_GLOBAL(drive_g->cntl_id);
193     u8 channel = ataid / 2;
194     u8 slave = ataid % 2;
195     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
196     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
197
198     // Disable interrupts
199     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
200
201     // Select device
202     int status = await_not_bsy(iobase1);
203     if (status < 0)
204         return status;
205     u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
206                 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
207     u8 olddh = inb(iobase1 + ATA_CB_DH);
208     outb(newdh, iobase1 + ATA_CB_DH);
209     if ((olddh ^ newdh) & (1<<4)) {
210         // Was a device change - wait for device to become not busy.
211         status = ndelay_await_not_bsy(iobase1);
212         if (status < 0)
213             return status;
214     }
215
216     if (cmd->command & 0x04) {
217         outb(0x00, iobase1 + ATA_CB_FR);
218         outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
219         outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
220         outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
221         outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
222     }
223     outb(cmd->feature, iobase1 + ATA_CB_FR);
224     outb(cmd->sector_count, iobase1 + ATA_CB_SC);
225     outb(cmd->lba_low, iobase1 + ATA_CB_SN);
226     outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
227     outb(cmd->lba_high, iobase1 + ATA_CB_CH);
228     outb(cmd->command, iobase1 + ATA_CB_CMD);
229
230     status = ndelay_await_not_bsy(iobase1);
231     if (status < 0)
232         return status;
233
234     if (status & ATA_CB_STAT_ERR) {
235         dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
236                 , status, inb(iobase1 + ATA_CB_ERR));
237         return -4;
238     }
239     if (!(status & ATA_CB_STAT_DRQ)) {
240         dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
241         return -5;
242     }
243
244     return 0;
245 }
246
247
248 /****************************************************************
249  * ATA transfers
250  ****************************************************************/
251
252 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
253 // 'op->drive_g'.
254 static int
255 ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
256 {
257     dprintf(16, "ata_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
258             , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
259
260     u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
261     u8 channel = ataid / 2;
262     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
263     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
264     int count = op->count;
265     void *buf_fl = op->buf_fl;
266     int status;
267     for (;;) {
268         if (iswrite) {
269             // Write data to controller
270             dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
271             if (CONFIG_ATA_PIO32)
272                 outsl_fl(iobase1, buf_fl, blocksize / 4);
273             else
274                 outsw_fl(iobase1, buf_fl, blocksize / 2);
275         } else {
276             // Read data from controller
277             dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
278             if (CONFIG_ATA_PIO32)
279                 insl_fl(iobase1, buf_fl, blocksize / 4);
280             else
281                 insw_fl(iobase1, buf_fl, blocksize / 2);
282         }
283         buf_fl += blocksize;
284
285         status = pause_await_not_bsy(iobase1, iobase2);
286         if (status < 0) {
287             // Error
288             op->count -= count;
289             return status;
290         }
291
292         count--;
293         if (!count)
294             break;
295         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
296         if (status != ATA_CB_STAT_DRQ) {
297             dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
298                     , status);
299             op->count -= count;
300             return -6;
301         }
302     }
303
304     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
305                | ATA_CB_STAT_ERR);
306     if (!iswrite)
307         status &= ~ATA_CB_STAT_DF;
308     if (status != 0) {
309         dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
310         return -7;
311     }
312
313     // Enable interrupts
314     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
315     return 0;
316 }
317
318
319 /****************************************************************
320  * ATA hard drive functions
321  ****************************************************************/
322
323 // Read/write count blocks from a harddrive.
324 static int
325 ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
326 {
327     u64 lba = op->lba;
328
329     struct ata_pio_command cmd;
330     memset(&cmd, 0, sizeof(cmd));
331
332     cmd.command = command;
333     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
334         cmd.sector_count2 = op->count >> 8;
335         cmd.lba_low2 = lba >> 24;
336         cmd.lba_mid2 = lba >> 32;
337         cmd.lba_high2 = lba >> 40;
338
339         cmd.command |= 0x04;
340         lba &= 0xffffff;
341     }
342
343     cmd.feature = 0;
344     cmd.sector_count = op->count;
345     cmd.lba_low = lba;
346     cmd.lba_mid = lba >> 8;
347     cmd.lba_high = lba >> 16;
348     cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
349
350     int ret = send_cmd(op->drive_g, &cmd);
351     if (ret)
352         return ret;
353     return ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
354 }
355
356 int
357 process_ata_op(struct disk_op_s *op)
358 {
359     if (!CONFIG_ATA)
360         return 0;
361
362     int ret;
363     switch (op->command) {
364     case CMD_READ:
365         ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
366         break;
367     case CMD_WRITE:
368         ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
369         break;
370     default:
371         return process_ata_misc_op(op);
372     }
373     if (ret)
374         return DISK_RET_EBADTRACK;
375     return DISK_RET_SUCCESS;
376 }
377
378
379 /****************************************************************
380  * ATAPI functions
381  ****************************************************************/
382
383 // Low-level atapi command transmit function.
384 static int
385 send_atapi_cmd(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
386 {
387     u8 ataid = GET_GLOBAL(drive_g->cntl_id);
388     u8 channel = ataid / 2;
389     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
390     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
391
392     struct ata_pio_command cmd;
393     cmd.sector_count = 0;
394     cmd.feature = 0;
395     cmd.lba_low = 0;
396     cmd.lba_mid = blocksize;
397     cmd.lba_high = blocksize >> 8;
398     cmd.device = 0;
399     cmd.command = ATA_CMD_PACKET;
400
401     int ret = send_cmd(drive_g, &cmd);
402     if (ret)
403         return ret;
404
405     // Send command to device
406     outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
407
408     int status = pause_await_not_bsy(iobase1, iobase2);
409     if (status < 0)
410         return status;
411
412     if (status & ATA_CB_STAT_ERR) {
413         u8 err = inb(iobase1 + ATA_CB_ERR);
414         // skip "Not Ready"
415         if (err != 0x20)
416             dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
417                     , status, err);
418         return -2;
419     }
420     if (!(status & ATA_CB_STAT_DRQ)) {
421         dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
422         return -3;
423     }
424
425     return 0;
426 }
427
428 // Read sectors from the cdrom.
429 int
430 cdrom_read(struct disk_op_s *op)
431 {
432     u8 atacmd[12];
433     memset(atacmd, 0, sizeof(atacmd));
434     atacmd[0]=0x28;                         // READ command
435     atacmd[7]=(op->count & 0xff00) >> 8;    // Sectors
436     atacmd[8]=(op->count & 0x00ff);
437     atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
438     atacmd[3]=(op->lba & 0x00ff0000) >> 16;
439     atacmd[4]=(op->lba & 0x0000ff00) >> 8;
440     atacmd[5]=(op->lba & 0x000000ff);
441
442     int ret = send_atapi_cmd(op->drive_g, atacmd, sizeof(atacmd)
443                              , CDROM_SECTOR_SIZE);
444     if (ret)
445         return ret;
446
447     return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
448 }
449
450 int
451 process_atapi_op(struct disk_op_s *op)
452 {
453     int ret;
454     switch (op->command) {
455     case CMD_READ:
456         ret = cdrom_read(op);
457         break;
458     case CMD_FORMAT:
459     case CMD_WRITE:
460         return DISK_RET_EWRITEPROTECT;
461     default:
462         return process_ata_misc_op(op);
463     }
464     if (ret)
465         return DISK_RET_EBADTRACK;
466     return DISK_RET_SUCCESS;
467 }
468
469 // Send a simple atapi command to a drive.
470 int
471 ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
472                , u32 length, void *buf_fl)
473 {
474     int ret = send_atapi_cmd(drive_g, cmdbuf, cmdlen, length);
475     if (ret)
476         return ret;
477
478     struct disk_op_s dop;
479     memset(&dop, 0, sizeof(dop));
480     dop.drive_g = drive_g;
481     dop.count = 1;
482     dop.buf_fl = buf_fl;
483
484     return ata_transfer(&dop, 0, length);
485 }
486
487
488 /****************************************************************
489  * ATA detect and init
490  ****************************************************************/
491
492 // Extract the ATA/ATAPI version info.
493 static int
494 extract_version(u16 *buffer)
495 {
496     // Extract ATA/ATAPI version.
497     u16 ataversion = buffer[80];
498     u8 version;
499     for (version=15; version>0; version--)
500         if (ataversion & (1<<version))
501             break;
502     return version;
503 }
504
505 // Extract common information from IDENTIFY commands.
506 static void
507 extract_identify(struct drive_s *drive_g, u16 *buffer)
508 {
509     dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
510
511     // Read model name
512     char *model = drive_g->model;
513     int maxsize = ARRAY_SIZE(drive_g->model);
514     int i;
515     for (i=0; i<maxsize/2; i++) {
516         u16 v = buffer[27+i];
517         model[i*2] = v >> 8;
518         model[i*2+1] = v & 0xff;
519     }
520     model[maxsize-1] = 0x00;
521
522     // Trim trailing spaces from model name.
523     for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
524         model[i] = 0x00;
525
526     // Common flags.
527     SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
528     SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
529 }
530
531 void
532 describe_atapi(struct drive_s *drive_g)
533 {
534     u8 ataid = drive_g->cntl_id;
535     u8 channel = ataid / 2;
536     u8 slave = ataid % 2;
537     u8 version = drive_g->cntl_info;
538     int iscd = drive_g->floppy_type;
539     printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
540            , drive_g->model, version
541            , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
542 }
543
544 static struct drive_s *
545 init_drive_atapi(struct drive_s *dummy, u16 *buffer)
546 {
547     // Send an IDENTIFY_DEVICE_PACKET command to device
548     memset(buffer, 0, DISK_SECTOR_SIZE);
549     struct disk_op_s dop;
550     memset(&dop, 0, sizeof(dop));
551     dop.drive_g = dummy;
552     dop.count = 1;
553     dop.lba = 1;
554     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
555     int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
556     if (ret)
557         return NULL;
558
559     // Success - setup as ATAPI.
560     struct drive_s *drive_g = allocDrive();
561     if (! drive_g)
562         return NULL;
563     SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
564     extract_identify(drive_g, buffer);
565     SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
566     SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
567     SET_GLOBAL(drive_g->sectors, (u64)-1);
568     u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
569     SET_GLOBAL(drive_g->floppy_type, iscd);
570
571     // fill cdidmap
572     if (iscd)
573         map_cd_drive(drive_g);
574
575     return drive_g;
576 }
577
578 void
579 describe_ata(struct drive_s *drive_g)
580 {
581     u8 ataid = drive_g->cntl_id;
582     u8 channel = ataid / 2;
583     u8 slave = ataid % 2;
584     u64 sectors = drive_g->sectors;
585     u8 version = drive_g->cntl_info;
586     char *model = drive_g->model;
587     printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
588     u64 sizeinmb = sectors >> 11;
589     if (sizeinmb < (1 << 16))
590         printf(" (%u MiBytes)", (u32)sizeinmb);
591     else
592         printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
593 }
594
595 static struct drive_s *
596 init_drive_ata(struct drive_s *dummy, u16 *buffer)
597 {
598     // Send an IDENTIFY_DEVICE command to device
599     memset(buffer, 0, DISK_SECTOR_SIZE);
600     struct disk_op_s dop;
601     memset(&dop, 0, sizeof(dop));
602     dop.drive_g = dummy;
603     dop.count = 1;
604     dop.lba = 1;
605     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
606     int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
607     if (ret)
608         return NULL;
609
610     // Success - setup as ATA.
611     struct drive_s *drive_g = allocDrive();
612     if (! drive_g)
613         return NULL;
614     SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
615     extract_identify(drive_g, buffer);
616     SET_GLOBAL(drive_g->type, DTYPE_ATA);
617     SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
618
619     SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
620     SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
621     SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
622
623     u64 sectors;
624     if (buffer[83] & (1 << 10)) // word 83 - lba48 support
625         sectors = *(u64*)&buffer[100]; // word 100-103
626     else
627         sectors = *(u32*)&buffer[60]; // word 60 and word 61
628     SET_GLOBAL(drive_g->sectors, sectors);
629
630     // Setup disk geometry translation.
631     setup_translation(drive_g);
632
633     // Register with bcv system.
634     add_bcv_internal(drive_g);
635
636     return drive_g;
637 }
638
639 static int
640 powerup_await_non_bsy(u16 base, u64 end)
641 {
642     u8 orstatus = 0;
643     u8 status;
644     for (;;) {
645         status = inb(base+ATA_CB_STAT);
646         if (!(status & ATA_CB_STAT_BSY))
647             break;
648         orstatus |= status;
649         if (orstatus == 0xff) {
650             dprintf(1, "powerup IDE floating\n");
651             return orstatus;
652         }
653         if (check_time(end)) {
654             dprintf(1, "powerup IDE time out\n");
655             return -1;
656         }
657         yield();
658     }
659     dprintf(6, "powerup iobase=%x st=%x\n", base, status);
660     return status;
661 }
662
663 static void
664 ata_detect()
665 {
666     struct drive_s dummy;
667     memset(&dummy, 0, sizeof(dummy));
668     // Device detection
669     u64 end = calc_future_tsc(IDE_TIMEOUT);
670     int ataid, last_reset_ataid=-1;
671     for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
672         u8 channel = ataid / 2;
673         u8 slave = ataid % 2;
674
675         u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
676         if (!iobase1)
677             break;
678
679         // Wait for not-bsy.
680         int status = powerup_await_non_bsy(iobase1, end);
681         if (status < 0)
682             continue;
683         u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
684         outb(newdh, iobase1+ATA_CB_DH);
685         ndelay(400);
686         status = powerup_await_non_bsy(iobase1, end);
687         if (status < 0)
688             continue;
689
690         // Check if ioport registers look valid.
691         outb(newdh, iobase1+ATA_CB_DH);
692         u8 dh = inb(iobase1+ATA_CB_DH);
693         outb(0x55, iobase1+ATA_CB_SC);
694         outb(0xaa, iobase1+ATA_CB_SN);
695         u8 sc = inb(iobase1+ATA_CB_SC);
696         u8 sn = inb(iobase1+ATA_CB_SN);
697         dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
698                 , ataid, sc, sn, dh);
699         if (sc != 0x55 || sn != 0xaa || dh != newdh)
700             continue;
701
702         // Prepare new drive.
703         dummy.cntl_id = ataid;
704
705         // reset the channel
706         if (slave && ataid == last_reset_ataid + 1) {
707             // The drive was just reset - no need to reset it again.
708         } else {
709             ata_reset(&dummy);
710             last_reset_ataid = ataid;
711         }
712
713         // check for ATAPI
714         u16 buffer[256];
715         struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
716         if (!drive_g) {
717             // Didn't find an ATAPI drive - look for ATA drive.
718             u8 st = inb(iobase1+ATA_CB_STAT);
719             if (!st)
720                 // Status not set - can't be a valid drive.
721                 continue;
722
723             // Wait for RDY.
724             int ret = await_rdy(iobase1);
725             if (ret < 0)
726                 continue;
727
728             // check for ATA.
729             drive_g = init_drive_ata(&dummy, buffer);
730             if (!drive_g)
731                 // No ATA drive found
732                 continue;
733         }
734
735         // Report drive info to user.
736         describe_drive(drive_g);
737         printf("\n");
738
739         u16 resetresult = buffer[93];
740         dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
741         if (!slave && (resetresult & 0xdf61) == 0x4041)
742             // resetresult looks valid and device 0 is responding to
743             // device 1 requests - device 1 must not be present - skip
744             // detection.
745             ataid++;
746     }
747
748     printf("\n");
749 }
750
751 static void
752 ata_init()
753 {
754     // Scan PCI bus for ATA adapters
755     int count=0;
756     int bdf, max;
757     foreachpci(bdf, max) {
758         if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
759             continue;
760         if (count >= ARRAY_SIZE(ATA_channels))
761             break;
762
763         u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
764         SET_GLOBAL(ATA_channels[count].irq, irq);
765         SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
766
767         u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
768         u32 port1, port2;
769
770         if (prog_if & 1) {
771             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
772             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
773         } else {
774             port1 = 0x1f0;
775             port2 = 0x3f0;
776         }
777         SET_GLOBAL(ATA_channels[count].iobase1, port1);
778         SET_GLOBAL(ATA_channels[count].iobase2, port2);
779         dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
780                 , count, port1, port2, bdf, prog_if);
781         count++;
782
783         if (prog_if & 4) {
784             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
785             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
786         } else {
787             port1 = 0x170;
788             port2 = 0x370;
789         }
790         dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
791                 , count, port1, port2, bdf, prog_if);
792         SET_GLOBAL(ATA_channels[count].iobase1, port1);
793         SET_GLOBAL(ATA_channels[count].iobase2, port2);
794         count++;
795     }
796 }
797
798 void
799 ata_setup()
800 {
801     if (!CONFIG_ATA)
802         return;
803
804     dprintf(3, "init hard drives\n");
805     ata_init();
806     ata_detect();
807
808     SET_BDA(disk_control_byte, 0xc0);
809
810     enable_hwirq(14, entry_76);
811 }