IDE interrupt based driver
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m36s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m36s
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <status.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
#define IDE_REG_DATA 0x00
|
||||
#define IDE_REG_ERROR 0x01
|
||||
@@ -38,6 +39,10 @@
|
||||
#define IDE_CMD_FLUSH28 0xE7
|
||||
#define IDE_CMD_IDENTIFY 0xEC
|
||||
|
||||
#define IDE_READ 1
|
||||
#define IDE_WRITE 2
|
||||
#define IDE_FLUSH 3
|
||||
|
||||
static bool ide_wait (uint16_t io, uint32_t timeout, bool drq, bool errcheck) {
|
||||
uint32_t i = 0;
|
||||
uint8_t status;
|
||||
@@ -54,14 +59,14 @@ static bool ide_wait (uint16_t io, uint32_t timeout, bool drq, bool errcheck) {
|
||||
|
||||
if (drq) {
|
||||
i = 0;
|
||||
while (!(status & IDE_DRQ)) {
|
||||
while (!(status & IDE_DRQ) || (status & IDE_BSY)) {
|
||||
if (status & IDE_ERR)
|
||||
return false;
|
||||
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0xFF)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
if (++i >= timeout)
|
||||
return false;
|
||||
@@ -83,13 +88,6 @@ static void ide_delay (uint16_t ctrl) {
|
||||
}
|
||||
#pragma clang optimize on
|
||||
|
||||
static void ide_flush (struct idedrv* idedrv) {
|
||||
if (idedrv->lba48)
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH48);
|
||||
else
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH28);
|
||||
}
|
||||
|
||||
static void ide_irq (void* arg, void* regs, bool user, struct reschedule_ctx* rctx) {
|
||||
uint64_t fd, fp;
|
||||
|
||||
@@ -106,51 +104,47 @@ static void ide_irq (void* arg, void* regs, bool user, struct reschedule_ctx* rc
|
||||
}
|
||||
|
||||
struct idedrv_request* req = list_entry (node, struct idedrv_request, requests_link);
|
||||
struct list_node_link* sqlist_node = req->sq.proc_list;
|
||||
struct proc_sq_entry* sq_entry = list_entry (sqlist_node, struct proc_sq_entry, sq_link);
|
||||
struct proc* resumed_proc = sq_entry->proc;
|
||||
|
||||
uint8_t status = inb (idedrv->io + IDE_REG_STATUS);
|
||||
|
||||
if ((status & (IDE_ERR | IDE_DRQ))) {
|
||||
if (req->type == IDE_FLUSH) {
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
proc_sq_resume (resumed_proc, sq_entry, rctx);
|
||||
free (req);
|
||||
atomic_store (&req->done, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
spin_lock (&resumed_proc->lock, &fp);
|
||||
if ((status & (IDE_ERR | IDE_DF))) {
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
atomic_store (&req->done, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req->rw == 1) {
|
||||
if ((status & IDE_DRQ)) {
|
||||
uint16_t* p = req->outbuffer + (req->sector_done_count * (idedrv->sector_size / 2));
|
||||
if (!(status & IDE_BSY) && (status & IDE_DRQ)) {
|
||||
switch (req->type) {
|
||||
case IDE_READ: {
|
||||
uint16_t* p = req->buffer + (req->sector_done_count * (idedrv->sector_size / 2));
|
||||
insw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
|
||||
req->sector_done_count++;
|
||||
}
|
||||
} else if (req->rw == 2) {
|
||||
req->sector_done_count++;
|
||||
if (req->sector_done_count < req->sector_count) {
|
||||
uint16_t* p = req->outbuffer + (req->sector_done_count * (idedrv->sector_size / 2));
|
||||
outsw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
|
||||
} break;
|
||||
case IDE_WRITE: {
|
||||
req->sector_done_count++;
|
||||
if (req->sector_done_count < req->sector_count) {
|
||||
uint16_t* p = req->buffer + (req->sector_done_count * (idedrv->sector_size / 2));
|
||||
outsw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
if (req->sector_done_count >= req->sector_count) {
|
||||
if (req->rw == 2)
|
||||
ide_flush (idedrv);
|
||||
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
|
||||
spin_unlock (&resumed_proc->lock, fp);
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
|
||||
free (req);
|
||||
proc_sq_resume (resumed_proc, sq_entry, rctx);
|
||||
atomic_store (&req->done, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
spin_unlock (&resumed_proc->lock, fp);
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
}
|
||||
|
||||
@@ -286,8 +280,6 @@ void idedrv_fini (struct device* device, struct proc* proc, struct reschedule_ct
|
||||
list_foreach (idedrv->requests, req_link, tmp_req_link) {
|
||||
struct idedrv_request* req = list_entry (req_link, struct idedrv_request, requests_link);
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
struct proc_sq_entry* sq_entry = list_entry (req->sq.proc_list, struct proc_sq_entry, sq_link);
|
||||
proc_sq_resume (proc, sq_entry, rctx);
|
||||
free (req);
|
||||
}
|
||||
|
||||
@@ -295,10 +287,9 @@ void idedrv_fini (struct device* device, struct proc* proc, struct reschedule_ct
|
||||
free (idedrv);
|
||||
}
|
||||
|
||||
int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4) {
|
||||
int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a4;
|
||||
uint64_t fp;
|
||||
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
@@ -312,52 +303,48 @@ int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
spin_lock (&proc->lock, &fp);
|
||||
bool is_kproc = (proc->flags & PROC_KPROC) != 0;
|
||||
spin_unlock (&proc->lock, fp);
|
||||
struct idedrv_request* req = malloc (sizeof (*req));
|
||||
|
||||
/* /1* polling *1/ */
|
||||
/* if (is_kproc) { */
|
||||
ide_prepare (idedrv, sector, sector_count, false);
|
||||
if (req == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
memset (req, 0, sizeof (*req));
|
||||
req->buffer = buffer;
|
||||
req->sector_count = sector_count;
|
||||
req->sector_done_count = 0;
|
||||
req->type = IDE_READ;
|
||||
|
||||
list_append (idedrv->requests, &req->requests_link);
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count, true);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_READ_ERROR;
|
||||
|
||||
insw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
(idedrv->sector_size / 2));
|
||||
if (!ide_wait (idedrv->io, 100000, true, true)) {
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
free (req);
|
||||
return -ST_XDRV_READ_ERROR;
|
||||
}
|
||||
|
||||
insw (idedrv->io + IDE_REG_DATA, buffer, idedrv->sector_size / 2);
|
||||
|
||||
req->sector_done_count = 1;
|
||||
|
||||
spin_unlock (&device->lock, *lockflags);
|
||||
|
||||
while (!atomic_load (&req->done))
|
||||
spin_lock_relax ();
|
||||
|
||||
spin_lock (&device->lock, lockflags);
|
||||
|
||||
free (req);
|
||||
|
||||
return ST_OK;
|
||||
/* } else { /1* IRQ *1/ */
|
||||
/* struct idedrv_request* req = malloc (sizeof (*req)); */
|
||||
|
||||
/* if (req == NULL) */
|
||||
/* return -ST_OOM_ERROR; */
|
||||
|
||||
/* memset (req, 0, sizeof (*req)); */
|
||||
/* req->outbuffer = buffer; */
|
||||
/* req->sector_count = sector_count; */
|
||||
/* req->sector_done_count = 0; */
|
||||
/* req->rw = 1; */
|
||||
|
||||
/* list_append (idedrv->requests, &req->requests_link); */
|
||||
/* proc_sq_suspend (proc, &req->sq, NULL, rctx); */
|
||||
|
||||
/* ide_prepare (idedrv, sector, sector_count, true); */
|
||||
|
||||
/* uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28; */
|
||||
/* outb (idedrv->io + IDE_REG_CMD, cmd); */
|
||||
|
||||
/* return ST_OK; */
|
||||
/* } */
|
||||
}
|
||||
|
||||
int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4) {
|
||||
int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a4;
|
||||
uint64_t fp;
|
||||
|
||||
@@ -373,63 +360,76 @@ int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ct
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
spin_lock (&proc->lock, &fp);
|
||||
bool is_kproc = (proc->flags & PROC_KPROC) != 0;
|
||||
spin_unlock (&proc->lock, fp);
|
||||
struct idedrv_request* req = malloc (sizeof (*req));
|
||||
|
||||
/* /1* polling *1/ */
|
||||
/* if (is_kproc) { */
|
||||
ide_prepare (idedrv, sector, sector_count, false);
|
||||
struct idedrv_request* flushreq = malloc (sizeof (*req));
|
||||
|
||||
if (req == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
if (flushreq == NULL) {
|
||||
free (req);
|
||||
return -ST_OOM_ERROR;
|
||||
}
|
||||
|
||||
memset (req, 0, sizeof (*req));
|
||||
req->buffer = buffer;
|
||||
req->sector_count = sector_count;
|
||||
req->sector_done_count = 0;
|
||||
req->type = IDE_WRITE;
|
||||
|
||||
list_append (idedrv->requests, &req->requests_link);
|
||||
|
||||
memset (flushreq, 0, sizeof (*flushreq));
|
||||
flushreq->buffer = NULL;
|
||||
flushreq->sector_count = 0;
|
||||
flushreq->sector_done_count = 0;
|
||||
flushreq->type = IDE_FLUSH;
|
||||
|
||||
list_append (idedrv->requests, &flushreq->requests_link);
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count, true);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
|
||||
outsw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
(idedrv->sector_size / 2));
|
||||
if (!ide_wait (idedrv->io, 100000, true, true)) {
|
||||
list_remove (idedrv->requests, &req->requests_link);
|
||||
free (req);
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
}
|
||||
|
||||
outsw (idedrv->io + IDE_REG_DATA, buffer, idedrv->sector_size / 2);
|
||||
|
||||
req->sector_done_count = 1;
|
||||
|
||||
spin_unlock (&device->lock, *lockflags);
|
||||
|
||||
while (!atomic_load (&req->done))
|
||||
spin_lock_relax ();
|
||||
|
||||
spin_lock (&device->lock, lockflags);
|
||||
|
||||
if (idedrv->lba48)
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH48);
|
||||
else
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH28);
|
||||
|
||||
spin_unlock (&device->lock, *lockflags);
|
||||
|
||||
while (!atomic_load (&flushreq->done))
|
||||
spin_lock_relax ();
|
||||
|
||||
spin_lock (&device->lock, lockflags);
|
||||
|
||||
free (req);
|
||||
free (flushreq);
|
||||
|
||||
return ST_OK;
|
||||
/* } else { /1* IRQ *1/ */
|
||||
/* struct idedrv_request* req = malloc (sizeof (*req)); */
|
||||
|
||||
/* if (req == NULL) */
|
||||
/* return -ST_OOM_ERROR; */
|
||||
|
||||
/* memset (req, 0, sizeof (*req)); */
|
||||
/* req->outbuffer = buffer; */
|
||||
/* req->sector_count = sector_count; */
|
||||
/* req->sector_done_count = 0; */
|
||||
/* req->rw = 2; */
|
||||
|
||||
/* list_append (idedrv->requests, &req->requests_link); */
|
||||
/* proc_sq_suspend (proc, &req->sq, NULL, rctx); */
|
||||
|
||||
/* ide_prepare (idedrv, sector, sector_count, true); */
|
||||
|
||||
/* uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28; */
|
||||
/* outb (idedrv->io + IDE_REG_CMD, cmd); */
|
||||
|
||||
/* if (!ide_wait (idedrv->io, 100000, true, true)) { */
|
||||
/* list_remove (idedrv->requests, &req->requests_link); */
|
||||
/* struct proc_sq_entry* sq_entry = list_entry (req->sq.proc_list, struct proc_sq_entry,
|
||||
* sq_link); */
|
||||
/* proc_sq_resume (proc, sq_entry, rctx); */
|
||||
/* free (req); */
|
||||
/* return -ST_XDRV_WRITE_ERROR; */
|
||||
/* } */
|
||||
|
||||
/* outsw (idedrv->io + IDE_REG_DATA, buffer, idedrv->sector_size / 2); */
|
||||
|
||||
/* return ST_OK; */
|
||||
/* } */
|
||||
}
|
||||
|
||||
int idedrv_get_device_type (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)device, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
@@ -443,7 +443,7 @@ int idedrv_get_device_type (struct device* device, struct proc* proc, struct res
|
||||
}
|
||||
|
||||
int idedrv_get_sector_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
@@ -459,7 +459,7 @@ int idedrv_get_sector_size (struct device* device, struct proc* proc, struct res
|
||||
}
|
||||
|
||||
int idedrv_get_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
|
||||
Reference in New Issue
Block a user