1 // Simple PIO-based (non-DMA) IDE driver code.
18 #define IDE_CMD_READ 0x20
19 #define IDE_CMD_WRITE 0x30
21 // idequeue points to the buf now being read/written to the disk.
22 // idequeue->qnext points to the next buf to be processed.
23 // You must hold idelock while manipulating queue.
25 static struct spinlock idelock
;
26 static struct buf
*idequeue
;
29 static void idestart(struct buf
*);
31 // Wait for IDE disk to become ready.
37 while(((r
= inb(0x1f7)) & (IDE_BSY
|IDE_DRDY
)) != IDE_DRDY
)
39 if(checkerr
&& (r
& (IDE_DF
|IDE_ERR
)) != 0)
49 initlock(&idelock
, "ide");
51 ioapicenable(IRQ_IDE
, ncpu
- 1);
54 // Check if disk 1 is present
55 outb(0x1f6, 0xe0 | (1<<4));
56 for(i
=0; i
<1000; i
++){
63 // Switch back to disk 0.
64 outb(0x1f6, 0xe0 | (0<<4));
67 // Start the request for b. Caller must hold idelock.
69 idestart(struct buf
*b
)
75 outb(0x3f6, 0); // generate interrupt
76 outb(0x1f2, 1); // number of sectors
77 outb(0x1f3, b
->sector
& 0xff);
78 outb(0x1f4, (b
->sector
>> 8) & 0xff);
79 outb(0x1f5, (b
->sector
>> 16) & 0xff);
80 outb(0x1f6, 0xe0 | ((b
->dev
&1)<<4) | ((b
->sector
>>24)&0x0f));
81 if(b
->flags
& B_DIRTY
){
82 outb(0x1f7, IDE_CMD_WRITE
);
83 outsl(0x1f0, b
->data
, 512/4);
85 outb(0x1f7, IDE_CMD_READ
);
95 // Take first buffer off queue.
97 if((b
= idequeue
) == 0){
99 // cprintf("spurious IDE interrupt\n");
104 // Read data if needed.
105 if(!(b
->flags
& B_DIRTY
) && idewait(1) >= 0)
106 insl(0x1f0, b
->data
, 512/4);
108 // Wake process waiting for this buf.
110 b
->flags
&= ~B_DIRTY
;
113 // Start disk on next buf in queue.
120 // Sync buf with disk.
121 // If B_DIRTY is set, write buf to disk, clear B_DIRTY, set B_VALID.
122 // Else if B_VALID is not set, read buf from disk, set B_VALID.
128 if(!(b
->flags
& B_BUSY
))
129 panic("iderw: buf not busy");
130 if((b
->flags
& (B_VALID
|B_DIRTY
)) == B_VALID
)
131 panic("iderw: nothing to do");
132 if(b
->dev
!= 0 && !havedisk1
)
133 panic("iderw: ide disk 1 not present");
137 // Append b to idequeue.
139 for(pp
=&idequeue
; *pp
; pp
=&(*pp
)->qnext
)
143 // Start disk if necessary.
147 // Wait for request to finish.
148 // Assuming will not sleep too long: ignore proc->killed.
149 while((b
->flags
& (B_VALID
|B_DIRTY
)) != B_VALID
){