ahci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <arch/x86/paging.h>
  28. #include <modules/ahci.h>
  29. #include <lib/string.h>
  30. struct ahci_hba_mem *hba;
  31. static char buffer[512];
  32. build_irq(0x2e);
  33. build_irq(0x2f);
  34. errno_t ahci_init(struct pci_header0 *hdr) {
  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. hba->global_ctl |= HBA_MEM_GHC_AHCI_ENABLE;
  48. ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
  49. if (!(hba->bios_handoff & HBA_MEM_BHO_OWN_OS)) {
  50. /* get the ownership */
  51. hba->bios_handoff = (hba->bios_handoff & ~HBA_MEM_BHO_OWN_RWC) |
  52. HBA_MEM_BHO_OWN_OS;
  53. int timeout = 4096;
  54. while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--);
  55. if (!timeout) {
  56. hba->bios_handoff = HBA_MEM_BHO_OWN_OS;
  57. hba->bios_handoff |= HBA_MEM_BHO_OWN_CHANGED;
  58. }
  59. ahci_info("got the ownership. \n");
  60. ahci_info("current AHCI BIOS-handoff: %d\n", hba->bios_handoff);
  61. }
  62. else
  63. ahci_info("AHCI is already in the OS mode. \n");
  64. int i, nrdevs = 0;
  65. for (i = 0; i < (hba->cap & HBA_MEM_CAP_MASK); i++) {
  66. /* if (bitmap_read(ports_impled, i) == 0) return; */
  67. if (!(hba->ports_impled & (1 << i)))
  68. continue;
  69. ahci_info("found ahci device %d on port %d. ", nrdevs++, i);
  70. struct ahci_cmd_header *cmdlist = kmalloc(&devpool,
  71. sizeof(struct ahci_cmd_header));
  72. memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
  73. /* well, that needs a physical address, I think... */
  74. hba->ports[i].cmd_list = (__uint64_t) mm_vphy(cmdlist);
  75. struct ahci_cmd_table *cmdtable = kmalloc(&devpool,
  76. sizeof(struct ahci_cmd_table));
  77. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  78. cmdlist->cmd_table = (__uint64_t) mm_vphy(cmdlist);
  79. }
  80. ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0);
  81. return 0;
  82. }
  83. int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
  84. ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n",
  85. port, lba, buffer, atacmd);
  86. struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
  87. ahci_info("ahci_rw: command list: %q. \n", cmdhdr);
  88. memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
  89. cmdhdr->config = (4 << CMDHDR_CFG_CFISLEN_SHIFT) |
  90. CMDHDR_CFG_CLEAR_R_OK |
  91. (0 << CMDHDR_CFG_PORTML_SHIFT) |
  92. (1 << CMDHDR_CFG_PRDTLEN_SHIFT) |
  93. ((atacmd == ATACMD_WR || atacmd == ATACMD_WR48) ?
  94. CMDHDR_CFG_WRITE : 0);
  95. cmdhdr->transfered = 0;
  96. struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
  97. memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
  98. cmdhdr->cmd_table = (__uint64_t) mm_vphy(cmdtable);
  99. struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
  100. memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
  101. /* FIS_TYPE_H2D_FIS */
  102. cmd_fis->fis_type = 0x27;
  103. cmd_fis->config = 0 | FISH2D_CMD;
  104. cmd_fis->command = atacmd;
  105. cmd_fis->lba0 = lba & 0xff;
  106. cmd_fis->lba1 = (lba >> 8) & 0xff;
  107. cmd_fis->lba2 = (lba >> 16) & 0xff;
  108. cmd_fis->lba3 = (lba >> 24) & 0xff;
  109. cmd_fis->lba4 = (lba >> 32) & 0xff;
  110. cmd_fis->lba5 = (lba >> 40) & 0xff;
  111. cmd_fis->dev = 0xe0;
  112. cmd_fis->nsectors = 1;
  113. memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d));
  114. cmdtable->prdts[0].buf = (__uint64_t) buffer;
  115. cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
  116. port->int_enable = 1 << 5;
  117. port->int_status = 1 << 5;
  118. port->cmd_issue |= 1;
  119. port->cmd |= 1;
  120. /* wait it complete */
  121. ahci_info("waiting device complete...\n");
  122. while (!(port->int_status & (1 << 5)));
  123. ahci_info("AHCI request completed. \n");
  124. port->cmd &= ~1;
  125. return 0;
  126. }
  127. void do_ahci_intr(void) {
  128. ahci_info("AHCI interrupt. \n");
  129. return;
  130. }