ahci.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "ahci.h"
  2. #include "../../../common/kprint.h"
  3. #include "../../../mm/slab.h"
  4. struct pci_device_structure_header_t *ahci_devs[MAX_AHCI_DEVICES];
  5. uint32_t count_ahci_devices = 0;
  6. uint64_t ahci_port_base_vaddr; // 端口映射base addr
  7. static void start_cmd(HBA_PORT *port);
  8. static void stop_cmd(HBA_PORT *port);
  9. static void port_rebase(HBA_PORT *port, int portno);
  10. // Find a free command list slot
  11. static int ahci_find_cmdslot(HBA_PORT *port);
  12. // 计算HBA_MEM的虚拟内存地址
  13. #define cal_HBA_MEM_VIRT_ADDR(device_num) (AHCI_MAPPING_BASE + (ul)(((struct pci_device_structure_general_device_t *)(ahci_devs[device_num]))->BAR5 - ((((struct pci_device_structure_general_device_t *)(ahci_devs[0]))->BAR5) & PAGE_2M_MASK)))
  14. /**
  15. * @brief 初始化ahci模块
  16. *
  17. */
  18. void ahci_init()
  19. {
  20. pci_get_device_structure(0x1, 0x6, ahci_devs, &count_ahci_devices);
  21. kdebug("phys addr=%#018lx", (ul)(((struct pci_device_structure_general_device_t *)(ahci_devs[0]))->BAR5));
  22. // 映射ABAR
  23. mm_map_phys_addr(AHCI_MAPPING_BASE, ((ul)(((struct pci_device_structure_general_device_t *)(ahci_devs[0]))->BAR5)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
  24. kdebug("ABAR mapped!");
  25. for (int i = 0; i < count_ahci_devices; ++i)
  26. {
  27. kdebug("[%d] class_code=%d, sub_class=%d, progIF=%d, ABAR=%#010lx", i, ahci_devs[i]->Class_code, ahci_devs[i]->SubClass, ahci_devs[i]->ProgIF, ((struct pci_device_structure_general_device_t *)(ahci_devs[i]))->BAR5);
  28. // 赋值HBA_MEM结构体
  29. ahci_devices[i].dev_struct = ahci_devs[i];
  30. ahci_devices[i].hba_mem = (HBA_MEM *)(cal_HBA_MEM_VIRT_ADDR(i));
  31. }
  32. ahci_port_base_vaddr = (uint64_t)kmalloc(1048576, 0);
  33. ahci_probe_port(0);
  34. port_rebase(&ahci_devices[0].hba_mem->ports[0], 0);
  35. uint64_t buf[100];
  36. bool res = ahci_read(&(ahci_devices[0].hba_mem->ports[0]), 0, 0, 1, (uint64_t)&buf);
  37. kdebug("res=%d, buf[0]=%#010lx", (uint)res, (uint32_t)buf[0]);
  38. }
  39. // Check device type
  40. static int check_type(HBA_PORT *port)
  41. {
  42. uint32_t ssts = port->ssts;
  43. uint8_t ipm = (ssts >> 8) & 0x0F;
  44. uint8_t det = ssts & 0x0F;
  45. if (det != HBA_PORT_DET_PRESENT) // Check drive status
  46. return AHCI_DEV_NULL;
  47. if (ipm != HBA_PORT_IPM_ACTIVE)
  48. return AHCI_DEV_NULL;
  49. switch (port->sig)
  50. {
  51. case SATA_SIG_ATAPI:
  52. return AHCI_DEV_SATAPI;
  53. case SATA_SIG_SEMB:
  54. return AHCI_DEV_SEMB;
  55. case SATA_SIG_PM:
  56. return AHCI_DEV_PM;
  57. default:
  58. return AHCI_DEV_SATA;
  59. }
  60. }
  61. /**
  62. * @brief 检测端口连接的设备的类型
  63. *
  64. * @param device_num ahci设备号
  65. */
  66. void ahci_probe_port(const uint32_t device_num)
  67. {
  68. HBA_MEM *abar = ahci_devices[device_num].hba_mem;
  69. uint32_t pi = abar->pi;
  70. for (int i = 0; i < 32; ++i, (pi >>= 1))
  71. {
  72. if (pi & 1)
  73. {
  74. uint dt = check_type(&abar->ports[i]);
  75. ahci_devices[i].type = dt;
  76. if (dt == AHCI_DEV_SATA)
  77. {
  78. kdebug("SATA drive found at port %d", i);
  79. }
  80. else if (dt == AHCI_DEV_SATAPI)
  81. {
  82. kdebug("SATAPI drive found at port %d", i);
  83. }
  84. else if (dt == AHCI_DEV_SEMB)
  85. {
  86. kdebug("SEMB drive found at port %d", i);
  87. }
  88. else if (dt == AHCI_DEV_PM)
  89. {
  90. kdebug("PM drive found at port %d", i);
  91. }
  92. else
  93. {
  94. kdebug("No drive found at port %d", i);
  95. }
  96. }
  97. }
  98. }
  99. // Start command engine
  100. static void start_cmd(HBA_PORT *port)
  101. {
  102. // Wait until CR (bit15) is cleared
  103. while (port->cmd & HBA_PxCMD_CR)
  104. ;
  105. // Set FRE (bit4) and ST (bit0)
  106. port->cmd |= HBA_PxCMD_FRE;
  107. port->cmd |= HBA_PxCMD_ST;
  108. }
  109. // Stop command engine
  110. static void stop_cmd(HBA_PORT *port)
  111. {
  112. // Clear ST (bit0)
  113. port->cmd &= ~HBA_PxCMD_ST;
  114. // Clear FRE (bit4)
  115. port->cmd &= ~HBA_PxCMD_FRE;
  116. // Wait until FR (bit14), CR (bit15) are cleared
  117. while (1)
  118. {
  119. if (port->cmd & HBA_PxCMD_FR)
  120. continue;
  121. if (port->cmd & HBA_PxCMD_CR)
  122. continue;
  123. break;
  124. }
  125. }
  126. static void port_rebase(HBA_PORT *port, int portno)
  127. {
  128. // Before rebasing Port memory space, OS must wait for current pending commands to finish
  129. // and tell HBA to stop receiving FIS from the port. Otherwise an accidently incoming FIS may be
  130. // written into a partially configured memory area.
  131. stop_cmd(port); // Stop command engine
  132. // Command list offset: 1K*portno
  133. // Command list entry size = 32
  134. // Command list entry maxim count = 32
  135. // Command list maxim size = 32*32 = 1K per port
  136. port->clb = ahci_port_base_vaddr + (portno << 10);
  137. memset((void *)(port->clb), 0, 1024);
  138. // FIS offset: 32K+256*portno
  139. // FIS entry size = 256 bytes per port
  140. port->fb = ahci_port_base_vaddr + (32 << 10) + (portno << 8);
  141. memset((void *)(port->fb), 0, 256);
  142. // Command table offset: 40K + 8K*portno
  143. // Command table size = 256*32 = 8K per port
  144. HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER *)(port->clb);
  145. for (int i = 0; i < 32; ++i)
  146. {
  147. cmdheader[i].prdtl = 8; // 8 prdt entries per command table
  148. // 256 bytes per command table, 64+16+48+16*8
  149. // Command table offset: 40K + 8K*portno + cmdheader_index*256
  150. cmdheader[i].ctba = ahci_port_base_vaddr + (40 << 10) + (portno << 13) + (i << 8);
  151. memset((void *)cmdheader[i].ctba, 0, 256);
  152. }
  153. start_cmd(port); // Start command engine
  154. }
  155. /**
  156. * @brief read data from SATA device using 48bit LBA address
  157. *
  158. * @param port HBA PORT
  159. * @param startl low 32bits of start addr
  160. * @param starth high 32bits of start addr
  161. * @param count total sectors to read
  162. * @param buf buffer
  163. * @return true done
  164. * @return false failed
  165. */
  166. bool ahci_read(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t count, uint64_t buf)
  167. {
  168. port->is = (uint32_t)-1; // Clear pending interrupt bits
  169. int spin = 0; // Spin lock timeout counter
  170. int slot = ahci_find_cmdslot(port);
  171. if (slot == -1)
  172. return false;
  173. HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER *)port->clb;
  174. cmdheader += slot;
  175. cmdheader->cfl = sizeof(FIS_REG_H2D) / sizeof(uint32_t); // Command FIS size
  176. cmdheader->w = 0; // Read from device
  177. cmdheader->prdtl = (uint16_t)((count - 1) >> 4) + 1; // PRDT entries count
  178. HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL *)(cmdheader->ctba);
  179. memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) + (cmdheader->prdtl - 1) * sizeof(HBA_PRDT_ENTRY));
  180. // 8K bytes (16 sectors) per PRDT
  181. int i;
  182. for (i = 0; i < cmdheader->prdtl - 1; ++i)
  183. {
  184. cmdtbl->prdt_entry[i].dba = buf;
  185. cmdtbl->prdt_entry[i].dbc = 8 * 1024 - 1; // 8K bytes (this value should always be set to 1 less than the actual value)
  186. cmdtbl->prdt_entry[i].i = 1;
  187. buf += 4 * 1024; // 4K words
  188. count -= 16; // 16 sectors
  189. }
  190. // Last entry
  191. cmdtbl->prdt_entry[i].dba = buf;
  192. cmdtbl->prdt_entry[i].dbc = (count << 9) - 1; // 512 bytes per sector
  193. cmdtbl->prdt_entry[i].i = 1;
  194. // Setup command
  195. FIS_REG_H2D *cmdfis = (FIS_REG_H2D *)(&cmdtbl->cfis);
  196. cmdfis->fis_type = FIS_TYPE_REG_H2D;
  197. cmdfis->c = 1; // Command
  198. cmdfis->command = ATA_CMD_READ_DMA_EXT;
  199. cmdfis->lba0 = (uint8_t)startl;
  200. cmdfis->lba1 = (uint8_t)(startl >> 8);
  201. cmdfis->lba2 = (uint8_t)(startl >> 16);
  202. cmdfis->device = 1 << 6; // LBA mode
  203. cmdfis->lba3 = (uint8_t)(startl >> 24);
  204. cmdfis->lba4 = (uint8_t)starth;
  205. cmdfis->lba5 = (uint8_t)(starth >> 8);
  206. cmdfis->countl = count & 0xFF;
  207. cmdfis->counth = (count >> 8) & 0xFF;
  208. // The below loop waits until the port is no longer busy before issuing a new command
  209. while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000)
  210. {
  211. spin++;
  212. }
  213. if (spin == 1000000)
  214. {
  215. kerror("Port is hung");
  216. return false;
  217. }
  218. kdebug("slot=%d", slot);
  219. port->ci = 1 << slot; // Issue command
  220. // Wait for completion
  221. while (1)
  222. {
  223. // In some longer duration reads, it may be helpful to spin on the DPS bit
  224. // in the PxIS port field as well (1 << 5)
  225. if ((port->ci & (1 << slot)) == 0)
  226. break;
  227. if (port->is & HBA_PxIS_TFES) // Task file error
  228. {
  229. kerror("Read disk error");
  230. return false;
  231. }
  232. }
  233. // Check again
  234. if (port->is & HBA_PxIS_TFES)
  235. {
  236. kerror("Read disk error");
  237. return false;
  238. }
  239. return true;
  240. }
  241. // Find a free command list slot
  242. static int ahci_find_cmdslot(HBA_PORT *port)
  243. {
  244. // If not set in SACT and CI, the slot is free
  245. uint32_t slots = (port->sact | port->ci);
  246. int num_of_cmd_clots = (ahci_devices[0].hba_mem->cap&0x0f00)>>8; // bit 12-8
  247. for (int i = 0; i < num_of_cmd_clots; i++)
  248. {
  249. if ((slots & 1) == 0)
  250. return i;
  251. slots >>= 1;
  252. }
  253. kerror("Cannot find free command list entry");
  254. return -1;
  255. }