ahci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. ahci.abar = (unsigned int)ioremap(bar, 0x2000, IOREMAP_MMIO);
  220. if (!ahci.abar)
  221. return -1;
  222. cap = rd(H_CAP);
  223. if (cap == 0 || cap == 0xFFFFFFFFu) {
  224. serial_puts(SERIAL_COM1, "ahci: no register response\n");
  225. return -1;
  226. }
  227. ahci.cl = mm_alloc_page();
  228. ahci.fis = mm_alloc_page();
  229. ahci.ct = mm_alloc_page();
  230. ahci.bounce = mm_alloc_pages(BOUNCE_PAGES);
  231. if (!ahci.cl || !ahci.fis || !ahci.ct || !ahci.bounce) {
  232. serial_puts(SERIAL_COM1, "ahci: no memory for DMA buffers\n");
  233. return -1;
  234. }
  235. {
  236. unsigned int *p;
  237. unsigned int n;
  238. for (p = (unsigned int *)ahci.cl, n = 0; n < 0x400; n++) p[n] = 0;
  239. for (p = (unsigned int *)ahci.fis, n = 0; n < 0x400; n++) p[n] = 0;
  240. for (p = (unsigned int *)ahci.ct, n = 0; n < 0x400; n++) p[n] = 0;
  241. for (p = (unsigned int *)ahci.bounce, n = 0; n < BOUNCE_PAGES * 0x400; n++)
  242. p[n] = 0;
  243. }
  244. wr(H_GHC, rd(H_GHC) | GHC_AE);
  245. ahci.nports = (cap & 0x1Fu) + 1;
  246. if (ahci.nports > 32)
  247. ahci.nports = 32;
  248. ahci.pi = rd(H_PI);
  249. serial_puts(SERIAL_COM1, "ahci: ctrl ");
  250. serial_putdec(SERIAL_COM1, bus);
  251. serial_putc(SERIAL_COM1, ':');
  252. serial_putdec(SERIAL_COM1, dev);
  253. serial_putc(SERIAL_COM1, '.');
  254. serial_putdec(SERIAL_COM1, func);
  255. serial_puts(SERIAL_COM1, " bar=");
  256. serial_puthex(SERIAL_COM1, bar);
  257. serial_puts(SERIAL_COM1, " pi=");
  258. serial_puthex(SERIAL_COM1, ahci.pi);
  259. serial_putc(SERIAL_COM1, '\n');
  260. /* Ports are brought up on demand by ahci_scan(): empty ports are
  261. * skipped, and a scan after a disk was plugged in still works. */
  262. ahci.ok = 1;
  263. return 0;
  264. }
  265. int ahci_controller_present(void)
  266. {
  267. ahci_init();
  268. return ahci.ok;
  269. }
  270. /* ---- command issue (slot 0, polled) ---- */
  271. static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
  272. unsigned int dma_addr, unsigned int bytes)
  273. {
  274. unsigned int *hdr = (unsigned int *)ahci.cl;
  275. unsigned char *ct = (unsigned char *)ahci.ct;
  276. unsigned int t0, i, prdtl = bytes ? 1u : 0u;
  277. /* Wait for the port to be idle (previous command finished). */
  278. t0 = get_ticks_ms();
  279. while (prd(port, P_CI) & 1u) {
  280. vfs_scan_poll(t0);
  281. if (get_ticks_ms() - t0 > 4000)
  282. return -1;
  283. }
  284. if (prd(port, P_TFD) & TFD_BSY)
  285. return -1;
  286. for (i = 0; i < 64; i++)
  287. ct[i] = 0;
  288. for (i = 0; i < 20; i++)
  289. ct[i] = fis[i];
  290. /* PRDT entry 0: data buffer + byte count (no data: PRDTL = 0). */
  291. if (prdtl) {
  292. ((unsigned int *)(ct + 0x80))[0] = dma_addr;
  293. ((unsigned int *)(ct + 0x80))[1] = 0;
  294. ((unsigned int *)(ct + 0x80))[2] = 0;
  295. ((unsigned int *)(ct + 0x80))[3] = ((bytes - 1) & 0x3FFFFFu) | 0x80000000u;
  296. }
  297. /* Command header: CFL=5 dwords, W (write), PRDTL. */
  298. hdr[0] = 5u | (write ? (1u << 6) : 0) | (prdtl << 16);
  299. hdr[1] = 0;
  300. hdr[2] = ahci.ct;
  301. hdr[3] = 0;
  302. pwr(port, P_IS, 0xFFFFFFFFu);
  303. pwr(port, P_CI, 1u);
  304. t0 = get_ticks_ms();
  305. while (prd(port, P_CI) & 1u) {
  306. vfs_scan_poll(t0);
  307. if (get_ticks_ms() - t0 > 4000) {
  308. #if AHCI_DEBUG
  309. serial_puts(SERIAL_COM1, "ahci: command timeout\n");
  310. #endif
  311. return -1;
  312. }
  313. }
  314. if (prd(port, P_IS) & IS_TFES) {
  315. #if AHCI_DEBUG
  316. serial_puts(SERIAL_COM1, "ahci: task file error, tfd=");
  317. serial_puthex(SERIAL_COM1, prd(port, P_TFD));
  318. serial_putc(SERIAL_COM1, '\n');
  319. #endif
  320. return -1;
  321. }
  322. if (prd(port, P_TFD) & TFD_ERR_BITS)
  323. return -1;
  324. return 0;
  325. }
  326. static int ahci_identify_port(unsigned int port, unsigned int *sectors, char model[41])
  327. {
  328. unsigned char fis[20];
  329. unsigned int attempt, i;
  330. int r = -1;
  331. for (i = 0; i < 20; i++)
  332. fis[i] = 0;
  333. fis[0] = FIS_H2D;
  334. fis[1] = FIS_C;
  335. fis[2] = ATA_IDENTIFY;
  336. fis[7] = 0xA0; /* device 0 */
  337. for (attempt = 0; attempt < 3; attempt++) {
  338. r = ahci_issue(port, fis, 0, ahci.bounce, 512);
  339. if (r == 0)
  340. break;
  341. delay_ms(20);
  342. }
  343. if (r != 0)
  344. return -1;
  345. return ata_identify_parse((const unsigned short *)ahci.bounce, sectors, model);
  346. }
  347. static int ahci_xfer(unsigned int port, unsigned int lba, void *buf,
  348. unsigned int count, int write)
  349. {
  350. unsigned char *p = buf;
  351. unsigned char *bounce = (unsigned char *)ahci.bounce;
  352. unsigned int done = 0;
  353. if (!ahci.ok || count == 0)
  354. return -1;
  355. while (done < count) {
  356. unsigned int chunk = count - done;
  357. unsigned int cur = lba + done;
  358. unsigned char fis[20];
  359. unsigned int i;
  360. if (chunk > AHCI_CHUNK)
  361. chunk = AHCI_CHUNK;
  362. for (i = 0; i < 20; i++)
  363. fis[i] = 0;
  364. fis[0] = FIS_H2D;
  365. fis[1] = FIS_C;
  366. fis[2] = write ? ATA_WRITE_DMA_EXT : ATA_READ_DMA_EXT;
  367. fis[4] = (unsigned char)(cur & 0xFF);
  368. fis[5] = (unsigned char)((cur >> 8) & 0xFF);
  369. fis[6] = (unsigned char)((cur >> 16) & 0xFF);
  370. fis[7] = 0x40; /* LBA mode */
  371. fis[8] = (unsigned char)((cur >> 24) & 0xFF);
  372. fis[9] = 0; /* LBA4/LBA5: 32-bit LBA only */
  373. fis[10] = 0;
  374. fis[12] = (unsigned char)(chunk & 0xFF);
  375. fis[13] = (unsigned char)((chunk >> 8) & 0xFF);
  376. if (write)
  377. mem_copy(bounce, p + done * 512u, chunk * 512u);
  378. if (ahci_issue(port, fis, write, ahci.bounce, chunk * 512u) != 0) {
  379. serial_puts(SERIAL_COM1, "ahci: ");
  380. serial_puts(SERIAL_COM1, write ? "write" : "read");
  381. serial_puts(SERIAL_COM1, " failed port ");
  382. serial_putdec(SERIAL_COM1, port);
  383. serial_puts(SERIAL_COM1, " lba ");
  384. serial_putdec(SERIAL_COM1, cur);
  385. serial_puts(SERIAL_COM1, " count ");
  386. serial_putdec(SERIAL_COM1, chunk);
  387. serial_puts(SERIAL_COM1, " tfd ");
  388. serial_puthex(SERIAL_COM1, prd(port, P_TFD));
  389. serial_puts(SERIAL_COM1, " is ");
  390. serial_puthex(SERIAL_COM1, prd(port, P_IS));
  391. serial_putc(SERIAL_COM1, '\n');
  392. return -1;
  393. }
  394. if (!write)
  395. mem_copy(p + done * 512u, bounce, chunk * 512u);
  396. done += chunk;
  397. }
  398. return 0;
  399. }
  400. int ahci_read_sectors(unsigned int port, unsigned int lba, void *buf, unsigned int count)
  401. {
  402. return ahci_xfer(port, lba, buf, count, 0);
  403. }
  404. int ahci_write_sectors(unsigned int port, unsigned int lba, const void *buf,
  405. unsigned int count)
  406. {
  407. return ahci_xfer(port, lba, (void *)buf, count, 1);
  408. }
  409. int ahci_flush(unsigned int port)
  410. {
  411. unsigned char fis[20];
  412. unsigned int i;
  413. if (!ahci.ok || port >= ahci.nports)
  414. return -1;
  415. for (i = 0; i < 20; i++)
  416. fis[i] = 0;
  417. fis[0] = FIS_H2D;
  418. fis[1] = FIS_C;
  419. fis[2] = 0xEA; /* FLUSH CACHE EXT */
  420. return ahci_issue(port, fis, 0, 0, 0);
  421. }
  422. unsigned int ahci_scan(struct ahci_disk *out, unsigned int max)
  423. {
  424. unsigned int port, n = 0;
  425. if (ahci_init() != 0)
  426. return 0;
  427. for (port = 0; port < ahci.nports && n < max; port++) {
  428. unsigned int ssts, bit = 1u << port;
  429. if (!(ahci.pi & bit))
  430. continue;
  431. ssts = prd(port, P_SSTS) & 0x0Fu;
  432. if (ssts == 0 || ssts == 4) {
  433. /* No device (or PHY offline). Drop the ready bit so a
  434. * later scan re-initialises the port if a disk appears. */
  435. ahci.port_ready &= ~bit;
  436. continue;
  437. }
  438. if (!(ahci.port_ready & bit)) {
  439. unsigned int t0 = get_ticks_ms();
  440. ahci_port_init(port);
  441. serial_puts(SERIAL_COM1, "ahci: port ");
  442. serial_putdec(SERIAL_COM1, port);
  443. serial_puts(SERIAL_COM1, " init ");
  444. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  445. serial_puts(SERIAL_COM1, " ms\n");
  446. if ((prd(port, P_SSTS) & 0x0Fu) != 3)
  447. continue;
  448. ahci.port_ready |= bit;
  449. }
  450. if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
  451. ahci.port_ready &= ~bit;
  452. continue;
  453. }
  454. if (prd(port, P_SIG) != SIG_ATA)
  455. continue; /* ATAPI / port multiplier / SEMB */
  456. if (ahci_identify_port(port, &out[n].sectors, out[n].model) != 0) {
  457. /* A slow disk may not answer yet; leave the port for a
  458. * later scan instead of reporting a half-initialised one. */
  459. ahci.port_ready &= ~bit;
  460. continue;
  461. }
  462. out[n].port = port;
  463. serial_puts(SERIAL_COM1, "ahci: port ");
  464. serial_putdec(SERIAL_COM1, port);
  465. serial_puts(SERIAL_COM1, " ");
  466. serial_puts(SERIAL_COM1, out[n].model);
  467. serial_puts(SERIAL_COM1, " ");
  468. serial_putdec(SERIAL_COM1, out[n].sectors);
  469. serial_puts(SERIAL_COM1, " sectors\n");
  470. n++;
  471. }
  472. return n;
  473. }