Channel.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringView.h>
  9. #include <Kernel/Arch/Delay.h>
  10. #include <Kernel/Bus/PCI/API.h>
  11. #include <Kernel/IOWindow.h>
  12. #include <Kernel/Memory/MemoryManager.h>
  13. #include <Kernel/Process.h>
  14. #include <Kernel/Sections.h>
  15. #include <Kernel/Storage/ATA/ATADiskDevice.h>
  16. #include <Kernel/Storage/ATA/Definitions.h>
  17. #include <Kernel/Storage/ATA/GenericIDE/Channel.h>
  18. #include <Kernel/Storage/ATA/GenericIDE/Controller.h>
  19. #include <Kernel/WorkQueue.h>
  20. namespace Kernel {
  21. #define PATA_PRIMARY_IRQ 14
  22. #define PATA_SECONDARY_IRQ 15
  23. UNMAP_AFTER_INIT NonnullLockRefPtr<IDEChannel> IDEChannel::create(IDEController const& controller, IOWindowGroup io_window_group, ChannelType type)
  24. {
  25. auto ata_identify_data_buffer = KBuffer::try_create_with_size("ATA Identify Page"sv, 4096, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow).release_value();
  26. return adopt_lock_ref(*new IDEChannel(controller, move(io_window_group), type, move(ata_identify_data_buffer)));
  27. }
  28. UNMAP_AFTER_INIT NonnullLockRefPtr<IDEChannel> IDEChannel::create(IDEController const& controller, u8 irq, IOWindowGroup io_window_group, ChannelType type)
  29. {
  30. auto ata_identify_data_buffer = KBuffer::try_create_with_size("ATA Identify Page"sv, 4096, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow).release_value();
  31. return adopt_lock_ref(*new IDEChannel(controller, irq, move(io_window_group), type, move(ata_identify_data_buffer)));
  32. }
  33. StringView IDEChannel::channel_type_string() const
  34. {
  35. if (m_channel_type == ChannelType::Primary)
  36. return "Primary"sv;
  37. return "Secondary"sv;
  38. }
  39. bool IDEChannel::select_device_and_wait_until_not_busy(DeviceType device_type, size_t milliseconds_timeout)
  40. {
  41. microseconds_delay(20);
  42. u8 slave = device_type == DeviceType::Slave;
  43. m_io_window_group.io_window().write8(ATA_REG_HDDEVSEL, 0xA0 | (slave << 4)); // First, we need to select the drive itself
  44. microseconds_delay(20);
  45. size_t time_elapsed = 0;
  46. while (m_io_window_group.control_window().read8(0) & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
  47. microseconds_delay(1000);
  48. time_elapsed++;
  49. }
  50. return time_elapsed <= milliseconds_timeout;
  51. }
  52. ErrorOr<void> IDEChannel::port_phy_reset()
  53. {
  54. MutexLocker locker(m_lock);
  55. SpinlockLocker hard_locker(m_hard_lock);
  56. // reset the channel
  57. u8 device_control = m_io_window_group.control_window().read8(0);
  58. // Wait 30 milliseconds
  59. microseconds_delay(30000);
  60. m_io_window_group.control_window().write8(0, device_control | (1 << 2));
  61. // Wait 30 milliseconds
  62. microseconds_delay(30000);
  63. m_io_window_group.control_window().write8(0, device_control);
  64. // Wait up to 30 seconds before failing
  65. if (!select_device_and_wait_until_not_busy(DeviceType::Master, 30000)) {
  66. dbgln("IDEChannel: reset failed, busy flag on master stuck");
  67. return Error::from_errno(EBUSY);
  68. }
  69. // Wait up to 30 seconds before failing
  70. if (!select_device_and_wait_until_not_busy(DeviceType::Slave, 30000)) {
  71. dbgln("IDEChannel: reset failed, busy flag on slave stuck");
  72. return Error::from_errno(EBUSY);
  73. }
  74. return {};
  75. }
  76. #if ARCH(I386) || ARCH(X86_64)
  77. ErrorOr<void> IDEChannel::allocate_resources_for_pci_ide_controller(Badge<PCIIDELegacyModeController>, bool force_pio)
  78. {
  79. return allocate_resources(force_pio);
  80. }
  81. ErrorOr<void> IDEChannel::allocate_resources_for_isa_ide_controller(Badge<ISAIDEController>)
  82. {
  83. return allocate_resources(true);
  84. }
  85. #endif
  86. UNMAP_AFTER_INIT ErrorOr<void> IDEChannel::allocate_resources(bool force_pio)
  87. {
  88. dbgln_if(PATA_DEBUG, "IDEChannel: {} IO base: {}", channel_type_string(), m_io_window_group.io_window());
  89. dbgln_if(PATA_DEBUG, "IDEChannel: {} control base: {}", channel_type_string(), m_io_window_group.control_window());
  90. if (m_io_window_group.bus_master_window())
  91. dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base: {}", channel_type_string(), m_io_window_group.bus_master_window());
  92. else
  93. dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base disabled", channel_type_string());
  94. if (!force_pio) {
  95. m_dma_enabled = true;
  96. VERIFY(m_io_window_group.bus_master_window());
  97. // Let's try to set up DMA transfers.
  98. m_prdt_region = TRY(MM.allocate_dma_buffer_page("IDE PRDT"sv, Memory::Region::Access::ReadWrite, m_prdt_page));
  99. VERIFY(!m_prdt_page.is_null());
  100. m_dma_buffer_region = TRY(MM.allocate_dma_buffer_page("IDE DMA region"sv, Memory::Region::Access::ReadWrite, m_dma_buffer_page));
  101. VERIFY(!m_dma_buffer_page.is_null());
  102. prdt().end_of_table = 0x8000;
  103. // clear bus master interrupt status
  104. m_io_window_group.bus_master_window()->write8(2, m_io_window_group.bus_master_window()->read8(2) | 4);
  105. }
  106. return {};
  107. }
  108. UNMAP_AFTER_INIT IDEChannel::IDEChannel(IDEController const& controller, u8 irq, IOWindowGroup io_group, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer)
  109. : ATAPort(controller, (type == ChannelType::Primary ? 0 : 1), move(ata_identify_data_buffer))
  110. , IRQHandler(irq)
  111. , m_channel_type(type)
  112. , m_io_window_group(move(io_group))
  113. {
  114. }
  115. UNMAP_AFTER_INIT IDEChannel::IDEChannel(IDEController const& controller, IOWindowGroup io_group, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer)
  116. : ATAPort(controller, (type == ChannelType::Primary ? 0 : 1), move(ata_identify_data_buffer))
  117. , IRQHandler(type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ)
  118. , m_channel_type(type)
  119. , m_io_window_group(move(io_group))
  120. {
  121. }
  122. UNMAP_AFTER_INIT IDEChannel::~IDEChannel() = default;
  123. bool IDEChannel::handle_irq(RegisterState const&)
  124. {
  125. auto result = handle_interrupt_after_dma_transaction();
  126. // FIXME: Propagate errors properly
  127. VERIFY(!result.is_error());
  128. return result.release_value();
  129. }
  130. ErrorOr<void> IDEChannel::stop_busmastering()
  131. {
  132. VERIFY(m_lock.is_locked());
  133. VERIFY(m_io_window_group.bus_master_window());
  134. m_io_window_group.bus_master_window()->write8(0, 0);
  135. return {};
  136. }
  137. ErrorOr<void> IDEChannel::start_busmastering(TransactionDirection direction)
  138. {
  139. VERIFY(m_lock.is_locked());
  140. VERIFY(m_io_window_group.bus_master_window());
  141. m_io_window_group.bus_master_window()->write8(0, (direction != TransactionDirection::Write ? 0x9 : 0x1));
  142. return {};
  143. }
  144. ErrorOr<void> IDEChannel::force_busmastering_status_clean()
  145. {
  146. VERIFY(m_lock.is_locked());
  147. VERIFY(m_io_window_group.bus_master_window());
  148. m_io_window_group.bus_master_window()->write8(2, m_io_window_group.bus_master_window()->read8(2) | 4);
  149. return {};
  150. }
  151. ErrorOr<u8> IDEChannel::busmastering_status()
  152. {
  153. VERIFY(m_io_window_group.bus_master_window());
  154. return m_io_window_group.bus_master_window()->read8(2);
  155. }
  156. ErrorOr<void> IDEChannel::prepare_transaction_with_busmastering(TransactionDirection direction, PhysicalAddress prdt_buffer)
  157. {
  158. VERIFY(m_lock.is_locked());
  159. m_io_window_group.bus_master_window()->write32(4, prdt_buffer.get());
  160. m_io_window_group.bus_master_window()->write8(0, direction != TransactionDirection::Write ? 0x8 : 0);
  161. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  162. m_io_window_group.bus_master_window()->write8(2, m_io_window_group.bus_master_window()->read8(2) | 0x6);
  163. return {};
  164. }
  165. ErrorOr<void> IDEChannel::initiate_transaction(TransactionDirection)
  166. {
  167. VERIFY(m_lock.is_locked());
  168. return {};
  169. }
  170. ErrorOr<u8> IDEChannel::task_file_status()
  171. {
  172. VERIFY(m_lock.is_locked());
  173. return m_io_window_group.control_window().read8(0);
  174. }
  175. ErrorOr<u8> IDEChannel::task_file_error()
  176. {
  177. VERIFY(m_lock.is_locked());
  178. return m_io_window_group.io_window().read8(ATA_REG_ERROR);
  179. }
  180. ErrorOr<bool> IDEChannel::detect_presence_on_selected_device()
  181. {
  182. VERIFY(m_lock.is_locked());
  183. m_io_window_group.io_window().write8(ATA_REG_SECCOUNT0, 0x55);
  184. m_io_window_group.io_window().write8(ATA_REG_LBA0, 0xAA);
  185. m_io_window_group.io_window().write8(ATA_REG_SECCOUNT0, 0xAA);
  186. m_io_window_group.io_window().write8(ATA_REG_LBA0, 0x55);
  187. m_io_window_group.io_window().write8(ATA_REG_SECCOUNT0, 0x55);
  188. m_io_window_group.io_window().write8(ATA_REG_LBA0, 0xAA);
  189. auto nsectors_value = m_io_window_group.io_window().read8(ATA_REG_SECCOUNT0);
  190. auto lba0 = m_io_window_group.io_window().read8(ATA_REG_LBA0);
  191. if (lba0 == 0xAA && nsectors_value == 0x55)
  192. return true;
  193. return false;
  194. }
  195. ErrorOr<void> IDEChannel::wait_if_busy_until_timeout(size_t timeout_in_milliseconds)
  196. {
  197. size_t time_elapsed = 0;
  198. while (m_io_window_group.control_window().read8(0) & ATA_SR_BSY && time_elapsed <= timeout_in_milliseconds) {
  199. microseconds_delay(1000);
  200. time_elapsed++;
  201. }
  202. if (time_elapsed <= timeout_in_milliseconds)
  203. return {};
  204. return Error::from_errno(EBUSY);
  205. }
  206. ErrorOr<void> IDEChannel::force_clear_interrupts()
  207. {
  208. VERIFY(m_lock.is_locked());
  209. m_io_window_group.io_window().read8(ATA_REG_STATUS);
  210. return {};
  211. }
  212. ErrorOr<void> IDEChannel::load_taskfile_into_registers(ATAPort::TaskFile const& task_file, LBAMode lba_mode, size_t completion_timeout_in_milliseconds)
  213. {
  214. VERIFY(m_lock.is_locked());
  215. VERIFY(m_hard_lock.is_locked());
  216. u8 head = 0;
  217. if (lba_mode == LBAMode::FortyEightBit) {
  218. head = 0;
  219. } else if (lba_mode == LBAMode::TwentyEightBit) {
  220. head = (task_file.lba_high[0] & 0x0F);
  221. }
  222. // Note: Preserve the selected drive, always use LBA addressing
  223. auto driver_register = ((m_io_window_group.io_window().read8(ATA_REG_HDDEVSEL) & (1 << 4)) | (head | (1 << 5) | (1 << 6)));
  224. m_io_window_group.io_window().write8(ATA_REG_HDDEVSEL, driver_register);
  225. microseconds_delay(50);
  226. if (lba_mode == LBAMode::FortyEightBit) {
  227. m_io_window_group.io_window().write8(ATA_REG_SECCOUNT1, (task_file.count >> 8) & 0xFF);
  228. m_io_window_group.io_window().write8(ATA_REG_LBA3, task_file.lba_high[0]);
  229. m_io_window_group.io_window().write8(ATA_REG_LBA4, task_file.lba_high[1]);
  230. m_io_window_group.io_window().write8(ATA_REG_LBA5, task_file.lba_high[2]);
  231. }
  232. m_io_window_group.io_window().write8(ATA_REG_SECCOUNT0, task_file.count & 0xFF);
  233. m_io_window_group.io_window().write8(ATA_REG_LBA0, task_file.lba_low[0]);
  234. m_io_window_group.io_window().write8(ATA_REG_LBA1, task_file.lba_low[1]);
  235. m_io_window_group.io_window().write8(ATA_REG_LBA2, task_file.lba_low[2]);
  236. // FIXME: Set a timeout here?
  237. size_t time_elapsed = 0;
  238. for (;;) {
  239. if (time_elapsed > completion_timeout_in_milliseconds)
  240. return Error::from_errno(EBUSY);
  241. // FIXME: Use task_file_status method
  242. auto status = m_io_window_group.control_window().read8(0);
  243. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  244. break;
  245. microseconds_delay(1000);
  246. time_elapsed++;
  247. }
  248. m_io_window_group.io_window().write8(ATA_REG_COMMAND, task_file.command);
  249. return {};
  250. }
  251. ErrorOr<void> IDEChannel::device_select(size_t device_index)
  252. {
  253. VERIFY(m_lock.is_locked());
  254. if (device_index > 1)
  255. return Error::from_errno(EINVAL);
  256. microseconds_delay(20);
  257. m_io_window_group.io_window().write8(ATA_REG_HDDEVSEL, (0xA0 | ((device_index) << 4)));
  258. microseconds_delay(20);
  259. return {};
  260. }
  261. ErrorOr<void> IDEChannel::enable_interrupts()
  262. {
  263. VERIFY(m_lock.is_locked());
  264. m_io_window_group.control_window().write8(0, 0);
  265. m_interrupts_enabled = true;
  266. return {};
  267. }
  268. ErrorOr<void> IDEChannel::disable_interrupts()
  269. {
  270. VERIFY(m_lock.is_locked());
  271. m_io_window_group.control_window().write8(0, 1 << 1);
  272. m_interrupts_enabled = false;
  273. return {};
  274. }
  275. ErrorOr<void> IDEChannel::read_pio_data_to_buffer(UserOrKernelBuffer& buffer, size_t block_offset, size_t words_count)
  276. {
  277. VERIFY(m_lock.is_locked());
  278. VERIFY(words_count == 256);
  279. for (u32 i = 0; i < 256; ++i) {
  280. u16 data = m_io_window_group.io_window().read16(ATA_REG_DATA);
  281. // FIXME: Don't assume 512 bytes sector
  282. TRY(buffer.write(&data, block_offset * 512 + (i * 2), 2));
  283. }
  284. return {};
  285. }
  286. ErrorOr<void> IDEChannel::write_pio_data_from_buffer(UserOrKernelBuffer const& buffer, size_t block_offset, size_t words_count)
  287. {
  288. VERIFY(m_lock.is_locked());
  289. VERIFY(words_count == 256);
  290. for (u32 i = 0; i < 256; ++i) {
  291. u16 buf;
  292. // FIXME: Don't assume 512 bytes sector
  293. TRY(buffer.read(&buf, block_offset * 512 + (i * 2), 2));
  294. m_io_window_group.io_window().write16(ATA_REG_DATA, buf);
  295. }
  296. return {};
  297. }
  298. }