ahci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. static char buffer[512] = {0};
  33. build_irq(0x2a);
  34. build_irq(0x2e);
  35. build_irq(0x2f);
  36. errno_t ahci_init(struct pci_header0 *hdr) {
  37. schk_init();
  38. register_irq(10, irq0x2a_interrupt, do_ahci_intr);
  39. register_irq(14, irq0x2e_interrupt, do_ahci_intr);
  40. register_irq(15, irq0x2f_interrupt, do_ahci_intr);
  41. ahci_info("PCI BAR0: \t%q\n", hdr->bar0);
  42. ahci_info("PCI BAR1: \t%q\n", hdr->bar1);
  43. ahci_info("PCI BAR2: \t%q\n", hdr->bar2);
  44. ahci_info("PCI BAR3: \t%q\n", hdr->bar3);
  45. ahci_info("PCI BAR4: \t%q\n", hdr->bar4);
  46. ahci_info("PCI BAR5: \t%q\n", hdr->bar5);
  47. hba = (struct ahci_hba_mem *) mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
  48. ahci_info("AHCI HBA MEM maps to %q. \n", hba);
  49. ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr);
  50. ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK);
  51. if (hba->global_ctl & HBA_MEM_GHC_AHCI_ENABLE)
  52. ahci_info("AHCI is already in the AHCI mode. \n");
  53. else
  54. ahci_info("AHCI is in the IDE mode. \n");
  55. hba->global_ctl = 0x80000002;
  56. mfence();
  57. ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
  58. int i, nrdevs = 0;
  59. for (i = 0; i <= (hba->cap & HBA_MEM_CAP_MASK); i++) {
  60. /* if (bitmap_read(ports_impled, i) == 0) return; */
  61. if (!(hba->ports_impled & (1 << i)))
  62. continue;
  63. ahci_info("found ahci device %d on port %d. \n", nrdevs++, i);
  64. volatile struct ahci_cmd_header *cmdlist = kmalloc(&devpool,
  65. sizeof(struct ahci_cmd_header));
  66. memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
  67. /* well, that needs a physical address, I think... */
  68. hba->ports[i].cmd_list = (__uint64_t) mm_vphy((void *) cmdlist);
  69. volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool,
  70. sizeof(struct ahci_cmd_table));
  71. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  72. cmdlist->cmd_table = (__uint64_t) mm_vphy((void *) cmdlist);
  73. }
  74. hba->ports[0].cmd = hba->ports[0].cmd | 0x10000003;
  75. hba->ports[0].sata_ctl = 0x04;
  76. mfence();
  77. ahci_info("SATA error: %x. \n", hba->ports[0].sata_error);
  78. ahci_info("interrupt status: %x\n", hba->ports[0].int_status);
  79. ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 2);
  80. schk_leave();
  81. return 0;
  82. }
  83. static void wait_controller_ready(volatile struct ahci_hba_port *port) {
  84. schk_init();
  85. while (port->sata_status & ATASTAT_BUSY && !(port->sata_status & ATASTAT_ERR));
  86. ahci_info("wait_controller_ready: controller is now ready. \n");
  87. schk_leave();
  88. return;
  89. }
  90. int ahci_rw_sect(volatile struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
  91. schk_init();
  92. volatile char *devbuf = kmalloc(&devpool, 512);
  93. wait_controller_ready(port);
  94. if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
  95. memcpy(devbuf, buffer, 512);
  96. ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n",
  97. port, lba, buffer, atacmd);
  98. port->sata_error = port->sata_error;
  99. volatile struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
  100. ahci_info("ahci_rw: command list: %q. \n", mm_vphy((void *) cmdhdr));
  101. memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
  102. cmdhdr->config = (4 << CMDHDR_CFG_CFISLEN_SHIFT) |
  103. CMDHDR_CFG_CLEAR_R_OK |
  104. (0 << CMDHDR_CFG_PORTML_SHIFT) |
  105. (1 << CMDHDR_CFG_PRDTLEN_SHIFT);
  106. if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48)
  107. cmdhdr->config |= CMDHDR_CFG_WRITE;
  108. cmdhdr->transfered = 0;
  109. volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
  110. ahci_info("ahci_rw: command table: %q. \n", mm_vphy((void *) cmdtable));
  111. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  112. cmdhdr->cmd_table = (__uint64_t) mm_vphy((void *) cmdtable);
  113. volatile struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
  114. ahci_info("ahci_rw: command fis: %q. \n", mm_vphy((void *) cmd_fis));
  115. memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
  116. /* FIS_TYPE_H2D_FIS */
  117. cmd_fis->fis_type = 0x27;
  118. cmd_fis->config = 0 | FISH2D_CMD;
  119. cmd_fis->command = atacmd;
  120. cmd_fis->lba0 = lba & 0xff;
  121. cmd_fis->lba1 = (lba >> 8) & 0xff;
  122. cmd_fis->lba2 = (lba >> 16) & 0xff;
  123. cmd_fis->lba3 = (lba >> 24) & 0xff;
  124. cmd_fis->lba4 = (lba >> 32) & 0xff;
  125. cmd_fis->lba5 = (lba >> 40) & 0xff;
  126. cmd_fis->dev = 0xe0;
  127. cmd_fis->nsectors = 1;
  128. memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d));
  129. cmdtable->prdts[0].buf = (__uint64_t) mm_vphy((void *) devbuf);
  130. cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
  131. port->int_enable = 0xffffffff;
  132. port->int_status = 0xffffffff;
  133. port->cmd_issue = 1;
  134. port->sata_active = 1;
  135. /* interrupt enable */
  136. port->sata_ctl = 0x00;
  137. mfence();
  138. /* wait it complete */
  139. ahci_info("waiting device complete...\n");
  140. ahci_info("sata error: %q. \n", port->sata_error);
  141. ahci_info("sata status: %q. \n", port->sata_status);
  142. for (;;) {
  143. /* task file error */
  144. if (port->cmd_issue & (1 << 30)) {
  145. ahci_info("ahci_rw_sect: controller error. \n");
  146. goto panic;
  147. }
  148. if (!(port->cmd_issue & 1))
  149. /* complete */
  150. break;
  151. }
  152. if (port->sata_status & ATASTAT_ERR) {
  153. ahci_info("ahci_rw_sect: controller error. \n");
  154. ahci_info("sata error: %q\n", port->sata_status);
  155. goto panic;
  156. }
  157. int i;
  158. for (i = 0; i < 512 / 8; i++)
  159. printk(yellow, "%x", devbuf[i]);
  160. ahci_info("\nAHCI request completed. \n");
  161. port->cmd &= ~1;
  162. if (atacmd == ATACMD_RD || atacmd == ATACMD_RD48)
  163. memcpy(buffer, devbuf, 512);
  164. panic:
  165. kfree(&devpool, (void *) cmd_fis, sizeof(struct fis_h2d));
  166. kfree(&devpool, (void *) cmdtable, sizeof(struct ahci_cmd_table));
  167. kfree(&devpool, (void *) devbuf, 512);
  168. schk_leave();
  169. return 0;
  170. }
  171. void do_ahci_intr(void) {
  172. ahci_info("AHCI interrupt. \n");
  173. return;
  174. }