ata.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* ATA PIO driver: IDENTIFY + LBA28/LBA48 read/write, polled (no IRQ).
  2. * The ATA IRQs (14/15) stay masked at boot, so we poll BSY/DRQ/ERR.
  3. *
  4. * Channel port bases default to the legacy primary/secondary ports and are
  5. * replaced by the PCI IDE interface's BARs when it runs in native mode
  6. * (nVidia MCP79 "compatibility mode" and friends). */
  7. #include "ata.h"
  8. #include "pci.h"
  9. #include "serial.h"
  10. #include "timer.h"
  11. static unsigned short ata_base[2] = { 0x1F0, 0x170 };
  12. /* Kept for the bring-up log only; transfers poll the command-block status
  13. * because some controllers' alt-status BAR does not answer correctly. */
  14. static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
  15. /* Set when a multi-sector PIO command left DRQ pending on this channel.
  16. * From then on use one ATA command per sector; slower, but it works on
  17. * bridges that mishandle multi-sector PIO. */
  18. static int ata_single[2];
  19. static int ata_lba48[2];
  20. static int ata_tf_logged;
  21. /* Register offsets from the I/O base. */
  22. #define REG_DATA 0
  23. #define REG_ERROR 1
  24. #define REG_COUNT 2
  25. #define REG_LBA_LO 3
  26. #define REG_LBA_MID 4
  27. #define REG_LBA_HI 5
  28. #define REG_DRIVE 6
  29. #define REG_CMD 7
  30. /* Status bits (status = base+7, alt-status = ctrl+0). */
  31. #define SR_BSY 0x80
  32. #define SR_DRDY 0x40
  33. #define SR_DRQ 0x08
  34. #define SR_ERR 0x01
  35. /* Commands. */
  36. #define CMD_IDENTIFY 0xEC
  37. #define CMD_READ 0x20 /* LBA28 */
  38. #define CMD_WRITE 0x30 /* LBA28 */
  39. #define CMD_READ_EXT 0x24 /* LBA48 */
  40. #define CMD_WRITE_EXT 0x34 /* LBA48 */
  41. /* A spun-down disk can take several seconds to accept its first command;
  42. * keep a generous poll budget. */
  43. #define ATA_TIMEOUT_MS 15000
  44. /* Once the data is transferred, the drive should finish the sector quickly.
  45. * A longer wait here would only delay the single-sector fallback. */
  46. #define ATA_COMPLETE_TIMEOUT_MS 2000
  47. static void ata_reset_channel(unsigned short base, unsigned short ctrl);
  48. /* Point the primary/secondary channels at the PCI IDE interface's native
  49. * per-channel I/O BARs when it uses native mode, keeping the legacy ports
  50. * otherwise. Runs once; without a PCI IDE function the defaults stay. */
  51. void ata_init_ports(void)
  52. {
  53. static int inited;
  54. unsigned int bus, dev, func;
  55. if (inited)
  56. return;
  57. inited = 1;
  58. for (bus = 0; bus < 256; bus++) {
  59. for (dev = 0; dev < 32; dev++) {
  60. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  61. unsigned int htype, nfunc;
  62. if (id == 0xFFFFFFFFu || id == 0)
  63. continue;
  64. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  65. nfunc = (htype & 0x80u) ? 8u : 1u;
  66. for (func = 0; func < nfunc; func++) {
  67. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  68. unsigned int crev, cls, sub, prog;
  69. unsigned int bar0, bar1, bar2, bar3, cmd;
  70. unsigned short b0, c0, b1, c1;
  71. if (idf == 0xFFFFFFFFu || idf == 0)
  72. continue;
  73. crev = pci_cfg_read32(bus, dev, func, 0x08);
  74. cls = (crev >> 24) & 0xFFu;
  75. sub = (crev >> 16) & 0xFFu;
  76. prog = (crev >> 8) & 0xFFu;
  77. if (cls != 0x01 || sub != 0x01)
  78. continue;
  79. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  80. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0005u);
  81. bar0 = pci_cfg_read32(bus, dev, func, 0x10);
  82. bar1 = pci_cfg_read32(bus, dev, func, 0x14);
  83. bar2 = pci_cfg_read32(bus, dev, func, 0x18);
  84. bar3 = pci_cfg_read32(bus, dev, func, 0x1C);
  85. /* prog-if bit 0/2: primary/secondary native mode. */
  86. if ((prog & 0x01u) && (bar0 & 0x1u)) {
  87. b0 = (unsigned short)(bar0 & 0xFFFCu);
  88. /* A native-mode BAR1 of 0 is malformed;
  89. * the legacy control port is a safer
  90. * fallback than b0+8. */
  91. c0 = (bar1 & 0x1u)
  92. ? (unsigned short)(bar1 & 0xFFFCu)
  93. : (unsigned short)0x3F6;
  94. } else {
  95. b0 = 0x1F0; c0 = 0x3F6;
  96. }
  97. if ((prog & 0x04u) && (bar2 & 0x1u)) {
  98. b1 = (unsigned short)(bar2 & 0xFFFCu);
  99. c1 = (bar3 & 0x1u)
  100. ? (unsigned short)(bar3 & 0xFFFCu)
  101. : (unsigned short)0x376;
  102. } else {
  103. b1 = 0x170; c1 = 0x376;
  104. }
  105. ata_base[0] = b0; ata_ctrl[0] = c0;
  106. ata_base[1] = b1; ata_ctrl[1] = c1;
  107. ata_reset_channel(ata_base[0], ata_ctrl[0]);
  108. ata_reset_channel(ata_base[1], ata_ctrl[1]);
  109. serial_puts(SERIAL_COM1, "ata: ctl ");
  110. serial_puthex(SERIAL_COM1, port_inb(c0));
  111. serial_putc(SERIAL_COM1, '/');
  112. serial_puthex(SERIAL_COM1, port_inb(c0 + 2));
  113. serial_putc(SERIAL_COM1, ' ');
  114. serial_puthex(SERIAL_COM1, port_inb(c1));
  115. serial_putc(SERIAL_COM1, '/');
  116. serial_puthex(SERIAL_COM1, port_inb(c1 + 2));
  117. serial_putc(SERIAL_COM1, '\n');
  118. serial_puts(SERIAL_COM1, "ata: pci ide ");
  119. serial_putdec(SERIAL_COM1, bus);
  120. serial_putc(SERIAL_COM1, ':');
  121. serial_putdec(SERIAL_COM1, dev);
  122. serial_putc(SERIAL_COM1, '.');
  123. serial_putdec(SERIAL_COM1, func);
  124. serial_puts(SERIAL_COM1, " prog ");
  125. serial_puthex(SERIAL_COM1, prog);
  126. serial_puts(SERIAL_COM1, " ch0 ");
  127. serial_puthex(SERIAL_COM1, b0);
  128. serial_putc(SERIAL_COM1, '-');
  129. serial_puthex(SERIAL_COM1, c0);
  130. serial_puts(SERIAL_COM1, " ch1 ");
  131. serial_puthex(SERIAL_COM1, b1);
  132. serial_putc(SERIAL_COM1, '-');
  133. serial_puthex(SERIAL_COM1, c1);
  134. serial_putc(SERIAL_COM1, '\n');
  135. return;
  136. }
  137. }
  138. }
  139. serial_puts(SERIAL_COM1, "ata: no PCI IDE function, legacy ports\n");
  140. }
  141. /* Delay 400 ns by reading the command-block status register a few times.
  142. * The alt-status port is not used for polling: some IDE functions expose a
  143. * BAR that does not answer correctly, and a bogus BSY bit there made every
  144. * transfer time out even though the command block reported ready. */
  145. static void ata_400ns(unsigned short base)
  146. {
  147. (void)port_inb(base + REG_CMD);
  148. (void)port_inb(base + REG_CMD);
  149. (void)port_inb(base + REG_CMD);
  150. (void)port_inb(base + REG_CMD);
  151. }
  152. static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
  153. {
  154. unsigned int t0 = get_ticks_ms();
  155. while (get_ticks_ms() - t0 < timeout_ms) {
  156. if (!(port_inb(base + REG_CMD) & SR_BSY))
  157. return 0;
  158. }
  159. return -1;
  160. }
  161. /* Assert SRST for ~10 ms. This aborts a command a crashed/reloaded kernel
  162. * left half-transferred (the drive would otherwise stay in DRQ forever and
  163. * ignore the next IDENTIFY). Best effort: a malformed control BAR simply
  164. * makes the reset a no-op. */
  165. static void ata_reset_channel(unsigned short base, unsigned short ctrl)
  166. {
  167. unsigned int t0;
  168. port_outb(ctrl, 0x04); /* SRST */
  169. t0 = get_ticks_ms();
  170. while (get_ticks_ms() - t0 < 10)
  171. ;
  172. /* nIEN=1: the driver polls, so keep the drive's IRQ line quiet
  173. * instead of leaving a masked level-triggered interrupt asserted. */
  174. port_outb(ctrl, 0x02);
  175. t0 = get_ticks_ms();
  176. while ((port_inb(base + REG_CMD) & SR_BSY) &&
  177. get_ticks_ms() - t0 < 1000)
  178. ;
  179. ata_400ns(base);
  180. }
  181. static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
  182. {
  183. unsigned int t0 = get_ticks_ms();
  184. while (get_ticks_ms() - t0 < timeout_ms) {
  185. unsigned char st = port_inb(base + REG_CMD);
  186. if (st & SR_ERR)
  187. return -1;
  188. if (st & SR_DRQ)
  189. return 0;
  190. }
  191. return -1;
  192. }
  193. /* A command is done only when both BSY and DRQ are clear. Waiting for BSY
  194. * alone can return while the last sector is still pending; the next command
  195. * is then ignored and the drive stays in DRQ, which looks like a zeroed /
  196. * unmountable filesystem. */
  197. static int ata_wait_idle(unsigned short base, unsigned int timeout_ms)
  198. {
  199. unsigned int t0 = get_ticks_ms();
  200. while (get_ticks_ms() - t0 < timeout_ms) {
  201. unsigned char st = port_inb(base + REG_CMD);
  202. if (st & SR_ERR)
  203. return -1;
  204. if (!(st & (SR_BSY | SR_DRQ)))
  205. return 0;
  206. }
  207. return -1;
  208. }
  209. static int ata_identify(int bus, int unit, unsigned short *id)
  210. {
  211. unsigned short base = ata_base[bus];
  212. unsigned char st;
  213. unsigned int t0;
  214. unsigned int i;
  215. /* A previous failed transfer can leave the drive waiting for PIO
  216. * data, in which case it ignores IDENTIFY and returns zeros. */
  217. if (ata_wait_idle(base, 1000) != 0)
  218. ata_reset_channel(base, ata_ctrl[bus]);
  219. port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
  220. port_outb(base + REG_COUNT, 0);
  221. port_outb(base + REG_LBA_LO, 0);
  222. port_outb(base + REG_LBA_MID, 0);
  223. port_outb(base + REG_LBA_HI, 0);
  224. port_outb(base + REG_CMD, CMD_IDENTIFY);
  225. ata_400ns(base);
  226. t0 = get_ticks_ms();
  227. for (;;) {
  228. st = port_inb(base + REG_CMD);
  229. if (st == 0 || st == 0xFF) /* floating bus: no device */
  230. return -1;
  231. if (!(st & SR_BSY))
  232. break;
  233. if (get_ticks_ms() - t0 > ATA_TIMEOUT_MS)
  234. return -1;
  235. }
  236. if (!(st & SR_DRQ))
  237. return -1;
  238. for (i = 0; i < 256; i++)
  239. id[i] = port_inw(base + REG_DATA);
  240. return 0;
  241. }
  242. int ata_identify_parse(const unsigned short *id, unsigned int *total_sectors, char model[41])
  243. {
  244. unsigned int sectors, lo, hi, i;
  245. if (id[0] & 0x8000) /* ATAPI (cdrom/zip): not a data disk */
  246. return -1;
  247. /* Model string: words 27..46, byte-swapped. */
  248. for (i = 0; i < 20; i++) {
  249. unsigned short w = id[27 + i];
  250. model[i * 2] = (char)(w >> 8);
  251. model[i * 2 + 1] = (char)(w & 0xFF);
  252. }
  253. model[40] = '\0';
  254. for (i = 40; i > 0; i--) {
  255. if (model[i - 1] == ' ')
  256. model[i - 1] = '\0';
  257. else
  258. break;
  259. }
  260. sectors = (unsigned int)id[60] | ((unsigned int)id[61] << 16);
  261. if (id[83] & 0x0400) { /* LBA48 supported */
  262. lo = (unsigned int)id[100] | ((unsigned int)id[101] << 16);
  263. hi = (unsigned int)id[102] | ((unsigned int)id[103] << 16);
  264. if (lo || hi)
  265. sectors = lo;
  266. if (hi)
  267. sectors = 0xFFFFFFFFu; /* > 2 TB: clamp to 32-bit LBA */
  268. }
  269. *total_sectors = sectors;
  270. return 0;
  271. }
  272. int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
  273. {
  274. static unsigned short id[256];
  275. int r;
  276. if (ata_identify(bus, unit, id) != 0)
  277. return -1;
  278. r = ata_identify_parse(id, total_sectors, model);
  279. if (r == 0)
  280. ata_lba48[bus] = (id[83] & 0x0400) != 0;
  281. return r;
  282. }
  283. static void ata_log_failure(int write, unsigned int lba, unsigned int count,
  284. const char *what, unsigned short base)
  285. {
  286. unsigned char st = port_inb(base + REG_CMD);
  287. unsigned char err = port_inb(base + REG_ERROR);
  288. serial_puts(SERIAL_COM1, "ata: ");
  289. serial_puts(SERIAL_COM1, write ? "write" : "read");
  290. serial_puts(SERIAL_COM1, " failed (");
  291. serial_puts(SERIAL_COM1, what);
  292. serial_puts(SERIAL_COM1, ") lba ");
  293. serial_putdec(SERIAL_COM1, lba);
  294. serial_puts(SERIAL_COM1, " count ");
  295. serial_putdec(SERIAL_COM1, count);
  296. serial_puts(SERIAL_COM1, " status ");
  297. serial_puthex(SERIAL_COM1, st);
  298. serial_puts(SERIAL_COM1, " error ");
  299. serial_puthex(SERIAL_COM1, err);
  300. serial_puts(SERIAL_COM1, " cnt ");
  301. serial_puthex(SERIAL_COM1, port_inb(base + REG_COUNT));
  302. serial_puts(SERIAL_COM1, " lba ");
  303. serial_puthex(SERIAL_COM1,
  304. port_inb(base + REG_LBA_LO) |
  305. (port_inb(base + REG_LBA_MID) << 8) |
  306. (port_inb(base + REG_LBA_HI) << 16));
  307. serial_putc(SERIAL_COM1, '\n');
  308. }
  309. /* Use explicit 16-bit port reads/writes instead of `rep ins/outs`. The
  310. * string form writes through ES:EDI; the C loop uses the normal data
  311. * segment and is the same path IDENTIFY already uses successfully on the
  312. * real MCP79 board. A sector is only 256 words, so the speed difference
  313. * is small compared with the drive's PIO timing. */
  314. static void ata_insw(unsigned short port, void *dst, unsigned int count)
  315. {
  316. unsigned short *p = (unsigned short *)dst;
  317. while (count--)
  318. *p++ = port_inw(port);
  319. }
  320. static void ata_outsw(unsigned short port, const void *src, unsigned int count)
  321. {
  322. const unsigned short *p = (const unsigned short *)src;
  323. while (count--)
  324. port_outw(port, *p++);
  325. }
  326. static int ata_xfer(int bus, int unit, unsigned int lba, void *buf,
  327. unsigned int count, int write)
  328. {
  329. unsigned short base = ata_base[bus];
  330. unsigned char *p = buf;
  331. unsigned int done = 0, i;
  332. if (ata_wait_idle(base, 1000) != 0)
  333. ata_reset_channel(base, ata_ctrl[bus]);
  334. while (done < count) {
  335. unsigned int chunk = count - done;
  336. int lba48;
  337. unsigned int cur = lba + done;
  338. if (ata_single[bus])
  339. chunk = 1;
  340. else if (chunk > 255)
  341. chunk = 255;
  342. lba48 = ata_lba48[bus] ||
  343. ((cur + chunk - 1) >= 0x10000000u);
  344. {
  345. unsigned char cmd;
  346. if (lba48) {
  347. port_outb(base + REG_DRIVE, (unsigned char)(0x40 | (unit << 4)));
  348. port_outb(base + REG_COUNT, (unsigned char)(chunk >> 8));
  349. port_outb(base + REG_LBA_LO, (unsigned char)(cur >> 24));
  350. port_outb(base + REG_LBA_MID, 0);
  351. port_outb(base + REG_LBA_HI, 0);
  352. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  353. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  354. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  355. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  356. cmd = (unsigned char)(write ? CMD_WRITE_EXT : CMD_READ_EXT);
  357. } else {
  358. port_outb(base + REG_DRIVE,
  359. (unsigned char)(0xE0 | (unit << 4) | ((cur >> 24) & 0x0F)));
  360. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  361. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  362. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  363. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  364. cmd = (unsigned char)(write ? CMD_WRITE : CMD_READ);
  365. }
  366. if (!ata_tf_logged) {
  367. ata_tf_logged = 1;
  368. serial_puts(SERIAL_COM1, "ata: tf cnt ");
  369. serial_puthex(SERIAL_COM1, port_inb(base + REG_COUNT));
  370. serial_puts(SERIAL_COM1, " lba ");
  371. serial_puthex(SERIAL_COM1,
  372. port_inb(base + REG_LBA_LO) |
  373. (port_inb(base + REG_LBA_MID) << 8) |
  374. (port_inb(base + REG_LBA_HI) << 16));
  375. serial_puts(SERIAL_COM1, " drv ");
  376. serial_puthex(SERIAL_COM1, port_inb(base + REG_DRIVE));
  377. serial_puts(SERIAL_COM1, " cmd ");
  378. serial_puthex(SERIAL_COM1, cmd);
  379. serial_putc(SERIAL_COM1, '\n');
  380. }
  381. port_outb(base + REG_CMD, cmd);
  382. }
  383. ata_400ns(base);
  384. /* PIO transfers must wait for DRQ before *each* sector: the
  385. * drive drops DRQ between sectors while it commits the previous
  386. * one, and writing into the data register while DRQ is low is
  387. * silently lost. A single rep outsw over a large chunk looked
  388. * fine for one sector but left the drive waiting for more data
  389. * (DRQ set, count/LBA registers advanced), so the next command
  390. * read stale data and a large format eventually failed. */
  391. for (i = 0; i < chunk; i++) {
  392. if (ata_wait_not_busy(base, ATA_TIMEOUT_MS) != 0) {
  393. ata_log_failure(write, cur + i, 1,
  394. "busy timeout", base);
  395. goto xfer_fail;
  396. }
  397. if (ata_wait_drq(base, ATA_TIMEOUT_MS) != 0) {
  398. ata_log_failure(write, cur + i, 1,
  399. "drq timeout", base);
  400. goto xfer_fail;
  401. }
  402. if (write)
  403. ata_outsw(base + REG_DATA, p + i * 512, 256);
  404. else
  405. ata_insw(base + REG_DATA, p + i * 512, 256);
  406. }
  407. /* Let the last sector finish before the next command. */
  408. if (ata_wait_idle(base, ATA_COMPLETE_TIMEOUT_MS) != 0) {
  409. ata_log_failure(write, cur, chunk, "completion timeout", base);
  410. goto xfer_fail;
  411. }
  412. if (port_inb(base + REG_CMD) & SR_ERR) {
  413. ata_log_failure(write, cur, chunk, "transfer error", base);
  414. goto xfer_fail;
  415. }
  416. p += chunk * 512;
  417. done += chunk;
  418. continue;
  419. xfer_fail:
  420. ata_reset_channel(base, ata_ctrl[bus]);
  421. if (ata_single[bus])
  422. return -1;
  423. /* A multi-sector command upset this bridge; retry from the same
  424. * sector with one ATA command per sector. */
  425. ata_single[bus] = 1;
  426. serial_puts(SERIAL_COM1, "ata: switching to single-sector PIO\n");
  427. }
  428. return 0;
  429. }
  430. int ata_read_sectors(int bus, int unit, unsigned int lba, void *buf, unsigned int count)
  431. {
  432. return ata_xfer(bus, unit, lba, buf, count, 0);
  433. }
  434. int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsigned int count)
  435. {
  436. return ata_xfer(bus, unit, lba, (void *)buf, count, 1);
  437. }
  438. int ata_flush(int bus, int unit)
  439. {
  440. unsigned short base;
  441. if (bus < 0 || bus > 1)
  442. return -1;
  443. base = ata_base[bus];
  444. if (ata_wait_idle(base, 1000) != 0)
  445. ata_reset_channel(base, ata_ctrl[bus]);
  446. port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
  447. port_outb(base + REG_CMD, 0xE7); /* FLUSH CACHE */
  448. ata_400ns(base);
  449. if (ata_wait_idle(base, ATA_TIMEOUT_MS) != 0) {
  450. ata_log_failure(1, 0, 0, "cache flush timeout", base);
  451. ata_reset_channel(base, ata_ctrl[bus]);
  452. return -1;
  453. }
  454. if (port_inb(base + REG_CMD) & SR_ERR) {
  455. ata_log_failure(1, 0, 0, "cache flush error", base);
  456. ata_reset_channel(base, ata_ctrl[bus]);
  457. return -1;
  458. }
  459. return 0;
  460. }