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