Channel.cpp 12 KB

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