feat: block partition support
[lunaix-os.git] / lunaix-os / hal / ahci / io_event.c
1 #include <hal/ahci/hba.h>
2 #include <hal/ahci/sata.h>
3 #include <lunaix/isrm.h>
4 #include <lunaix/mm/valloc.h>
5
6 extern struct ahci_hba hba;
7
8 void
9 __ahci_hba_isr(const isr_param* param)
10 {
11     // ignore spurious interrupt
12     if (!hba.base[HBA_RIS])
13         return;
14
15     u32_t port_num = 31 - __builtin_clz(hba.base[HBA_RIS]);
16     struct hba_port* port = hba.ports[port_num];
17     struct hba_cmd_context* cmdctx = &port->cmdctx;
18     u32_t ci_filtered = port->regs[HBA_RPxCI] ^ cmdctx->tracked_ci;
19
20     if (!ci_filtered) {
21         goto done;
22     }
23
24     u32_t slot = 31 - __builtin_clz(ci_filtered);
25     struct hba_cmd_state* cmdstate = cmdctx->issued[slot];
26
27     if (!cmdstate) {
28         goto done;
29     }
30
31     struct blkio_req* ioreq = (struct blkio_req*)cmdstate->state_ctx;
32     sata_read_error(port);
33     if ((port->device->last_result.status & HBA_PxTFD_ERR)) {
34         ioreq->errcode = port->regs[HBA_RPxTFD] & 0xffff;
35         ioreq->flags |= BLKIO_ERROR;
36     }
37
38     blkio_complete(ioreq);
39     vfree(cmdstate->cmd_table);
40
41 done:
42     hba_clear_reg(port->regs[HBA_RPxIS]);
43 }
44
45 void
46 __ahci_blkio_handler(struct blkio_req* req)
47 {
48     struct hba_device* hbadev = (struct hba_device*)req->io_ctx->driver;
49
50     hbadev->ops.submit(hbadev, req);
51 }