ahci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* AHCI (SATA) driver: PCI discovery, port bring-up, IDENTIFY and DMA
  2. * read/write (READ/WRITE DMA EXT, LBA48), all polled (no IRQs).
  3. *
  4. * Target: the ION/AT3IONT-I MCP79 SATA controller in AHCI mode (the same
  5. * hardware family Linux drives with board_ahci_noncq), and QEMU's ich9-ahci
  6. * for testing. Only non-NCQ commands are used.
  7. *
  8. * All buffers (command list, FIS receive area, command table, DMA bounce
  9. * buffer) come from the page allocator -- the kernel lives under a tight
  10. * linker bound, so nothing large may go into BSS. */
  11. #include "ahci.h"
  12. #include "vfs.h"
  13. #include "ata.h"
  14. #include "mm.h"
  15. #include "paging.h"
  16. #include "pci.h"
  17. #include "process.h"
  18. #include "serial.h"
  19. #include "timer.h"
  20. #define AHCI_DEBUG 0
  21. /* ---- generic host control registers (ABAR-relative) ---- */
  22. #define H_CAP 0x00
  23. #define H_GHC 0x04
  24. #define H_PI 0x0C
  25. #define GHC_AE 0x80000000u /* AHCI enable */
  26. /* ---- port registers (0x100 + 0x80 * port) ---- */
  27. #define P_CLB 0x00
  28. #define P_CLBU 0x04
  29. #define P_FB 0x08
  30. #define P_FBU 0x0C
  31. #define P_IS 0x10
  32. #define P_IE 0x14
  33. #define P_CMD 0x18
  34. #define P_TFD 0x20
  35. #define P_SIG 0x24
  36. #define P_SSTS 0x28
  37. #define P_SCTL 0x2C
  38. #define P_SERR 0x30
  39. #define P_CI 0x38
  40. #define CMD_ST 0x00000001u /* start */
  41. #define CMD_FRE 0x00000010u /* FIS receive enable */
  42. #define CMD_FR 0x00004000u /* FIS receive running */
  43. #define CMD_CR 0x00008000u /* command list running */
  44. #define TFD_ERR 0x01
  45. #define TFD_BSY 0x80
  46. #define TFD_DRQ 0x08
  47. #define TFD_ERR_BITS (TFD_ERR | TFD_BSY | TFD_DRQ)
  48. #define IS_TFES 0x40000000u /* task file error status */
  49. #define SIG_ATA 0x00000101u
  50. /* Register Host-to-Device FIS. */
  51. #define FIS_H2D 0x27
  52. #define FIS_C 0x80
  53. #define ATA_IDENTIFY 0xEC
  54. #define ATA_READ_DMA_EXT 0x25
  55. #define ATA_WRITE_DMA_EXT 0x35
  56. #define AHCI_CHUNK 255 /* sectors per command (128 KB-ish) */
  57. #define BOUNCE_PAGES 33 /* 255 * 512 = 130560 B < 33 * 4096 */
  58. static struct {
  59. int inited;
  60. int ok;
  61. unsigned int abar;
  62. unsigned int nports;
  63. unsigned int pi;
  64. unsigned int port_ready; /* bitmask: port brought up and usable */
  65. unsigned int cl; /* command list (1 page) */
  66. unsigned int fis; /* FIS receive area (1 page) */
  67. unsigned int ct; /* command table (1 page) */
  68. unsigned int bounce; /* data bounce buffer (BOUNCE_PAGES) */
  69. } ahci;
  70. static unsigned int rd(unsigned int off)
  71. {
  72. return *(volatile unsigned int *)(ahci.abar + off);
  73. }
  74. static void wr(unsigned int off, unsigned int v)
  75. {
  76. *(volatile unsigned int *)(ahci.abar + off) = v;
  77. }
  78. static unsigned int prd(unsigned int port, unsigned int off)
  79. {
  80. return *(volatile unsigned int *)(ahci.abar + 0x100 + 0x80 * port + off);
  81. }
  82. static void pwr(unsigned int port, unsigned int off, unsigned int v)
  83. {
  84. *(volatile unsigned int *)(ahci.abar + 0x100 + 0x80 * port + off) = v;
  85. }
  86. static void mem_copy(unsigned char *dst, const unsigned char *src, unsigned int n)
  87. {
  88. while (n--)
  89. *dst++ = *src++;
  90. }
  91. static void delay_ms(unsigned int ms)
  92. {
  93. unsigned int t0 = get_ticks_ms();
  94. while (get_ticks_ms() - t0 < ms)
  95. vfs_scan_poll(t0);
  96. }
  97. /* ---- PCI discovery ---- */
  98. static int ahci_find(unsigned int *out_bus, unsigned int *out_dev, unsigned int *out_func)
  99. {
  100. unsigned int bus, dev, func;
  101. for (bus = 0; bus < 256; bus++) {
  102. for (dev = 0; dev < 32; dev++) {
  103. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  104. unsigned int htype, nfunc;
  105. if (id == 0xFFFFFFFFu || id == 0)
  106. continue;
  107. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  108. nfunc = (htype & 0x80u) ? 8u : 1u;
  109. for (func = 0; func < nfunc; func++) {
  110. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  111. unsigned int crev, cls, sub, prog;
  112. if (idf == 0xFFFFFFFFu || idf == 0)
  113. continue;
  114. crev = pci_cfg_read32(bus, dev, func, 0x08);
  115. cls = (crev >> 24) & 0xFFu;
  116. sub = (crev >> 16) & 0xFFu;
  117. prog = (crev >> 8) & 0xFFu;
  118. if (cls != 0x01)
  119. continue;
  120. /* One line per storage function so a board whose
  121. * SATA mode does not match can be diagnosed from
  122. * the serial/netlog output (class CCDD, prog-if,
  123. * vendor:device). */
  124. serial_puts(SERIAL_COM1, "ahci: storage fn ");
  125. serial_putdec(SERIAL_COM1, bus);
  126. serial_putc(SERIAL_COM1, ':');
  127. serial_putdec(SERIAL_COM1, dev);
  128. serial_putc(SERIAL_COM1, '.');
  129. serial_putdec(SERIAL_COM1, func);
  130. serial_puts(SERIAL_COM1, " id ");
  131. serial_puthex(SERIAL_COM1, idf);
  132. serial_puts(SERIAL_COM1, " class ");
  133. serial_puthex(SERIAL_COM1, (cls << 8) | sub);
  134. serial_puts(SERIAL_COM1, " prog ");
  135. serial_puthex(SERIAL_COM1, prog);
  136. serial_putc(SERIAL_COM1, '\n');
  137. if (sub == 0x06 && prog == 0x01) {
  138. *out_bus = bus;
  139. *out_dev = dev;
  140. *out_func = func;
  141. return 0;
  142. }
  143. }
  144. }
  145. }
  146. return -1;
  147. }
  148. /* ---- port bring-up ---- */
  149. static void ahci_port_init(unsigned int port)
  150. {
  151. unsigned int det, t0, c;
  152. /* An implemented port with no device (DET=0) or a powered-down PHY
  153. * (DET=4) has nothing to bring up. Issuing COMRESET and waiting for
  154. * the link there is what made boot spend ~1 s on every empty port;
  155. * the port is initialised lazily by ahci_scan() if a device appears. */
  156. det = prd(port, P_SSTS) & 0x0Fu;
  157. if (det == 0 || det == 4)
  158. return;
  159. /* Stop the port and its FIS receive engine. */
  160. c = prd(port, P_CMD);
  161. if (c & CMD_ST) {
  162. pwr(port, P_CMD, c & ~CMD_ST);
  163. t0 = get_ticks_ms();
  164. while ((prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
  165. vfs_scan_poll(t0);
  166. }
  167. c = prd(port, P_CMD);
  168. if (c & CMD_FRE) {
  169. pwr(port, P_CMD, c & ~CMD_FRE);
  170. t0 = get_ticks_ms();
  171. while ((prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
  172. vfs_scan_poll(t0);
  173. }
  174. pwr(port, P_CLB, ahci.cl);
  175. pwr(port, P_CLBU, 0);
  176. pwr(port, P_FB, ahci.fis);
  177. pwr(port, P_FBU, 0);
  178. pwr(port, P_SERR, 0xFFFFFFFFu);
  179. pwr(port, P_IS, 0xFFFFFFFFu);
  180. pwr(port, P_IE, 0);
  181. pwr(port, P_CMD, prd(port, P_CMD) | CMD_FRE);
  182. t0 = get_ticks_ms();
  183. while (!(prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
  184. vfs_scan_poll(t0);
  185. /* Cold plug: reset the link if it is not established yet. */
  186. if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
  187. pwr(port, P_SCTL, 1); /* DET = 1: initiate COMRESET */
  188. t0 = get_ticks_ms();
  189. while ((prd(port, P_SSTS) & 0x0Fu) != 3 && get_ticks_ms() - t0 < 1000)
  190. vfs_scan_poll(t0);
  191. }
  192. pwr(port, P_CMD, prd(port, P_CMD) | CMD_ST);
  193. t0 = get_ticks_ms();
  194. while (!(prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
  195. vfs_scan_poll(t0);
  196. }
  197. int ahci_init(void)
  198. {
  199. unsigned int bus, dev, func, bar, cmd, cap;
  200. if (ahci.inited)
  201. return ahci.ok ? 0 : -1;
  202. ahci.inited = 1;
  203. if (ahci_find(&bus, &dev, &func) != 0) {
  204. serial_puts(SERIAL_COM1, "ahci: no matching controller (need 0106:01)\n");
  205. return -1;
  206. }
  207. bar = pci_cfg_read32(bus, dev, func, 0x24);
  208. if (bar & 1u) {
  209. serial_puts(SERIAL_COM1, "ahci: BAR5 is not MMIO\n");
  210. return -1;
  211. }
  212. bar &= 0xFFFFFFF0u;
  213. if (!bar || bar == 0xFFFFFFF0u) {
  214. serial_puts(SERIAL_COM1, "ahci: BAR5 is unassigned\n");
  215. return -1;
  216. }
  217. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  218. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u); /* mem + bus master */
  219. process_map_mmio_all(bar, 0x2000);
  220. ahci.abar = bar;
  221. cap = rd(H_CAP);
  222. if (cap == 0 || cap == 0xFFFFFFFFu) {
  223. serial_puts(SERIAL_COM1, "ahci: no register response\n");
  224. return -1;
  225. }
  226. ahci.cl = mm_alloc_page();
  227. ahci.fis = mm_alloc_page();
  228. ahci.ct = mm_alloc_page();
  229. ahci.bounce = mm_alloc_pages(BOUNCE_PAGES);
  230. if (!ahci.cl || !ahci.fis || !ahci.ct || !ahci.bounce) {
  231. serial_puts(SERIAL_COM1, "ahci: no memory for DMA buffers\n");
  232. return -1;
  233. }
  234. {
  235. unsigned int *p;
  236. unsigned int n;
  237. for (p = (unsigned int *)ahci.cl, n = 0; n < 0x400; n++) p[n] = 0;
  238. for (p = (unsigned int *)ahci.fis, n = 0; n < 0x400; n++) p[n] = 0;
  239. for (p = (unsigned int *)ahci.ct, n = 0; n < 0x400; n++) p[n] = 0;
  240. for (p = (unsigned int *)ahci.bounce, n = 0; n < BOUNCE_PAGES * 0x400; n++)
  241. p[n] = 0;
  242. }
  243. wr(H_GHC, rd(H_GHC) | GHC_AE);
  244. ahci.nports = (cap & 0x1Fu) + 1;
  245. if (ahci.nports > 32)
  246. ahci.nports = 32;
  247. ahci.pi = rd(H_PI);
  248. serial_puts(SERIAL_COM1, "ahci: ctrl ");
  249. serial_putdec(SERIAL_COM1, bus);
  250. serial_putc(SERIAL_COM1, ':');
  251. serial_putdec(SERIAL_COM1, dev);
  252. serial_putc(SERIAL_COM1, '.');
  253. serial_putdec(SERIAL_COM1, func);
  254. serial_puts(SERIAL_COM1, " bar=");
  255. serial_puthex(SERIAL_COM1, bar);
  256. serial_puts(SERIAL_COM1, " pi=");
  257. serial_puthex(SERIAL_COM1, ahci.pi);
  258. serial_putc(SERIAL_COM1, '\n');
  259. /* Ports are brought up on demand by ahci_scan(): empty ports are
  260. * skipped, and a scan after a disk was plugged in still works. */
  261. ahci.ok = 1;
  262. return 0;
  263. }
  264. int ahci_controller_present(void)
  265. {
  266. ahci_init();
  267. return ahci.ok;
  268. }
  269. /* ---- command issue (slot 0, polled) ---- */
  270. static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
  271. unsigned int dma_addr, unsigned int bytes)
  272. {
  273. unsigned int *hdr = (unsigned int *)ahci.cl;
  274. unsigned char *ct = (unsigned char *)ahci.ct;
  275. unsigned int t0, i, prdtl = bytes ? 1u : 0u;
  276. /* Wait for the port to be idle (previous command finished). */
  277. t0 = get_ticks_ms();
  278. while (prd(port, P_CI) & 1u) {
  279. vfs_scan_poll(t0);
  280. if (get_ticks_ms() - t0 > 4000)
  281. return -1;
  282. }
  283. if (prd(port, P_TFD) & TFD_BSY)
  284. return -1;
  285. for (i = 0; i < 64; i++)
  286. ct[i] = 0;
  287. for (i = 0; i < 20; i++)
  288. ct[i] = fis[i];
  289. /* PRDT entry 0: data buffer + byte count (no data: PRDTL = 0). */
  290. if (prdtl) {
  291. ((unsigned int *)(ct + 0x80))[0] = dma_addr;
  292. ((unsigned int *)(ct + 0x80))[1] = 0;
  293. ((unsigned int *)(ct + 0x80))[2] = 0;
  294. ((unsigned int *)(ct + 0x80))[3] = ((bytes - 1) & 0x3FFFFFu) | 0x80000000u;
  295. }
  296. /* Command header: CFL=5 dwords, W (write), PRDTL. */
  297. hdr[0] = 5u | (write ? (1u << 6) : 0) | (prdtl << 16);
  298. hdr[1] = 0;
  299. hdr[2] = ahci.ct;
  300. hdr[3] = 0;
  301. pwr(port, P_IS, 0xFFFFFFFFu);
  302. pwr(port, P_CI, 1u);
  303. t0 = get_ticks_ms();
  304. while (prd(port, P_CI) & 1u) {
  305. vfs_scan_poll(t0);
  306. if (get_ticks_ms() - t0 > 4000) {
  307. #if AHCI_DEBUG
  308. serial_puts(SERIAL_COM1, "ahci: command timeout\n");
  309. #endif
  310. return -1;
  311. }
  312. }
  313. if (prd(port, P_IS) & IS_TFES) {
  314. #if AHCI_DEBUG
  315. serial_puts(SERIAL_COM1, "ahci: task file error, tfd=");
  316. serial_puthex(SERIAL_COM1, prd(port, P_TFD));
  317. serial_putc(SERIAL_COM1, '\n');
  318. #endif
  319. return -1;
  320. }
  321. if (prd(port, P_TFD) & TFD_ERR_BITS)
  322. return -1;
  323. return 0;
  324. }
  325. static int ahci_identify_port(unsigned int port, unsigned int *sectors, char model[41])
  326. {
  327. unsigned char fis[20];
  328. unsigned int attempt, i;
  329. int r = -1;
  330. for (i = 0; i < 20; i++)
  331. fis[i] = 0;
  332. fis[0] = FIS_H2D;
  333. fis[1] = FIS_C;
  334. fis[2] = ATA_IDENTIFY;
  335. fis[7] = 0xA0; /* device 0 */
  336. for (attempt = 0; attempt < 3; attempt++) {
  337. r = ahci_issue(port, fis, 0, ahci.bounce, 512);
  338. if (r == 0)
  339. break;
  340. delay_ms(20);
  341. }
  342. if (r != 0)
  343. return -1;
  344. return ata_identify_parse((const unsigned short *)ahci.bounce, sectors, model);
  345. }
  346. static int ahci_xfer(unsigned int port, unsigned int lba, void *buf,
  347. unsigned int count, int write)
  348. {
  349. unsigned char *p = buf;
  350. unsigned char *bounce = (unsigned char *)ahci.bounce;
  351. unsigned int done = 0;
  352. if (!ahci.ok || count == 0)
  353. return -1;
  354. while (done < count) {
  355. unsigned int chunk = count - done;
  356. unsigned int cur = lba + done;
  357. unsigned char fis[20];
  358. unsigned int i;
  359. if (chunk > AHCI_CHUNK)
  360. chunk = AHCI_CHUNK;
  361. for (i = 0; i < 20; i++)
  362. fis[i] = 0;
  363. fis[0] = FIS_H2D;
  364. fis[1] = FIS_C;
  365. fis[2] = write ? ATA_WRITE_DMA_EXT : ATA_READ_DMA_EXT;
  366. fis[4] = (unsigned char)(cur & 0xFF);
  367. fis[5] = (unsigned char)((cur >> 8) & 0xFF);
  368. fis[6] = (unsigned char)((cur >> 16) & 0xFF);
  369. fis[7] = 0x40; /* LBA mode */
  370. fis[8] = (unsigned char)((cur >> 24) & 0xFF);
  371. fis[9] = 0; /* LBA4/LBA5: 32-bit LBA only */
  372. fis[10] = 0;
  373. fis[12] = (unsigned char)(chunk & 0xFF);
  374. fis[13] = (unsigned char)((chunk >> 8) & 0xFF);
  375. if (write)
  376. mem_copy(bounce, p + done * 512u, chunk * 512u);
  377. if (ahci_issue(port, fis, write, ahci.bounce, chunk * 512u) != 0) {
  378. serial_puts(SERIAL_COM1, "ahci: ");
  379. serial_puts(SERIAL_COM1, write ? "write" : "read");
  380. serial_puts(SERIAL_COM1, " failed port ");
  381. serial_putdec(SERIAL_COM1, port);
  382. serial_puts(SERIAL_COM1, " lba ");
  383. serial_putdec(SERIAL_COM1, cur);
  384. serial_puts(SERIAL_COM1, " count ");
  385. serial_putdec(SERIAL_COM1, chunk);
  386. serial_puts(SERIAL_COM1, " tfd ");
  387. serial_puthex(SERIAL_COM1, prd(port, P_TFD));
  388. serial_puts(SERIAL_COM1, " is ");
  389. serial_puthex(SERIAL_COM1, prd(port, P_IS));
  390. serial_putc(SERIAL_COM1, '\n');
  391. return -1;
  392. }
  393. if (!write)
  394. mem_copy(p + done * 512u, bounce, chunk * 512u);
  395. done += chunk;
  396. }
  397. return 0;
  398. }
  399. int ahci_read_sectors(unsigned int port, unsigned int lba, void *buf, unsigned int count)
  400. {
  401. return ahci_xfer(port, lba, buf, count, 0);
  402. }
  403. int ahci_write_sectors(unsigned int port, unsigned int lba, const void *buf,
  404. unsigned int count)
  405. {
  406. return ahci_xfer(port, lba, (void *)buf, count, 1);
  407. }
  408. int ahci_flush(unsigned int port)
  409. {
  410. unsigned char fis[20];
  411. unsigned int i;
  412. if (!ahci.ok || port >= ahci.nports)
  413. return -1;
  414. for (i = 0; i < 20; i++)
  415. fis[i] = 0;
  416. fis[0] = FIS_H2D;
  417. fis[1] = FIS_C;
  418. fis[2] = 0xEA; /* FLUSH CACHE EXT */
  419. return ahci_issue(port, fis, 0, 0, 0);
  420. }
  421. unsigned int ahci_scan(struct ahci_disk *out, unsigned int max)
  422. {
  423. unsigned int port, n = 0;
  424. if (ahci_init() != 0)
  425. return 0;
  426. for (port = 0; port < ahci.nports && n < max; port++) {
  427. unsigned int ssts, bit = 1u << port;
  428. if (!(ahci.pi & bit))
  429. continue;
  430. ssts = prd(port, P_SSTS) & 0x0Fu;
  431. if (ssts == 0 || ssts == 4) {
  432. /* No device (or PHY offline). Drop the ready bit so a
  433. * later scan re-initialises the port if a disk appears. */
  434. ahci.port_ready &= ~bit;
  435. continue;
  436. }
  437. if (!(ahci.port_ready & bit)) {
  438. unsigned int t0 = get_ticks_ms();
  439. ahci_port_init(port);
  440. serial_puts(SERIAL_COM1, "ahci: port ");
  441. serial_putdec(SERIAL_COM1, port);
  442. serial_puts(SERIAL_COM1, " init ");
  443. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  444. serial_puts(SERIAL_COM1, " ms\n");
  445. if ((prd(port, P_SSTS) & 0x0Fu) != 3)
  446. continue;
  447. ahci.port_ready |= bit;
  448. }
  449. if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
  450. ahci.port_ready &= ~bit;
  451. continue;
  452. }
  453. if (prd(port, P_SIG) != SIG_ATA)
  454. continue; /* ATAPI / port multiplier / SEMB */
  455. if (ahci_identify_port(port, &out[n].sectors, out[n].model) != 0) {
  456. /* A slow disk may not answer yet; leave the port for a
  457. * later scan instead of reporting a half-initialised one. */
  458. ahci.port_ready &= ~bit;
  459. continue;
  460. }
  461. out[n].port = port;
  462. serial_puts(SERIAL_COM1, "ahci: port ");
  463. serial_putdec(SERIAL_COM1, port);
  464. serial_puts(SERIAL_COM1, " ");
  465. serial_puts(SERIAL_COM1, out[n].model);
  466. serial_puts(SERIAL_COM1, " ");
  467. serial_putdec(SERIAL_COM1, out[n].sectors);
  468. serial_puts(SERIAL_COM1, " sectors\n");
  469. n++;
  470. }
  471. return n;
  472. }