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