ahci.c 15 KB

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