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