ata.c 17 KB

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