ahci.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <kernel/pci.h>
  24. #include <kernel/mm.h>
  25. #include <kernel/int.h>
  26. #include <kernel/panic.h>
  27. #include <kernel/schk.h>
  28. #include <arch/x86/paging.h>
  29. #include <modules/ahci.h>
  30. #include <lib/string.h>
  31. volatile struct ahci_hba_mem *hba;
  32. errno_t ahci_init(struct pci_header0 *hdr) {
  33. schk_init();
  34. register_irq(10, irq0x2a_interrupt, do_ahci_intr);
  35. register_irq(14, irq0x2e_interrupt, do_ahci_intr);
  36. register_irq(15, irq0x2f_interrupt, do_ahci_intr);
  37. ahci_info("PCI BAR0: \t%q\n", hdr->bar0);
  38. ahci_info("PCI BAR1: \t%q\n", hdr->bar1);
  39. ahci_info("PCI BAR2: \t%q\n", hdr->bar2);
  40. ahci_info("PCI BAR3: \t%q\n", hdr->bar3);
  41. ahci_info("PCI BAR4: \t%q\n", hdr->bar4);
  42. ahci_info("PCI BAR5: \t%q\n", hdr->bar5);
  43. hba = (struct ahci_hba_mem *) mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
  44. ahci_info("AHCI HBA MEM maps to %q. \n", hba);
  45. ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr);
  46. ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK);
  47. if (hba->global_ctl & HBA_MEM_GHC_AHCI_ENABLE)
  48. ahci_info("AHCI is already in the AHCI mode. \n");
  49. else
  50. ahci_info("AHCI is in the IDE mode. \n");
  51. hba->global_ctl = 0x80000002;
  52. ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
  53. int i, nrdevs = 0;
  54. for (i = 0; i <= (hba->cap & HBA_MEM_CAP_MASK); i++) {
  55. /* if (bitmap_read(ports_impled, i) == 0) return; */
  56. if (!(hba->ports_impled & (1 << i)))
  57. continue;
  58. ahci_info("found ahci device %d on port %d. \n", nrdevs++, i);
  59. volatile struct ahci_cmd_header *cmdlist = kmalloc(&devpool,
  60. sizeof(struct ahci_cmd_header));
  61. memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
  62. /* well, that needs a physical address, I think... */
  63. hba->ports[i].cmd_list = (__uint64_t) mm_vphy((void *) cmdlist);
  64. volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool,
  65. sizeof(struct ahci_cmd_table));
  66. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  67. cmdlist->cmd_table = (__uint64_t) mm_vphy((void *) cmdlist);
  68. }
  69. hba->ports[0].cmd = hba->ports[0].cmd | 0x10000003;
  70. hba->ports[0].sata_ctl = 0x04;
  71. ahci_info("SATA error: %x. \n", hba->ports[0].sata_error);
  72. ahci_info("interrupt status: %x\n", hba->ports[0].int_status);
  73. schk_leave();
  74. return 0;
  75. }
  76. static void wait_controller_ready(volatile struct ahci_hba_port *port) {
  77. schk_init();
  78. while (port->sata_status & ATASTAT_BUSY && !(port->sata_status & ATASTAT_ERR));
  79. ahci_info("wait_controller_ready: controller is now ready. \n");
  80. schk_leave();
  81. return;
  82. }
  83. int ahci_rw_sect(volatile struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
  84. schk_init();
  85. volatile char *devbuf = kmalloc(&devpool, 512);
  86. wait_controller_ready(port);
  87. if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
  88. memcpy(devbuf, buffer, 512);
  89. ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n",
  90. port, lba, buffer, atacmd);
  91. port->sata_error = port->sata_error;
  92. volatile struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
  93. ahci_info("ahci_rw: command list: %q. \n", mm_vphy((void *) cmdhdr));
  94. memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
  95. cmdhdr->config = (4 << CMDHDR_CFG_CFISLEN_SHIFT) |
  96. CMDHDR_CFG_CLEAR_R_OK |
  97. (0 << CMDHDR_CFG_PORTML_SHIFT) |
  98. (1 << CMDHDR_CFG_PRDTLEN_SHIFT);
  99. if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
  100. cmdhdr->config |= CMDHDR_CFG_WRITE;
  101. cmdhdr->transfered = 0;
  102. volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
  103. ahci_info("ahci_rw: command table: %q. \n", mm_vphy((void *) cmdtable));
  104. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  105. cmdhdr->cmd_table = (__uint64_t) mm_vphy((void *) cmdtable);
  106. volatile struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
  107. ahci_info("ahci_rw: command fis: %q. \n", mm_vphy((void *) cmd_fis));
  108. memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
  109. /* FIS_TYPE_H2D_FIS */
  110. cmd_fis->fis_type = 0x27;
  111. cmd_fis->config = 0 | FISH2D_CMD;
  112. cmd_fis->command = atacmd;
  113. cmd_fis->lba0 = lba & 0xff;
  114. cmd_fis->lba1 = (lba >> 8) & 0xff;
  115. cmd_fis->lba2 = (lba >> 16) & 0xff;
  116. cmd_fis->lba3 = (lba >> 24) & 0xff;
  117. cmd_fis->lba4 = (lba >> 32) & 0xff;
  118. cmd_fis->lba5 = (lba >> 40) & 0xff;
  119. cmd_fis->dev = 0xe0;
  120. cmd_fis->nsectors = 1;
  121. memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d));
  122. cmdtable->prdts[0].buf = (__uint64_t) mm_vphy((void *) devbuf);
  123. cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
  124. port->int_enable = 0xffffffff;
  125. port->int_status = 0xffffffff;
  126. port->cmd_issue = 1;
  127. port->sata_active = 1;
  128. /* interrupt enable */
  129. port->sata_ctl = 0x00;
  130. /* wait it complete */
  131. ahci_info("waiting device complete...\n");
  132. ahci_info("sata error: %q. \n", port->sata_error);
  133. ahci_info("sata status: %q. \n", port->sata_status);
  134. for (;;) {
  135. /* task file error */
  136. if (port->cmd_issue & (1 << 30)) {
  137. ahci_info("ahci_rw_sect: controller error. \n");
  138. goto panic;
  139. }
  140. if (!(port->cmd_issue & 1))
  141. /* complete */
  142. break;
  143. }
  144. if (port->sata_status & ATASTAT_ERR) {
  145. ahci_info("ahci_rw_sect: controller error. \n");
  146. ahci_info("sata error: %q\n", port->sata_status);
  147. goto panic;
  148. }
  149. int i;
  150. for (i = 0; i < 512 / 8; i++)
  151. printk(yellow, "%x", devbuf[i]);
  152. ahci_info("\nAHCI request completed. \n");
  153. port->cmd &= ~1;
  154. if (atacmd == ATACMD_RD || atacmd == ATACMD_RD48)
  155. memcpy(buffer, devbuf, 512);
  156. panic:
  157. kfree(&devpool, (void *) cmd_fis, sizeof(struct fis_h2d));
  158. kfree(&devpool, (void *) cmdtable, sizeof(struct ahci_cmd_table));
  159. kfree(&devpool, (void *) devbuf, 512);
  160. schk_leave();
  161. return 0;
  162. }
  163. void do_ahci_intr(void) {
  164. ahci_info("AHCI interrupt. \n");
  165. return;
  166. }