IDEDiskDevice.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "IDEDiskDevice.h"
  2. #include <AK/Types.h>
  3. #include "Process.h"
  4. #include "StdLib.h"
  5. #include "IO.h"
  6. #include "Scheduler.h"
  7. #include "PIC.h"
  8. #include <Kernel/Lock.h>
  9. //#define DISK_DEBUG
  10. #define IRQ_FIXED_DISK 14
  11. #define IDE0_DATA 0x1F0
  12. #define IDE0_STATUS 0x1F7
  13. #define IDE0_COMMAND 0x1F7
  14. enum IDECommand : byte {
  15. IDENTIFY_DRIVE = 0xEC,
  16. READ_SECTORS = 0x21,
  17. WRITE_SECTORS = 0x30,
  18. FLUSH_CACHE = 0xe7,
  19. };
  20. enum IDEStatus : byte {
  21. BUSY = (1 << 7),
  22. DRDY = (1 << 6),
  23. DF = (1 << 5),
  24. SRV = (1 << 4),
  25. DRQ = (1 << 3),
  26. CORR = (1 << 2),
  27. IDX = (1 << 1),
  28. ERR = (1 << 0),
  29. };
  30. Retained<IDEDiskDevice> IDEDiskDevice::create()
  31. {
  32. return adopt(*new IDEDiskDevice);
  33. }
  34. IDEDiskDevice::IDEDiskDevice()
  35. : IRQHandler(IRQ_FIXED_DISK)
  36. , m_lock("IDEDiskDevice")
  37. {
  38. initialize();
  39. }
  40. IDEDiskDevice::~IDEDiskDevice()
  41. {
  42. }
  43. const char* IDEDiskDevice::class_name() const
  44. {
  45. return "IDEDiskDevice";
  46. }
  47. unsigned IDEDiskDevice::block_size() const
  48. {
  49. return 512;
  50. }
  51. bool IDEDiskDevice::read_block(unsigned index, byte* out) const
  52. {
  53. return const_cast<IDEDiskDevice&>(*this).read_sectors(index, 1, out);
  54. }
  55. bool IDEDiskDevice::write_block(unsigned index, const byte* data)
  56. {
  57. return write_sectors(index, 1, data);
  58. }
  59. static void print_ide_status(byte status)
  60. {
  61. kprintf("DRQ=%u BUSY=%u DRDY=%u SRV=%u DF=%u CORR=%u IDX=%u ERR=%u\n",
  62. (status & DRQ) != 0,
  63. (status & BUSY) != 0,
  64. (status & DRDY) != 0,
  65. (status & SRV) != 0,
  66. (status & DF) != 0,
  67. (status & CORR) != 0,
  68. (status & IDX) != 0,
  69. (status & ERR) != 0);
  70. }
  71. bool IDEDiskDevice::wait_for_irq()
  72. {
  73. #ifdef DISK_DEBUG
  74. kprintf("disk: waiting for interrupt...\n");
  75. #endif
  76. // FIXME: Add timeout.
  77. while (!m_interrupted) {
  78. // FIXME: Put this process into a Blocked state instead, it's stupid to wake up just to check a flag.
  79. Scheduler::yield();
  80. }
  81. #ifdef DISK_DEBUG
  82. kprintf("disk: got interrupt!\n");
  83. #endif
  84. memory_barrier();
  85. return true;
  86. }
  87. void IDEDiskDevice::handle_irq()
  88. {
  89. byte status = IO::in8(0x1f7);
  90. if (status & ERR) {
  91. print_ide_status(status);
  92. m_device_error = IO::in8(0x1f1);
  93. kprintf("IDEDiskDevice: Error %b!\n", m_device_error);
  94. } else {
  95. m_device_error = 0;
  96. }
  97. #ifdef DISK_DEBUG
  98. kprintf("disk:interrupt: DRQ=%u BUSY=%u DRDY=%u\n", (status & DRQ) != 0, (status & BUSY) != 0, (status & DRDY) != 0);
  99. #endif
  100. m_interrupted = true;
  101. }
  102. void IDEDiskDevice::initialize()
  103. {
  104. #ifdef DISK_DEBUG
  105. byte status = IO::in8(IDE0_STATUS);
  106. kprintf("initial status: ");
  107. print_ide_status(status);
  108. #endif
  109. m_interrupted = false;
  110. while (IO::in8(IDE0_STATUS) & BUSY);
  111. enable_irq();
  112. IO::out8(0x1F6, 0xA0); // 0xB0 for 2nd device
  113. IO::out8(0x3F6, 0xA0); // 0xB0 for 2nd device
  114. IO::out8(IDE0_COMMAND, IDENTIFY_DRIVE);
  115. enable_irq();
  116. wait_for_irq();
  117. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  118. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  119. byte* b = bbuf.pointer();
  120. word* w = (word*)wbuf.pointer();
  121. const word* wbufbase = (word*)wbuf.pointer();
  122. for (dword i = 0; i < 256; ++i) {
  123. word data = IO::in16(IDE0_DATA);
  124. *(w++) = data;
  125. *(b++) = MSB(data);
  126. *(b++) = LSB(data);
  127. }
  128. // "Unpad" the device name string.
  129. for (dword i = 93; i > 54 && bbuf[i] == ' '; --i)
  130. bbuf[i] = 0;
  131. m_cylinders = wbufbase[1];
  132. m_heads = wbufbase[3];
  133. m_sectors_per_track = wbufbase[6];
  134. kprintf(
  135. "IDEDiskDevice: Master=\"%s\", C/H/Spt=%u/%u/%u\n",
  136. bbuf.pointer() + 54,
  137. m_cylinders,
  138. m_heads,
  139. m_sectors_per_track
  140. );
  141. }
  142. bool IDEDiskDevice::read_sectors(dword start_sector, word count, byte* outbuf)
  143. {
  144. ASSERT(count <= 256);
  145. LOCKER(m_lock);
  146. #ifdef DISK_DEBUG
  147. dbgprintf("%s: Disk::read_sectors request (%u sector(s) @ %u)\n",
  148. current->process().name().characters(),
  149. count,
  150. start_sector);
  151. #endif
  152. disable_irq();
  153. while (IO::in8(IDE0_STATUS) & BUSY);
  154. #ifdef DISK_DEBUG
  155. kprintf("IDEDiskDevice: Reading %u sector(s) @ LBA %u\n", count, start_sector);
  156. #endif
  157. IO::out8(0x1f2, count == 256 ? 0 : LSB(count));
  158. IO::out8(0x1f3, start_sector & 0xff);
  159. IO::out8(0x1f4, (start_sector >> 8) & 0xff);
  160. IO::out8(0x1f5, (start_sector >> 16) & 0xff);
  161. IO::out8(0x1f6, 0xe0 | ((start_sector >> 24) & 0xf)); // 0xf0 for 2nd device
  162. IO::out8(0x3F6, 0x08);
  163. while (!(IO::in8(IDE0_STATUS) & DRDY));
  164. IO::out8(IDE0_COMMAND, READ_SECTORS);
  165. m_interrupted = false;
  166. enable_irq();
  167. wait_for_irq();
  168. if (m_device_error)
  169. return false;
  170. byte status = IO::in8(0x1f7);
  171. ASSERT(status & DRQ);
  172. #ifdef DISK_DEBUG
  173. kprintf("Retrieving %u bytes (status=%b), outbuf=%p...\n", count * 512, status, outbuf);
  174. #endif
  175. IO::repeated_in16(IDE0_DATA, outbuf, count * 256);
  176. return true;
  177. }
  178. bool IDEDiskDevice::write_sectors(dword start_sector, word count, const byte* data)
  179. {
  180. ASSERT(count <= 256);
  181. LOCKER(m_lock);
  182. #ifdef DISK_DEBUG
  183. dbgprintf("%s(%u): IDEDiskDevice::write_sectors request (%u sector(s) @ %u)\n",
  184. current->process().name().characters(),
  185. current->pid(),
  186. count,
  187. start_sector);
  188. #endif
  189. disable_irq();
  190. while (IO::in8(IDE0_STATUS) & BUSY);
  191. //dbgprintf("IDEDiskDevice: Writing %u sector(s) @ LBA %u\n", count, start_sector);
  192. IO::out8(0x1f2, count == 256 ? 0 : LSB(count));
  193. IO::out8(0x1f3, start_sector & 0xff);
  194. IO::out8(0x1f4, (start_sector >> 8) & 0xff);
  195. IO::out8(0x1f5, (start_sector >> 16) & 0xff);
  196. IO::out8(0x1f6, 0xe0 | ((start_sector >> 24) & 0xf)); // 0xf0 for 2nd device
  197. IO::out8(0x3F6, 0x08);
  198. IO::out8(IDE0_COMMAND, WRITE_SECTORS);
  199. while (!(IO::in8(IDE0_STATUS) & DRQ));
  200. byte status = IO::in8(0x1f7);
  201. ASSERT(status & DRQ);
  202. IO::repeated_out16(IDE0_DATA, data, count * 256);
  203. m_interrupted = false;
  204. enable_irq();
  205. wait_for_irq();
  206. disable_irq();
  207. IO::out8(IDE0_COMMAND, FLUSH_CACHE);
  208. while (IO::in8(IDE0_STATUS) & BUSY);
  209. m_interrupted = false;
  210. enable_irq();
  211. wait_for_irq();
  212. return !m_device_error;
  213. }