ahci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. static long ahci_query_disk();
  11. // Find a free command list slot
  12. static int ahci_find_cmdslot(HBA_PORT *port);
  13. // 计算HBA_MEM的虚拟内存地址
  14. #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)))
  15. /**
  16. * @brief 初始化ahci模块
  17. *
  18. */
  19. void ahci_init()
  20. {
  21. pci_get_device_structure(0x1, 0x6, ahci_devs, &count_ahci_devices);
  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. // 初始化请求队列
  36. ahci_req_queue.in_service = NULL;
  37. list_init(&(ahci_req_queue.queue_list));
  38. ahci_req_queue.request_count = 0;
  39. }
  40. // Check device type
  41. static int check_type(HBA_PORT *port)
  42. {
  43. uint32_t ssts = port->ssts;
  44. uint8_t ipm = (ssts >> 8) & 0x0F;
  45. uint8_t det = ssts & 0x0F;
  46. if (det != HBA_PORT_DET_PRESENT) // Check drive status
  47. return AHCI_DEV_NULL;
  48. if (ipm != HBA_PORT_IPM_ACTIVE)
  49. return AHCI_DEV_NULL;
  50. switch (port->sig)
  51. {
  52. case SATA_SIG_ATAPI:
  53. return AHCI_DEV_SATAPI;
  54. case SATA_SIG_SEMB:
  55. return AHCI_DEV_SEMB;
  56. case SATA_SIG_PM:
  57. return AHCI_DEV_PM;
  58. default:
  59. return AHCI_DEV_SATA;
  60. }
  61. }
  62. /**
  63. * @brief 检测端口连接的设备的类型
  64. *
  65. * @param device_num ahci控制器号
  66. */
  67. static void ahci_probe_port(const uint32_t device_num)
  68. {
  69. HBA_MEM *abar = ahci_devices[device_num].hba_mem;
  70. uint32_t pi = abar->pi;
  71. for (int i = 0; i < 32; ++i, (pi >>= 1))
  72. {
  73. if (pi & 1)
  74. {
  75. uint dt = check_type(&abar->ports[i]);
  76. ahci_devices[i].type = dt;
  77. if (dt == AHCI_DEV_SATA)
  78. {
  79. kdebug("SATA drive found at port %d", i);
  80. }
  81. else if (dt == AHCI_DEV_SATAPI)
  82. {
  83. kdebug("SATAPI drive found at port %d", i);
  84. }
  85. else if (dt == AHCI_DEV_SEMB)
  86. {
  87. kdebug("SEMB drive found at port %d", i);
  88. }
  89. else if (dt == AHCI_DEV_PM)
  90. {
  91. kdebug("PM drive found at port %d", i);
  92. }
  93. else
  94. {
  95. // kdebug("No drive found at port %d", i);
  96. }
  97. }
  98. }
  99. }
  100. // Start command engine
  101. static void start_cmd(HBA_PORT *port)
  102. {
  103. // Wait until CR (bit15) is cleared
  104. while (port->cmd & HBA_PxCMD_CR)
  105. ;
  106. // Set FRE (bit4) and ST (bit0)
  107. port->cmd |= HBA_PxCMD_FRE;
  108. port->cmd |= HBA_PxCMD_ST;
  109. }
  110. // Stop command engine
  111. static void stop_cmd(HBA_PORT *port)
  112. {
  113. // Clear ST (bit0)
  114. port->cmd &= ~HBA_PxCMD_ST;
  115. // Clear FRE (bit4)
  116. port->cmd &= ~HBA_PxCMD_FRE;
  117. // Wait until FR (bit14), CR (bit15) are cleared
  118. while (1)
  119. {
  120. if (port->cmd & HBA_PxCMD_FR)
  121. continue;
  122. if (port->cmd & HBA_PxCMD_CR)
  123. continue;
  124. break;
  125. }
  126. }
  127. static void port_rebase(HBA_PORT *port, int portno)
  128. {
  129. // Before rebasing Port memory space, OS must wait for current pending commands to finish
  130. // and tell HBA to stop receiving FIS from the port. Otherwise an accidently incoming FIS may be
  131. // written into a partially configured memory area.
  132. stop_cmd(port); // Stop command engine
  133. // Command list offset: 1K*portno
  134. // Command list entry size = 32
  135. // Command list entry maxim count = 32
  136. // Command list maxim size = 32*32 = 1K per port
  137. port->clb = ahci_port_base_vaddr + (portno << 10);
  138. memset((void *)(port->clb), 0, 1024);
  139. // FIS offset: 32K+256*portno
  140. // FIS entry size = 256 bytes per port
  141. port->fb = ahci_port_base_vaddr + (32 << 10) + (portno << 8);
  142. memset((void *)(port->fb), 0, 256);
  143. // Command table offset: 40K + 8K*portno
  144. // Command table size = 256*32 = 8K per port
  145. HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER *)(port->clb);
  146. for (int i = 0; i < 32; ++i)
  147. {
  148. cmdheader[i].prdtl = 8; // 8 prdt entries per command table
  149. // 256 bytes per command table, 64+16+48+16*8
  150. // Command table offset: 40K + 8K*portno + cmdheader_index*256
  151. cmdheader[i].ctba = ahci_port_base_vaddr + (40 << 10) + (portno << 13) + (i << 8);
  152. memset((void *)cmdheader[i].ctba, 0, 256);
  153. }
  154. start_cmd(port); // Start command engine
  155. }
  156. /**
  157. * @brief read data from SATA device using 48bit LBA address
  158. *
  159. * @param port HBA PORT
  160. * @param startl low 32bits of start addr
  161. * @param starth high 32bits of start addr
  162. * @param count total sectors to read
  163. * @param buf buffer
  164. * @return true done
  165. * @return false failed
  166. */
  167. static bool ahci_read(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t count, uint64_t buf)
  168. {
  169. port->is = (uint32_t)-1; // Clear pending interrupt bits
  170. int spin = 0; // Spin lock timeout counter
  171. int slot = ahci_find_cmdslot(port);
  172. if (slot == -1)
  173. return E_NOEMPTYSLOT;
  174. HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER *)port->clb;
  175. cmdheader += slot;
  176. cmdheader->cfl = sizeof(FIS_REG_H2D) / sizeof(uint32_t); // Command FIS size
  177. cmdheader->w = 0; // Read from device
  178. cmdheader->prdtl = (uint16_t)((count - 1) >> 4) + 1; // PRDT entries count
  179. HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL *)(cmdheader->ctba);
  180. memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) + (cmdheader->prdtl - 1) * sizeof(HBA_PRDT_ENTRY));
  181. // 8K bytes (16 sectors) per PRDT
  182. int i;
  183. for (i = 0; i < cmdheader->prdtl - 1; ++i)
  184. {
  185. cmdtbl->prdt_entry[i].dba = buf;
  186. cmdtbl->prdt_entry[i].dbc = 8 * 1024 - 1; // 8K bytes (this value should always be set to 1 less than the actual value)
  187. cmdtbl->prdt_entry[i].i = 1;
  188. buf += 4 * 1024; // 4K uint16_ts
  189. count -= 16; // 16 sectors
  190. }
  191. // Last entry
  192. cmdtbl->prdt_entry[i].dba = buf;
  193. cmdtbl->prdt_entry[i].dbc = (count << 9) - 1; // 512 bytes per sector
  194. cmdtbl->prdt_entry[i].i = 1;
  195. // Setup command
  196. FIS_REG_H2D *cmdfis = (FIS_REG_H2D *)(&cmdtbl->cfis);
  197. cmdfis->fis_type = FIS_TYPE_REG_H2D;
  198. cmdfis->c = 1; // Command
  199. cmdfis->command = ATA_CMD_READ_DMA_EXT;
  200. cmdfis->lba0 = (uint8_t)startl;
  201. cmdfis->lba1 = (uint8_t)(startl >> 8);
  202. cmdfis->lba2 = (uint8_t)(startl >> 16);
  203. cmdfis->device = 1 << 6; // LBA mode
  204. cmdfis->lba3 = (uint8_t)(startl >> 24);
  205. cmdfis->lba4 = (uint8_t)starth;
  206. cmdfis->lba5 = (uint8_t)(starth >> 8);
  207. cmdfis->countl = count & 0xFF;
  208. cmdfis->counth = (count >> 8) & 0xFF;
  209. // The below loop waits until the port is no longer busy before issuing a new command
  210. while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000)
  211. {
  212. spin++;
  213. }
  214. if (spin == 1000000)
  215. {
  216. kerror("Port is hung");
  217. return E_PORT_HUNG;
  218. }
  219. kdebug("slot=%d", slot);
  220. port->ci = 1 << slot; // Issue command
  221. // Wait for completion
  222. while (1)
  223. {
  224. // In some longer duration reads, it may be helpful to spin on the DPS bit
  225. // in the PxIS port field as well (1 << 5)
  226. if ((port->ci & (1 << slot)) == 0)
  227. break;
  228. if (port->is & HBA_PxIS_TFES) // Task file error
  229. {
  230. kerror("Read disk error");
  231. return E_TASK_FILE_ERROR;
  232. }
  233. }
  234. // Check again
  235. if (port->is & HBA_PxIS_TFES)
  236. {
  237. kerror("Read disk error");
  238. return E_TASK_FILE_ERROR;
  239. }
  240. return AHCI_SUCCESS;
  241. }
  242. static bool ahci_write(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t count,
  243. uint64_t buf)
  244. {
  245. port->is = 0xffff; // Clear pending interrupt bits
  246. int slot = ahci_find_cmdslot(port);
  247. if (slot == -1)
  248. return E_NOEMPTYSLOT;
  249. HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER *)port->clb;
  250. cmdheader += slot;
  251. cmdheader->cfl = sizeof(FIS_REG_H2D) / sizeof(uint32_t); // Command FIS size
  252. cmdheader->w = 1;
  253. cmdheader->c = 1;
  254. cmdheader->p = 1;
  255. cmdheader->prdtl = (uint16_t)((count - 1) >> 4) + 1; // PRDT entries count
  256. HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL *)(cmdheader->ctba);
  257. memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) + (cmdheader->prdtl - 1) * sizeof(HBA_PRDT_ENTRY));
  258. int i = 0;
  259. for (i = 0; i < cmdheader->prdtl - 1; ++i)
  260. {
  261. cmdtbl->prdt_entry[i].dba = buf;
  262. cmdtbl->prdt_entry[i].dbc = 8 * 1024 - 1; // 8K bytes
  263. cmdtbl->prdt_entry[i].i = 0;
  264. buf += 4 * 1024; // 4K words
  265. count -= 16; // 16 sectors
  266. }
  267. cmdtbl->prdt_entry[i].dba = buf;
  268. cmdtbl->prdt_entry[i].dbc = count << 9; // 512 bytes per sector
  269. cmdtbl->prdt_entry[i].i = 0;
  270. FIS_REG_H2D *cmdfis = (FIS_REG_H2D *)(&cmdtbl->cfis);
  271. cmdfis->fis_type = FIS_TYPE_REG_H2D;
  272. cmdfis->c = 1; // Command
  273. cmdfis->command = ATA_CMD_WRITE_DMA_EXT;
  274. cmdfis->lba0 = (uint8_t)startl;
  275. cmdfis->lba1 = (uint8_t)(startl >> 8);
  276. cmdfis->lba2 = (uint8_t)(startl >> 16);
  277. cmdfis->lba3 = (uint8_t)(startl >> 24);
  278. cmdfis->lba4 = (uint8_t)starth;
  279. cmdfis->lba5 = (uint8_t)(starth >> 8);
  280. cmdfis->device = 1 << 6; // LBA mode
  281. cmdfis->countl = count & 0xff;
  282. cmdfis->counth = count >> 8;
  283. // printk("[slot]{%d}", slot);
  284. port->ci = 1; // Issue command
  285. while (1)
  286. {
  287. // In some longer duration reads, it may be helpful to spin on the DPS bit
  288. // in the PxIS port field as well (1 << 5)
  289. if ((port->ci & (1 << slot)) == 0)
  290. break;
  291. if (port->is & HBA_PxIS_TFES)
  292. { // Task file error
  293. kerror("Write disk error");
  294. return E_TASK_FILE_ERROR;
  295. }
  296. }
  297. if (port->is & HBA_PxIS_TFES)
  298. {
  299. kerror("Write disk error");
  300. return E_TASK_FILE_ERROR;
  301. }
  302. return AHCI_SUCCESS;
  303. }
  304. // Find a free command list slot
  305. static int ahci_find_cmdslot(HBA_PORT *port)
  306. {
  307. // If not set in SACT and CI, the slot is free
  308. uint32_t slots = (port->sact | port->ci);
  309. int num_of_cmd_clots = (ahci_devices[0].hba_mem->cap & 0x0f00) >> 8; // bit 12-8
  310. for (int i = 0; i < num_of_cmd_clots; i++)
  311. {
  312. if ((slots & 1) == 0)
  313. return i;
  314. slots >>= 1;
  315. }
  316. kerror("Cannot find free command list entry");
  317. return -1;
  318. }
  319. long ahci_open()
  320. {
  321. return 0;
  322. }
  323. long ahci_close()
  324. {
  325. return 0;
  326. }
  327. /**
  328. * @brief 创建ahci磁盘请求包
  329. *
  330. * @param cmd 控制命令
  331. * @param base_addr 48位LBA地址
  332. * @param count total sectors to read
  333. * @param buf 缓冲区线性地址
  334. * @param ahci_ctrl_num ahci控制器号
  335. * @param port_num ahci控制器端口号
  336. * @return struct block_device_request_packet*
  337. */
  338. static struct block_device_request_packet *ahci_make_request(long cmd, uint64_t base_addr, uint64_t count, uint64_t buffer, uint8_t ahci_ctrl_num, uint8_t port_num)
  339. {
  340. struct block_device_request_packet *pack = (struct block_device_request_packet *)kmalloc(sizeof(struct block_device_request_packet), 0);
  341. list_init(&pack->list);
  342. // 由于ahci不需要中断即可读取磁盘,因此end handler为空
  343. switch (cmd)
  344. {
  345. case ATA_CMD_READ_DMA_EXT:
  346. pack->end_handler = NULL;
  347. pack->cmd = ATA_CMD_READ_DMA_EXT;
  348. break;
  349. case ATA_CMD_WRITE_DMA_EXT:
  350. pack->end_handler = NULL;
  351. pack->cmd = ATA_CMD_WRITE_DMA_EXT;
  352. break;
  353. default:
  354. pack->end_handler = NULL;
  355. pack->cmd = cmd;
  356. break;
  357. }
  358. pack->LBA_start = base_addr;
  359. pack->count = count;
  360. pack->buffer_vaddr = buffer;
  361. pack->ahci_ctrl_num = ahci_ctrl_num;
  362. pack->port_num = port_num;
  363. return pack;
  364. }
  365. /**
  366. * @brief 结束磁盘请求
  367. *
  368. */
  369. static void ahci_end_request()
  370. {
  371. kfree((uint64_t *)ahci_req_queue.in_service);
  372. ahci_req_queue.in_service = NULL;
  373. // 进行下一轮的磁盘请求 (由于未实现单独的io调度器,这里会造成长时间的io等待)
  374. if (ahci_req_queue.request_count>0)
  375. ahci_query_disk();
  376. }
  377. static long ahci_query_disk()
  378. {
  379. struct block_device_request_packet *pack = container_of(list_next(&ahci_req_queue.queue_list), struct block_device_request_packet, list);
  380. ahci_req_queue.in_service = pack;
  381. list_del(&(ahci_req_queue.in_service->list));
  382. --ahci_req_queue.request_count;
  383. long ret_val;
  384. switch (pack->cmd)
  385. {
  386. case ATA_CMD_READ_DMA_EXT:
  387. ret_val = ahci_read(&(ahci_devices[pack->ahci_ctrl_num].hba_mem->ports[pack->port_num]), pack->LBA_start & 0xFFFFFFFF, ((pack->LBA_start) >> 32) & 0xFFFFFFFF, pack->count, pack->buffer_vaddr);
  388. break;
  389. case ATA_CMD_WRITE_DMA_EXT:
  390. ret_val = ahci_write(&(ahci_devices[pack->ahci_ctrl_num].hba_mem->ports[pack->port_num]), pack->LBA_start & 0xFFFFFFFF, ((pack->LBA_start) >> 32) & 0xFFFFFFFF, pack->count, pack->buffer_vaddr);
  391. break;
  392. default:
  393. kerror("Unsupport ahci command: %#05lx", pack->cmd);
  394. ret_val = E_UNSUPPORTED_CMD;
  395. break;
  396. }
  397. ahci_end_request();
  398. return ret_val;
  399. }
  400. /**
  401. * @brief 将请求包提交到io队列
  402. *
  403. * @param pack
  404. */
  405. static void ahci_submit(struct block_device_request_packet *pack)
  406. {
  407. list_append(&(ahci_req_queue.queue_list), &(pack->list));
  408. ++ahci_req_queue.request_count;
  409. if (ahci_req_queue.in_service == NULL) // 当前没有正在请求的io包,立即执行磁盘请求
  410. ahci_query_disk();
  411. }
  412. /**
  413. * @brief ahci驱动程序的传输函数
  414. *
  415. * @param cmd 控制命令
  416. * @param base_addr 48位LBA地址
  417. * @param count total sectors to read
  418. * @param buf 缓冲区线性地址
  419. * @param ahci_ctrl_num ahci控制器号
  420. * @param port_num ahci控制器端口号
  421. * @return long
  422. */
  423. static long ahci_transfer(long cmd, uint64_t base_addr, uint64_t count, uint64_t buf, uint8_t ahci_ctrl_num, uint8_t port_num)
  424. {
  425. struct block_device_request_packet *pack = NULL;
  426. if (cmd == ATA_CMD_READ_DMA_EXT || cmd == ATA_CMD_WRITE_DMA_EXT)
  427. {
  428. pack = ahci_make_request(cmd, base_addr, count, buf, ahci_ctrl_num, port_num);
  429. ahci_submit(pack);
  430. }
  431. else
  432. return E_UNSUPPORTED_CMD;
  433. return AHCI_SUCCESS;
  434. }
  435. /**
  436. * @brief todo: io控制器函数
  437. *
  438. * @param cmd 命令
  439. * @param arg 参数
  440. * @return long
  441. */
  442. static long ahci_ioctl(long cmd, long arg)
  443. {
  444. }
  445. struct block_device_operation ahci_operation =
  446. {
  447. .open = ahci_open,
  448. .close = ahci_close,
  449. .ioctl = ahci_ioctl,
  450. .transfer = ahci_transfer,
  451. };