2006-09-06 19:50:20 +02:00
|
|
|
// Simple PIO-based (non-DMA) IDE driver code.
|
2006-06-16 22:29:25 +02:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
2006-09-03 17:39:29 +02:00
|
|
|
#include "traps.h"
|
2006-07-17 07:00:25 +02:00
|
|
|
#include "spinlock.h"
|
2007-08-24 21:32:36 +02:00
|
|
|
#include "buf.h"
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-09-06 19:54:29 +02:00
|
|
|
#define IDE_BSY 0x80
|
|
|
|
#define IDE_DRDY 0x40
|
|
|
|
#define IDE_DF 0x20
|
|
|
|
#define IDE_ERR 0x01
|
|
|
|
|
|
|
|
#define IDE_CMD_READ 0x20
|
|
|
|
#define IDE_CMD_WRITE 0x30
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2007-08-24 21:32:36 +02:00
|
|
|
// ide_queue points to the buf now being read/written to the disk.
|
|
|
|
// ide_queue->qnext points to the next buf to be processed.
|
2007-08-24 21:52:49 +02:00
|
|
|
// You must hold ide_lock while manipulating queue.
|
2006-09-07 16:28:12 +02:00
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
static struct spinlock ide_lock;
|
2007-08-24 21:32:36 +02:00
|
|
|
static struct buf *ide_queue;
|
2006-09-07 16:28:12 +02:00
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
static int disk_1_present;
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
static int ide_probe_disk1(void);
|
2007-08-24 21:32:36 +02:00
|
|
|
static void ide_start_request();
|
2006-08-30 20:55:06 +02:00
|
|
|
|
2007-08-14 20:46:29 +02:00
|
|
|
//PAGEBREAK: 10
|
2006-09-07 17:29:54 +02:00
|
|
|
// Wait for IDE disk to become ready.
|
2006-06-16 22:29:25 +02:00
|
|
|
static int
|
|
|
|
ide_wait_ready(int check_error)
|
|
|
|
{
|
2006-07-05 22:00:14 +02:00
|
|
|
int r;
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
while(((r = inb(0x1F7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY)
|
2006-09-06 19:50:20 +02:00
|
|
|
;
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
if(check_error && (r & (IDE_DF|IDE_ERR)) != 0)
|
2006-07-05 22:00:14 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2006-06-16 22:29:25 +02:00
|
|
|
}
|
|
|
|
|
2006-07-05 22:00:14 +02:00
|
|
|
void
|
|
|
|
ide_init(void)
|
|
|
|
{
|
2006-08-11 00:08:14 +02:00
|
|
|
initlock(&ide_lock, "ide");
|
2006-09-07 17:29:54 +02:00
|
|
|
irq_enable(IRQ_IDE);
|
|
|
|
ioapic_enable(IRQ_IDE, ncpu - 1);
|
2006-07-05 22:00:14 +02:00
|
|
|
ide_wait_ready(0);
|
2006-08-30 20:55:06 +02:00
|
|
|
disk_1_present = ide_probe_disk1();
|
2006-07-05 22:00:14 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
// Probe to see if disk 1 exists (we assume disk 0 exists).
|
|
|
|
static int
|
2006-06-16 22:29:25 +02:00
|
|
|
ide_probe_disk1(void)
|
|
|
|
{
|
2006-07-05 22:00:14 +02:00
|
|
|
int r, x;
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-07-05 22:00:14 +02:00
|
|
|
// wait for Device 0 to be ready
|
|
|
|
ide_wait_ready(0);
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-07-05 22:00:14 +02:00
|
|
|
// switch to Device 1
|
|
|
|
outb(0x1F6, 0xE0 | (1<<4));
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-07-05 22:00:14 +02:00
|
|
|
// check for Device 1 to be ready for a while
|
2006-09-06 19:27:19 +02:00
|
|
|
for(x = 0; x < 1000 && (r = inb(0x1F7)) == 0; x++)
|
2006-09-06 19:50:20 +02:00
|
|
|
;
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-07-05 22:00:14 +02:00
|
|
|
// switch back to Device 0
|
|
|
|
outb(0x1F6, 0xE0 | (0<<4));
|
2006-06-16 22:29:25 +02:00
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
return x < 1000;
|
2006-06-16 22:29:25 +02:00
|
|
|
}
|
|
|
|
|
2006-09-08 16:33:27 +02:00
|
|
|
// Interrupt handler - wake up the request that just finished.
|
|
|
|
void
|
|
|
|
ide_intr(void)
|
|
|
|
{
|
|
|
|
acquire(&ide_lock);
|
2007-08-24 21:32:36 +02:00
|
|
|
if(ide_queue){
|
|
|
|
if((ide_queue->flags & B_WRITE) == 0)
|
|
|
|
if(ide_wait_ready(1) >= 0)
|
|
|
|
insl(0x1F0, ide_queue->data, 512/4);
|
|
|
|
ide_queue->done = 1;
|
|
|
|
wakeup(ide_queue);
|
|
|
|
ide_queue = ide_queue->qnext;
|
|
|
|
ide_start_request();
|
|
|
|
} else {
|
|
|
|
cprintf("stray ide interrupt\n");
|
|
|
|
}
|
2006-09-08 16:33:27 +02:00
|
|
|
release(&ide_lock);
|
|
|
|
}
|
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
// Start the next request in the queue.
|
2007-08-24 21:32:36 +02:00
|
|
|
// Caller must hold ide_lock.
|
2006-09-07 17:29:54 +02:00
|
|
|
static void
|
2006-07-10 21:06:48 +02:00
|
|
|
ide_start_request (void)
|
|
|
|
{
|
2007-08-24 21:32:36 +02:00
|
|
|
if(ide_queue){
|
2006-07-10 21:06:48 +02:00
|
|
|
ide_wait_ready(0);
|
2006-08-06 22:28:15 +02:00
|
|
|
outb(0x3f6, 0); // generate interrupt
|
2007-08-24 21:32:36 +02:00
|
|
|
outb(0x1F2, 1); // number of sectors
|
|
|
|
outb(0x1F3, ide_queue->sector & 0xFF);
|
|
|
|
outb(0x1F4, (ide_queue->sector >> 8) & 0xFF);
|
|
|
|
outb(0x1F5, (ide_queue->sector >> 16) & 0xFF);
|
|
|
|
outb(0x1F6, 0xE0 |
|
|
|
|
((ide_queue->dev & 1)<<4) |
|
|
|
|
((ide_queue->sector>>24)&0x0F));
|
|
|
|
if(ide_queue->flags & B_WRITE){
|
2006-09-06 19:54:29 +02:00
|
|
|
outb(0x1F7, IDE_CMD_WRITE);
|
2007-08-24 21:32:36 +02:00
|
|
|
outsl(0x1F0, ide_queue->data, 512/4);
|
|
|
|
} else {
|
|
|
|
outb(0x1F7, IDE_CMD_READ);
|
2006-08-07 03:38:46 +02:00
|
|
|
}
|
2006-07-10 21:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:46:29 +02:00
|
|
|
//PAGEBREAK: 30
|
2007-08-24 21:32:36 +02:00
|
|
|
// Queue up a disk operation and wait for it to finish.
|
|
|
|
// b must have B_BUSY set.
|
2006-09-07 17:29:54 +02:00
|
|
|
void
|
2007-08-24 21:32:36 +02:00
|
|
|
ide_rw(struct buf *b)
|
2006-06-16 22:29:25 +02:00
|
|
|
{
|
2007-08-24 21:32:36 +02:00
|
|
|
struct buf **pp;
|
2006-08-06 22:28:15 +02:00
|
|
|
|
2007-08-24 21:32:36 +02:00
|
|
|
if((b->dev & 0xff) && !disk_1_present)
|
2006-08-30 20:55:06 +02:00
|
|
|
panic("ide disk 1 not present");
|
2006-07-05 22:00:14 +02:00
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
acquire(&ide_lock);
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2007-08-24 21:32:36 +02:00
|
|
|
b->done = 0;
|
|
|
|
b->qnext = 0;
|
2006-07-10 21:06:48 +02:00
|
|
|
|
2007-08-24 21:32:36 +02:00
|
|
|
// append b to ide_queue
|
|
|
|
pp = &ide_queue;
|
|
|
|
while(*pp)
|
|
|
|
pp = &(*pp)->qnext;
|
|
|
|
*pp = b;
|
|
|
|
|
|
|
|
if(ide_queue == b)
|
|
|
|
ide_start_request();
|
|
|
|
|
|
|
|
while(!b->done)
|
|
|
|
sleep(b, &ide_lock);
|
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
release(&ide_lock);
|
2006-06-16 22:29:25 +02:00
|
|
|
}
|