ahci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. volatile struct ahci_hba *hba;
  31. __uint64_t ahci_buffer;
  32. build_irq(0x2a);
  33. static char buf[512] = {0};
  34. /* calls from pci_init() */
  35. errno_t ahci_init(struct pci_header0 *hdr) {
  36. ahci_info("bar0: %q\n", hdr->bar0);
  37. ahci_info("bar1: %q\n", hdr->bar1);
  38. ahci_info("bar2: %q\n", hdr->bar2);
  39. ahci_info("bar3: %q\n", hdr->bar3);
  40. ahci_info("bar4: %q\n", hdr->bar4);
  41. ahci_info("bar5: %q\n", hdr->bar5);
  42. ahci_info("irq: %b\n", hdr->irqnr);
  43. /* map the bar5 to memory */
  44. hba = mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
  45. ahci_info("hba mem maps to the %q. \n", hba);
  46. /* ahci enable, interrupt enable, hba reset */
  47. hba->ghc = 0x8000000;
  48. register_irq(0x20 + 0x0a, irq0x2a_interrupt, do_ahci_intr);
  49. struct cmd_header *cmdhdr = kmalloc(&devpool, sizeof(struct cmd_header));
  50. if (!cmdhdr)
  51. panic("ahci_init: failed to allocate space for ahci command header. \n");
  52. hba->portctl[0].cmd_listl = (__uint32_t) ((__uint64_t) cmdhdr) & 0xffffffff;
  53. hba->portctl[0].cmd_listh = (__uint32_t) ((__uint64_t) cmdhdr >> 32) & 0xffffffff;
  54. ahci_info("current ahci list address: %q. \n", (__uint64_t) hba->portctl[0].cmd_listl);
  55. struct cmd_table *cmdtbl = alloc_page_att(PATT_DEVMM);
  56. ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
  57. ->cmd_table_low = (__uint32_t) ((__uint64_t) cmdtbl) & 0xffffffff;
  58. ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
  59. ->cmd_table_high = (__uint32_t) ((__uint64_t) cmdtbl >> 32) & 0xffffffff;
  60. ahci_info("current ahci command table address: %q. \n",
  61. (__uint64_t) ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
  62. ->cmd_table_low);
  63. ahci_info("reading the sector 1 from the ahci disk...\n");
  64. ahci_read(hba->portctl, 0x0000000000000001, 1, buf);
  65. return 0;
  66. }
  67. void do_ahci_intr(void) {
  68. ahci_info("irq 0x0e: sata master disk. \n");
  69. ahci_info("disk data: \n");
  70. int i;
  71. for (i = 0; i < 512 >> 3; i++)
  72. ahci_info("%q", ((__uint64_t *) buf)[i]);
  73. return;
  74. }
  75. static int find_free_cmdslot(volatile struct ahci_hba_portctl *port) {
  76. int i;
  77. __uint32_t slot = (port->active | port->ci);
  78. for (i = 0; i < 32; i++) {
  79. if (!(slot & 1))
  80. return i;
  81. slot >>= 1;
  82. }
  83. ahci_info("find_free_cmdslot: warning: can't find a free slot");
  84. return 0;
  85. }
  86. void ahci_read(volatile struct ahci_hba_portctl *port, __uint64_t lba, __uint16_t nsectors, char *buf) {
  87. int slot = find_free_cmdslot(port);
  88. ahci_info("ahci_read: free cmd slot: %d. \n", slot);
  89. port->int_status = -1;
  90. struct cmd_header *cmdhdr = (struct cmd_header *) (__uint64_t) port->cmd_listl;
  91. ahci_info("ahci_read: command header address: %q. \n", cmdhdr);
  92. cmdhdr += slot;
  93. cmdhdr->fis_len = sizeof(struct fis_h2d) / sizeof(int);
  94. cmdhdr->wr = 0;
  95. cmdhdr->atapi = 0;
  96. cmdhdr->clear = 1;
  97. cmdhdr->pre = 1;
  98. /* the max size of a prdt is 8kb (16 sectors, >> 4) */
  99. cmdhdr->prdt_len = ((nsectors - 1) >> 4) + 1;
  100. struct cmd_table *cmdtbl = (struct cmd_table *) (__uint64_t) cmdhdr->cmd_table_low;
  101. ahci_info("ahci_read: command table address: %q. \n", cmdtbl);
  102. int i;
  103. /* setup first 1 --- (last-1), and setup the last one later */
  104. for (i = 0; i < cmdhdr->prdt_len - 1; i++) {
  105. memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
  106. cmdtbl->prdts[i].buf_low = (__uint32_t) (__uint64_t) buf & 0xffffffff;
  107. cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
  108. /* max 8kb */
  109. cmdtbl->prdts[i].nbytes = 8191;
  110. cmdtbl->prdts[i].irq = 1;
  111. buf += 8192;
  112. }
  113. memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
  114. cmdtbl->prdts[i].buf_low = (__uint32_t) ((__uint64_t) buf) & 0xffffffff;
  115. cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
  116. cmdtbl->prdts[i].nbytes = (nsectors << 9) % 8192 - 1;
  117. cmdtbl->prdts[i].irq = 1;
  118. struct fis_h2d *h2dfis = (struct fis_h2d *) &cmdtbl->cmd_fis;
  119. h2dfis->fis_type = FIS_HOSTTODEV;
  120. h2dfis->cmd = 1;
  121. h2dfis->command = ATACMD_RDDMA;
  122. h2dfis->lba0 = lba & 0xff;
  123. h2dfis->lba1 = (lba >> 8) & 0xff;
  124. h2dfis->lba2 = (lba >> 16) & 0xff;
  125. h2dfis->lba3 = (lba >> 24) & 0xff;
  126. h2dfis->lba4 = (lba >> 32) & 0xff;
  127. h2dfis->lba5 = (lba >> 40) & 0xff;
  128. h2dfis->count_low = nsectors & 0xff;
  129. h2dfis->count_high = (nsectors >> 8) & 0xff;
  130. h2dfis->dev = 0xe0;
  131. port->active = 1;
  132. port->cmd |= 0x10000003;
  133. port->ci = 1;
  134. return;
  135. }