1 #include "devices/ide.h"
6 #include "devices/block.h"
7 #include "devices/partition.h"
8 #include "devices/timer.h"
9 #include "threads/io.h"
10 #include "threads/interrupt.h"
11 #include "threads/synch.h"
13 /* The code in this file is an interface to an ATA (IDE)
14 controller. It attempts to comply to [ATA-3]. */
16 /* ATA command block port addresses. */
17 #define reg_data(CHANNEL) ((CHANNEL)->reg_base + 0) /* Data. */
18 #define reg_error(CHANNEL) ((CHANNEL)->reg_base + 1) /* Error. */
19 #define reg_nsect(CHANNEL) ((CHANNEL)->reg_base + 2) /* Sector Count. */
20 #define reg_lbal(CHANNEL) ((CHANNEL)->reg_base + 3) /* LBA 0:7. */
21 #define reg_lbam(CHANNEL) ((CHANNEL)->reg_base + 4) /* LBA 15:8. */
22 #define reg_lbah(CHANNEL) ((CHANNEL)->reg_base + 5) /* LBA 23:16. */
23 #define reg_device(CHANNEL) ((CHANNEL)->reg_base + 6) /* Device/LBA 27:24. */
24 #define reg_status(CHANNEL) ((CHANNEL)->reg_base + 7) /* Status (r/o). */
25 #define reg_command(CHANNEL) reg_status (CHANNEL) /* Command (w/o). */
27 /* ATA control block port addresses.
28 (If we supported non-legacy ATA controllers this would not be
29 flexible enough, but it's fine for what we do.) */
30 #define reg_ctl(CHANNEL) ((CHANNEL)->reg_base + 0x206) /* Control (w/o). */
31 #define reg_alt_status(CHANNEL) reg_ctl (CHANNEL) /* Alt Status (r/o). */
33 /* Alternate Status Register bits. */
34 #define STA_BSY 0x80 /* Busy. */
35 #define STA_DRDY 0x40 /* Device Ready. */
36 #define STA_DRQ 0x08 /* Data Request. */
38 /* Control Register bits. */
39 #define CTL_SRST 0x04 /* Software Reset. */
41 /* Device Register bits. */
42 #define DEV_MBS 0xa0 /* Must be set. */
43 #define DEV_LBA 0x40 /* Linear based addressing. */
44 #define DEV_DEV 0x10 /* Select device: 0=master, 1=slave. */
47 Many more are defined but this is the small subset that we
49 #define CMD_IDENTIFY_DEVICE 0xec /* IDENTIFY DEVICE. */
50 #define CMD_READ_SECTOR_RETRY 0x20 /* READ SECTOR with retries. */
51 #define CMD_WRITE_SECTOR_RETRY 0x30 /* WRITE SECTOR with retries. */
56 char name[8]; /* Name, e.g. "hda". */
57 struct channel *channel; /* Channel that disk is attached to. */
58 int dev_no; /* Device 0 or 1 for master or slave. */
59 bool is_ata; /* Is device an ATA disk? */
62 /* An ATA channel (aka controller).
63 Each channel can control up to two disks. */
66 char name[8]; /* Name, e.g. "ide0". */
67 uint16_t reg_base; /* Base I/O port. */
68 uint8_t irq; /* Interrupt in use. */
70 struct lock lock; /* Must acquire to access the controller. */
71 bool expecting_interrupt; /* True if an interrupt is expected, false if
72 any interrupt would be spurious. */
73 struct semaphore completion_wait; /* Up'd by interrupt handler. */
75 struct ata_disk devices[2]; /* The devices on this channel. */
78 /* We support the two "legacy" ATA channels found in a standard PC. */
80 static struct channel channels[CHANNEL_CNT];
82 static struct block_operations ide_operations;
84 static void reset_channel (struct channel *);
85 static bool check_device_type (struct ata_disk *);
86 static void identify_ata_device (struct ata_disk *);
88 static void select_sector (struct ata_disk *, block_sector_t);
89 static void issue_pio_command (struct channel *, uint8_t command);
90 static void input_sector (struct channel *, void *);
91 static void output_sector (struct channel *, const void *);
93 static void wait_until_idle (const struct ata_disk *);
94 static bool wait_while_busy (const struct ata_disk *);
95 static void select_device (const struct ata_disk *);
96 static void select_device_wait (const struct ata_disk *);
98 static void interrupt_handler (struct intr_frame *);
100 /* Initialize the disk subsystem and detect disks. */
106 for (chan_no = 0; chan_no < CHANNEL_CNT; chan_no++)
108 struct channel *c = &channels[chan_no];
111 /* Initialize channel. */
112 snprintf (c->name, sizeof c->name, "ide%zu", chan_no);
126 lock_init (&c->lock);
127 c->expecting_interrupt = false;
128 sema_init (&c->completion_wait, 0);
130 /* Initialize devices. */
131 for (dev_no = 0; dev_no < 2; dev_no++)
133 struct ata_disk *d = &c->devices[dev_no];
134 snprintf (d->name, sizeof d->name,
135 "hd%c", 'a' + chan_no * 2 + dev_no);
141 /* Register interrupt handler. */
142 intr_register_ext (c->irq, interrupt_handler, c->name);
144 /* Reset hardware. */
147 /* Distinguish ATA hard disks from other devices. */
148 if (check_device_type (&c->devices[0]))
149 check_device_type (&c->devices[1]);
151 /* Read hard disk identity information. */
152 for (dev_no = 0; dev_no < 2; dev_no++)
153 if (c->devices[dev_no].is_ata)
154 identify_ata_device (&c->devices[dev_no]);
158 /* Disk detection and identification. */
160 static char *descramble_ata_string (char *, int size);
162 /* Resets an ATA channel and waits for any devices present on it
163 to finish the reset. */
165 reset_channel (struct channel *c)
170 /* The ATA reset sequence depends on which devices are present,
171 so we start by detecting device presence. */
172 for (dev_no = 0; dev_no < 2; dev_no++)
174 struct ata_disk *d = &c->devices[dev_no];
178 outb (reg_nsect (c), 0x55);
179 outb (reg_lbal (c), 0xaa);
181 outb (reg_nsect (c), 0xaa);
182 outb (reg_lbal (c), 0x55);
184 outb (reg_nsect (c), 0x55);
185 outb (reg_lbal (c), 0xaa);
187 present[dev_no] = (inb (reg_nsect (c)) == 0x55
188 && inb (reg_lbal (c)) == 0xaa);
191 /* Issue soft reset sequence, which selects device 0 as a side effect.
192 Also enable interrupts. */
193 outb (reg_ctl (c), 0);
195 outb (reg_ctl (c), CTL_SRST);
197 outb (reg_ctl (c), 0);
201 /* Wait for device 0 to clear BSY. */
204 select_device (&c->devices[0]);
205 wait_while_busy (&c->devices[0]);
208 /* Wait for device 1 to clear BSY. */
213 select_device (&c->devices[1]);
214 for (i = 0; i < 3000; i++)
216 if (inb (reg_nsect (c)) == 1 && inb (reg_lbal (c)) == 1)
220 wait_while_busy (&c->devices[1]);
224 /* Checks whether device D is an ATA disk and sets D's is_ata
225 member appropriately. If D is device 0 (master), returns true
226 if it's possible that a slave (device 1) exists on this
227 channel. If D is device 1 (slave), the return value is not
230 check_device_type (struct ata_disk *d)
232 struct channel *c = d->channel;
233 uint8_t error, lbam, lbah, status;
237 error = inb (reg_error (c));
238 lbam = inb (reg_lbam (c));
239 lbah = inb (reg_lbah (c));
240 status = inb (reg_status (c));
242 if ((error != 1 && (error != 0x81 || d->dev_no == 1))
243 || (status & STA_DRDY) == 0
244 || (status & STA_BSY) != 0)
247 return error != 0x81;
251 d->is_ata = (lbam == 0 && lbah == 0) || (lbam == 0x3c && lbah == 0xc3);
256 /* Sends an IDENTIFY DEVICE command to disk D and reads the
257 response. Registers the disk with the block device
260 identify_ata_device (struct ata_disk *d)
262 struct channel *c = d->channel;
263 char id[BLOCK_SECTOR_SIZE];
264 block_sector_t capacity;
265 char *model, *serial;
266 char extra_info[128];
271 /* Send the IDENTIFY DEVICE command, wait for an interrupt
272 indicating the device's response is ready, and read the data
274 select_device_wait (d);
275 issue_pio_command (c, CMD_IDENTIFY_DEVICE);
276 sema_down (&c->completion_wait);
277 if (!wait_while_busy (d))
282 input_sector (c, id);
284 /* Calculate capacity.
285 Read model name and serial number. */
286 capacity = *(uint32_t *) &id[60 * 2];
287 model = descramble_ata_string (&id[10 * 2], 20);
288 serial = descramble_ata_string (&id[27 * 2], 40);
289 snprintf (extra_info, sizeof extra_info,
290 "model \"%s\", serial \"%s\"", model, serial);
292 /* Disable access to IDE disks over 1 GB, which are likely
293 physical IDE disks rather than virtual ones. If we don't
294 allow access to those, we're less likely to scribble on
295 someone's important data. You can disable this check by
296 hand if you really want to do so. */
297 if (capacity >= 1024 * 1024 * 1024 / BLOCK_SECTOR_SIZE)
299 printf ("%s: ignoring ", d->name);
300 print_human_readable_size (capacity * 512);
301 printf ("disk for safety\n");
307 block = block_register (d->name, BLOCK_RAW, extra_info, capacity,
309 partition_scan (block);
312 /* Translates STRING, which consists of SIZE bytes in a funky
313 format, into a null-terminated string in-place. Drops
314 trailing whitespace and null bytes. Returns STRING. */
316 descramble_ata_string (char *string, int size)
320 /* Swap all pairs of bytes. */
321 for (i = 0; i + 1 < size; i += 2)
323 char tmp = string[i];
324 string[i] = string[i + 1];
328 /* Find the last non-white, non-null character. */
329 for (size--; size > 0; size--)
331 int c = string[size - 1];
332 if (c != '\0' && !isspace (c))
340 /* Reads sector SEC_NO from disk D into BUFFER, which must have
341 room for BLOCK_SECTOR_SIZE bytes.
342 Internally synchronizes accesses to disks, so external
343 per-disk locking is unneeded. */
345 ide_read (void *d_, block_sector_t sec_no, void *buffer)
347 struct ata_disk *d = d_;
348 struct channel *c = d->channel;
349 lock_acquire (&c->lock);
350 select_sector (d, sec_no);
351 issue_pio_command (c, CMD_READ_SECTOR_RETRY);
352 sema_down (&c->completion_wait);
353 if (!wait_while_busy (d))
354 PANIC ("%s: disk read failed, sector=%"PRDSNu, d->name, sec_no);
355 input_sector (c, buffer);
356 lock_release (&c->lock);
359 /* Write sector SEC_NO to disk D from BUFFER, which must contain
360 BLOCK_SECTOR_SIZE bytes. Returns after the disk has
361 acknowledged receiving the data.
362 Internally synchronizes accesses to disks, so external
363 per-disk locking is unneeded. */
365 ide_write (void *d_, block_sector_t sec_no, const void *buffer)
367 struct ata_disk *d = d_;
368 struct channel *c = d->channel;
369 lock_acquire (&c->lock);
370 select_sector (d, sec_no);
371 issue_pio_command (c, CMD_WRITE_SECTOR_RETRY);
372 if (!wait_while_busy (d))
373 PANIC ("%s: disk write failed, sector=%"PRDSNu, d->name, sec_no);
374 output_sector (c, buffer);
375 sema_down (&c->completion_wait);
376 lock_release (&c->lock);
379 static struct block_operations ide_operations =
385 /* Selects device D, waiting for it to become ready, and then
386 writes SEC_NO to the disk's sector selection registers. (We
389 select_sector (struct ata_disk *d, block_sector_t sec_no)
391 struct channel *c = d->channel;
393 ASSERT (sec_no < (1UL << 28));
395 select_device_wait (d);
396 outb (reg_nsect (c), 1);
397 outb (reg_lbal (c), sec_no);
398 outb (reg_lbam (c), sec_no >> 8);
399 outb (reg_lbah (c), (sec_no >> 16));
400 outb (reg_device (c),
401 DEV_MBS | DEV_LBA | (d->dev_no == 1 ? DEV_DEV : 0) | (sec_no >> 24));
404 /* Writes COMMAND to channel C and prepares for receiving a
405 completion interrupt. */
407 issue_pio_command (struct channel *c, uint8_t command)
409 /* Interrupts must be enabled or our semaphore will never be
410 up'd by the completion handler. */
411 ASSERT (intr_get_level () == INTR_ON);
413 c->expecting_interrupt = true;
414 outb (reg_command (c), command);
417 /* Reads a sector from channel C's data register in PIO mode into
418 SECTOR, which must have room for BLOCK_SECTOR_SIZE bytes. */
420 input_sector (struct channel *c, void *sector)
422 insw (reg_data (c), sector, BLOCK_SECTOR_SIZE / 2);
425 /* Writes SECTOR to channel C's data register in PIO mode.
426 SECTOR must contain BLOCK_SECTOR_SIZE bytes. */
428 output_sector (struct channel *c, const void *sector)
430 outsw (reg_data (c), sector, BLOCK_SECTOR_SIZE / 2);
433 /* Low-level ATA primitives. */
435 /* Wait up to 10 seconds for the controller to become idle, that
436 is, for the BSY and DRQ bits to clear in the status register.
438 As a side effect, reading the status register clears any
439 pending interrupt. */
441 wait_until_idle (const struct ata_disk *d)
445 for (i = 0; i < 1000; i++)
447 if ((inb (reg_status (d->channel)) & (STA_BSY | STA_DRQ)) == 0)
452 printf ("%s: idle timeout\n", d->name);
455 /* Wait up to 30 seconds for disk D to clear BSY,
456 and then return the status of the DRQ bit.
457 The ATA standards say that a disk may take as long as that to
458 complete its reset. */
460 wait_while_busy (const struct ata_disk *d)
462 struct channel *c = d->channel;
465 for (i = 0; i < 3000; i++)
468 printf ("%s: busy, waiting...", d->name);
469 if (!(inb (reg_alt_status (c)) & STA_BSY))
473 return (inb (reg_alt_status (c)) & STA_DRQ) != 0;
482 /* Program D's channel so that D is now the selected disk. */
484 select_device (const struct ata_disk *d)
486 struct channel *c = d->channel;
487 uint8_t dev = DEV_MBS;
490 outb (reg_device (c), dev);
491 inb (reg_alt_status (c));
495 /* Select disk D in its channel, as select_device(), but wait for
496 the channel to become idle before and after. */
498 select_device_wait (const struct ata_disk *d)
505 /* ATA interrupt handler. */
507 interrupt_handler (struct intr_frame *f)
511 for (c = channels; c < channels + CHANNEL_CNT; c++)
512 if (f->vec_no == c->irq)
514 if (c->expecting_interrupt)
516 inb (reg_status (c)); /* Acknowledge interrupt. */
517 sema_up (&c->completion_wait); /* Wake up waiter. */
520 printf ("%s: unexpected interrupt\n", c->name);