10 /* ATA command block port addresses. */
11 #define reg_data(CHANNEL) ((CHANNEL)->reg_base + 0) /* Data. */
12 #define reg_error(CHANNEL) ((CHANNEL)->reg_base + 1) /* Error. */
13 #define reg_nsect(CHANNEL) ((CHANNEL)->reg_base + 2) /* Sector Count. */
14 #define reg_lbal(CHANNEL) ((CHANNEL)->reg_base + 3) /* LBA 0:7. */
15 #define reg_lbam(CHANNEL) ((CHANNEL)->reg_base + 4) /* LBA 15:8. */
16 #define reg_lbah(CHANNEL) ((CHANNEL)->reg_base + 5) /* LBA 23:16. */
17 #define reg_device(CHANNEL) ((CHANNEL)->reg_base + 6) /* Device/LBA 27:24. */
18 #define reg_status(CHANNEL) ((CHANNEL)->reg_base + 7) /* Status (r/o). */
19 #define reg_command(CHANNEL) reg_status (CHANNEL) /* Command (w/o). */
21 /* ATA control block port addresses.
22 (If we supported non-legacy ATA controllers this would not be
23 flexible enough, but it's fine for what we do.) */
24 #define reg_ctl(CHANNEL) ((CHANNEL)->reg_base + 0x206) /* Control (w/o). */
25 #define reg_alt_status(CHANNEL) reg_ctl (CHANNEL) /* Alt Status (r/o). */
27 /* Alternate Status Register bits. */
28 #define STA_BSY 0x80 /* Busy. */
29 #define STA_DRQ 0x08 /* Data Request. */
31 /* Control Register bits. */
32 #define CTL_SRST 0x04 /* Software Reset. */
34 /* Device Register bits. */
35 #define DEV_MBS 0xa0 /* Must be set. */
36 #define DEV_LBA 0x40 /* Linear based addressing. */
37 #define DEV_DEV 0x10 /* Select device: 0=master, 1=slave. */
40 Many more are defined but this is the small subset that we
42 #define CMD_IDENTIFY_DEVICE 0xec /* IDENTIFY DEVICE. */
43 #define CMD_READ_SECTOR_RETRY 0x20 /* READ SECTOR with retries. */
44 #define CMD_WRITE_SECTOR_RETRY 0x30 /* WRITE SECTOR with retries. */
49 char name[8]; /* Name, e.g. "hd0:1". */
50 struct channel *channel; /* Channel disk is on. */
51 int dev_no; /* Device 0 or 1 for master or slave. */
53 bool is_ata; /* 1=This device is an ATA disk. */
54 disk_sector_t capacity; /* Capacity in sectors (if is_ata is true). */
57 /* An ATA channel (aka controller).
58 Each channel can control up to two disks. */
61 char name[8]; /* Name, e.g. "hd0". */
62 uint16_t reg_base; /* Base I/O port. */
63 uint8_t irq; /* Interrupt in use. */
65 struct lock lock; /* Must acquire to access the controller. */
66 bool expecting_interrupt; /* True if an interrupt is expected, false if
67 any interrupt would be spurious. */
68 struct semaphore completion_wait; /* Up'd by interrupt handler. */
70 struct disk devices[2]; /* The devices on this channel. */
73 /* We support the two "legacy" ATA channels found in a standard PC. */
75 static struct channel channels[CHANNEL_CNT];
77 static void reset_channel (struct channel *);
78 static bool check_device_type (struct disk *);
79 static void identify_ata_device (struct disk *);
81 static void select_sector (struct disk *, disk_sector_t);
82 static void issue_pio_command (struct channel *, uint8_t command);
83 static void input_sector (struct channel *, void *);
84 static void output_sector (struct channel *, const void *);
86 static void wait_until_idle (const struct disk *);
87 static bool wait_while_busy (const struct disk *);
88 static void select_device (const struct disk *);
89 static void select_device_wait (const struct disk *);
91 static void interrupt_handler (struct intr_frame *);
93 /* Initialize the disk subsystem and detect disks. */
99 for (chan_no = 0; chan_no < CHANNEL_CNT; chan_no++)
101 struct channel *c = &channels[chan_no];
104 /* Initialize channel. */
105 snprintf (c->name, sizeof c->name, "hd%d", chan_no);
119 lock_init (&c->lock, c->name);
120 c->expecting_interrupt = false;
121 sema_init (&c->completion_wait, 0, c->name);
123 /* Initialize devices. */
124 for (dev_no = 0; dev_no < 2; dev_no++)
126 struct disk *d = &c->devices[dev_no];
127 snprintf (d->name, sizeof d->name, "%s:%d", c->name, dev_no);
135 /* Register interrupt handler. */
136 intr_register (c->irq, 0, INTR_OFF, interrupt_handler, c->name);
138 /* Reset hardware. */
141 /* Distinguish ATA hard disks from other devices. */
142 if (check_device_type (&c->devices[0]))
143 check_device_type (&c->devices[1]);
145 /* Read hard disk identity information. */
146 for (dev_no = 0; dev_no < 2; dev_no++)
147 if (c->devices[dev_no].is_ata)
148 identify_ata_device (&c->devices[dev_no]);
152 /* Returns the disk numbered DEV_NO--either 0 or 1 for master or
153 slave, respectively--within the channel numbered CHAN_NO. */
155 disk_get (int chan_no, int dev_no)
157 ASSERT (dev_no == 0 || dev_no == 1);
159 if (chan_no < (int) CHANNEL_CNT)
161 struct disk *d = &channels[chan_no].devices[dev_no];
168 /* Returns the size of disk D, measured in DISK_SECTOR_SIZE-byte
171 disk_size (struct disk *d)
178 /* Reads sector SEC_NO from disk D into BUFFER, which must have
179 room for DISK_SECTOR_SIZE bytes. */
181 disk_read (struct disk *d, disk_sector_t sec_no, void *buffer)
186 ASSERT (buffer != NULL);
189 lock_acquire (&c->lock);
190 select_sector (d, sec_no);
191 issue_pio_command (c, CMD_READ_SECTOR_RETRY);
192 sema_down (&c->completion_wait);
193 if (!wait_while_busy (d))
194 PANIC ("%s: disk read failed, sector=%"PRDSNu, d->name, sec_no);
195 input_sector (c, buffer);
196 lock_release (&c->lock);
199 /* Write sector SEC_NO to disk D from BUFFER, which must contain
200 DISK_SECTOR_SIZE bytes. Returns after the disk has
201 acknowledged receiving the data. */
203 disk_write (struct disk *d, disk_sector_t sec_no, const void *buffer)
208 ASSERT (buffer != NULL);
211 lock_acquire (&c->lock);
212 select_sector (d, sec_no);
213 issue_pio_command (c, CMD_WRITE_SECTOR_RETRY);
214 if (!wait_while_busy (d))
215 PANIC ("%s: disk write failed, sector=%"PRDSNu, d->name, sec_no);
216 output_sector (c, buffer);
217 sema_down (&c->completion_wait);
218 lock_release (&c->lock);
221 /* Disk detection and identification. */
223 static void printk_ata_string (char *string, size_t size);
225 /* Resets an ATA channel and waits for any devices present on it
226 to finish the reset. */
228 reset_channel (struct channel *c)
233 /* The ATA reset sequence depends on which devices are present,
234 so we start by detecting device presence. */
235 for (dev_no = 0; dev_no < 2; dev_no++)
237 struct disk *d = &c->devices[dev_no];
241 outb (reg_nsect (c), 0x55);
242 outb (reg_lbal (c), 0xaa);
244 outb (reg_nsect (c), 0xaa);
245 outb (reg_lbal (c), 0x55);
247 outb (reg_nsect (c), 0x55);
248 outb (reg_lbal (c), 0xaa);
250 present[dev_no] = (inb (reg_nsect (c)) == 0x55
251 && inb (reg_lbal (c)) == 0xaa);
254 /* Issue soft reset sequence, which selects device 0 as a side effect.
255 Also enable interrupts. */
256 outb (reg_ctl (c), 0);
258 outb (reg_ctl (c), CTL_SRST);
260 outb (reg_ctl (c), 0);
264 /* Wait for device 0 to clear BSY. */
267 select_device (&c->devices[0]);
268 wait_while_busy (&c->devices[0]);
271 /* Wait for device 1 to clear BSY. */
276 select_device (&c->devices[1]);
277 for (i = 0; i < 3000; i++)
279 if (inb (reg_nsect (c)) == 1 && inb (reg_lbal (c)) == 1)
283 wait_while_busy (&c->devices[1]);
287 /* Checks whether device D is an ATA disk and sets D's is_ata
288 member appropriately. If D is device 0 (master), returns true
289 if it's possible that a slave (device 1) exists on this
290 channel. If D is device 1 (slave), the return value is not
293 check_device_type (struct disk *d)
295 struct channel *c = d->channel;
296 uint8_t error, lbam, lbah;
300 error = inb (reg_error (c));
301 lbam = inb (reg_lbam (c));
302 lbah = inb (reg_lbah (c));
304 if (error != 1 && (error != 0x81 || d->dev_no == 1))
307 return error != 0x81;
311 d->is_ata = (lbam == 0 && lbah == 0) || (lbam == 0x3c && lbah == 0xc3);
316 /* Sends an IDENTIFY DEVICE command to disk D and reads the
317 response. Initializes D's capacity member based on the result
318 and prints a message describing the disk to the console. */
320 identify_ata_device (struct disk *d)
322 struct channel *c = d->channel;
323 uint16_t id[DISK_SECTOR_SIZE / 2];
327 /* Send the IDENTIFY DEVICE command, wait for an interrupt
328 indicating the device's response is ready, and read the data
330 select_device_wait (d);
331 issue_pio_command (c, CMD_IDENTIFY_DEVICE);
332 sema_down (&c->completion_wait);
333 if (!wait_while_busy (d))
338 input_sector (c, id);
340 /* Calculate capacity. */
341 d->capacity = id[60] | ((uint32_t) id[61] << 16);
343 /* Print identification message. */
344 printk ("%s: detected %'"PRDSNu" sector (", d->name, d->capacity);
345 if (d->capacity > 1024 / DISK_SECTOR_SIZE * 1024 * 1024)
346 printk ("%"PRDSNu" GB",
347 d->capacity / (1024 / DISK_SECTOR_SIZE * 1024 * 1024));
348 else if (d->capacity > 1024 / DISK_SECTOR_SIZE * 1024)
349 printk ("%"PRDSNu" MB", d->capacity / (1024 / DISK_SECTOR_SIZE * 1024));
350 else if (d->capacity > 1024 / DISK_SECTOR_SIZE)
351 printk ("%"PRDSNu" kB", d->capacity / (1024 / DISK_SECTOR_SIZE));
353 printk ("%"PRDSNu" byte", d->capacity * DISK_SECTOR_SIZE);
354 printk (") disk, model \"");
355 printk_ata_string ((char *) &id[27], 40);
356 printk ("\", serial \"");
357 printk_ata_string ((char *) &id[10], 20);
361 /* Prints STRING, which consists of SIZE bytes in a funky format:
362 each pair of bytes is in reverse order. Does not print
363 trailing whitespace and/or nulls. */
365 printk_ata_string (char *string, size_t size)
369 /* Find the last non-white, non-null character. */
370 for (; size > 0; size--)
372 int c = string[(size - 1) ^ 1];
373 if (c != '\0' && !isspace (c))
378 for (i = 0; i < size; i++)
379 printk ("%c", string[i ^ 1]);
382 /* Selects device D, waiting for it to become ready, and then
383 writes SEC_NO to the disk's sector selection registers. (We
386 select_sector (struct disk *d, disk_sector_t sec_no)
388 struct channel *c = d->channel;
390 ASSERT (sec_no < d->capacity);
391 ASSERT (sec_no < (1UL << 28));
393 select_device_wait (d);
394 outb (reg_nsect (c), 1);
395 outb (reg_lbal (c), sec_no);
396 outb (reg_lbam (c), sec_no >> 8);
397 outb (reg_lbah (c), (sec_no >> 16));
398 outb (reg_device (c),
399 DEV_MBS | DEV_LBA | (d->dev_no == 1 ? DEV_DEV : 0) | (sec_no >> 24));
402 /* Writes COMMAND to channel C and prepares for receiving a
403 completion interrupt. */
405 issue_pio_command (struct channel *c, uint8_t command)
407 /* Interrupts must be enabled or our semaphore will never be
408 up'd by the completion handler. */
409 ASSERT (intr_get_level () == INTR_ON);
411 c->expecting_interrupt = true;
412 outb (reg_command (c), command);
415 /* Reads a sector from channel C's data register in PIO mode into
416 SECTOR, which must have room for DISK_SECTOR_SIZE bytes. */
418 input_sector (struct channel *c, void *sector)
420 insw (reg_data (c), sector, DISK_SECTOR_SIZE / 2);
423 /* Writes SECTOR to channel C's data register in PIO mode.
424 SECTOR must contain DISK_SECTOR_SIZE bytes. */
426 output_sector (struct channel *c, const void *sector)
428 outsw (reg_data (c), sector, DISK_SECTOR_SIZE / 2);
431 /* Low-level ATA primitives. */
433 /* Wait up to 10 seconds for the controller to become idle, that
434 is, for the BSY and DRQ bits to clear in the status register.
436 As a side effect, reading the status register clears any
437 pending interrupt. */
439 wait_until_idle (const struct disk *d)
443 for (i = 0; i < 1000; i++)
445 if ((inb (reg_status (d->channel)) & (STA_BSY | STA_DRQ)) == 0)
450 printk ("%s: idle timeout\n", d->name);
453 /* Wait up to 30 seconds for disk D to clear BSY,
454 and then return the status of the DRQ bit.
455 The ATA standards say that a disk may take as long as that to
456 complete its reset. */
458 wait_while_busy (const struct disk *d)
460 struct channel *c = d->channel;
463 for (i = 0; i < 3000; i++)
466 printk ("%s: busy, waiting...", d->name);
467 if (!(inb (reg_alt_status (c)) & STA_BSY))
471 return (inb (reg_alt_status (c)) & STA_DRQ) != 0;
480 /* Program D's channel so that D is now the selected disk. */
482 select_device (const struct disk *d)
484 struct channel *c = d->channel;
485 uint8_t dev = DEV_MBS;
488 outb (reg_device (c), dev);
489 inb (reg_alt_status (c));
493 /* Select disk D in its channel, as select_device(), but wait for
494 the channel to become idle before and after. */
496 select_device_wait (const struct disk *d)
503 /* ATA interrupt handler. */
505 interrupt_handler (struct intr_frame *f)
509 for (c = channels; c < channels + CHANNEL_CNT; c++)
510 if (f->vec_no == c->irq)
512 if (c->expecting_interrupt)
514 inb (reg_status (c)); /* Acknowledge interrupt. */
515 sema_up (&c->completion_wait); /* Wake up waiter. */
518 printk ("%s: unexpected interrupt\n", c->name);