IDEChannel.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/Memory/MemoryManager.h>
  11. #include <Kernel/Process.h>
  12. #include <Kernel/Sections.h>
  13. #include <Kernel/Storage/ATA/ATA.h>
  14. #include <Kernel/Storage/ATA/ATADiskDevice.h>
  15. #include <Kernel/Storage/ATA/IDEChannel.h>
  16. #include <Kernel/Storage/ATA/IDEController.h>
  17. #include <Kernel/WorkQueue.h>
  18. namespace Kernel {
  19. #define PATA_PRIMARY_IRQ 14
  20. #define PATA_SECONDARY_IRQ 15
  21. UNMAP_AFTER_INIT NonnullRefPtr<IDEChannel> IDEChannel::create(const IDEController& controller, IOAddressGroup io_group, ChannelType type)
  22. {
  23. return adopt_ref(*new IDEChannel(controller, io_group, type));
  24. }
  25. UNMAP_AFTER_INIT NonnullRefPtr<IDEChannel> IDEChannel::create(const IDEController& controller, u8 irq, IOAddressGroup io_group, ChannelType type)
  26. {
  27. return adopt_ref(*new IDEChannel(controller, irq, io_group, type));
  28. }
  29. RefPtr<StorageDevice> IDEChannel::master_device() const
  30. {
  31. return m_master;
  32. }
  33. RefPtr<StorageDevice> IDEChannel::slave_device() const
  34. {
  35. return m_slave;
  36. }
  37. UNMAP_AFTER_INIT void IDEChannel::initialize()
  38. {
  39. disable_irq();
  40. dbgln_if(PATA_DEBUG, "IDEChannel: {} IO base: {}", channel_type_string(), m_io_group.io_base());
  41. dbgln_if(PATA_DEBUG, "IDEChannel: {} control base: {}", channel_type_string(), m_io_group.control_base());
  42. if (m_io_group.bus_master_base().has_value())
  43. dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base: {}", channel_type_string(), m_io_group.bus_master_base().value());
  44. else
  45. dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base disabled", channel_type_string());
  46. m_parent_controller->enable_pin_based_interrupts();
  47. // reset the channel
  48. u8 device_control = m_io_group.control_base().in<u8>();
  49. // Wait 30 milliseconds
  50. IO::delay(30000);
  51. m_io_group.control_base().out<u8>(device_control | (1 << 2));
  52. // Wait 30 milliseconds
  53. IO::delay(30000);
  54. m_io_group.control_base().out<u8>(device_control);
  55. // Wait up to 30 seconds before failing
  56. if (!select_device_and_wait_until_not_busy(DeviceType::Master, 30000)) {
  57. dbgln("IDEChannel: reset failed, busy flag on master stuck");
  58. return;
  59. }
  60. // Wait up to 30 seconds before failing
  61. if (!select_device_and_wait_until_not_busy(DeviceType::Slave, 30000)) {
  62. dbgln("IDEChannel: reset failed, busy flag on slave stuck");
  63. return;
  64. }
  65. detect_disks();
  66. // Note: calling to detect_disks could generate an interrupt, clear it if that's the case
  67. clear_pending_interrupts();
  68. }
  69. UNMAP_AFTER_INIT IDEChannel::IDEChannel(const IDEController& controller, u8 irq, IOAddressGroup io_group, ChannelType type)
  70. : IRQHandler(irq)
  71. , m_channel_type(type)
  72. , m_io_group(io_group)
  73. , m_parent_controller(controller)
  74. {
  75. initialize();
  76. }
  77. UNMAP_AFTER_INIT IDEChannel::IDEChannel(const IDEController& controller, IOAddressGroup io_group, ChannelType type)
  78. : IRQHandler(type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ)
  79. , m_channel_type(type)
  80. , m_io_group(io_group)
  81. , m_parent_controller(controller)
  82. {
  83. initialize();
  84. }
  85. void IDEChannel::clear_pending_interrupts() const
  86. {
  87. m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  88. }
  89. UNMAP_AFTER_INIT IDEChannel::~IDEChannel()
  90. {
  91. }
  92. void IDEChannel::start_request(AsyncBlockDeviceRequest& request, bool is_slave, u16 capabilities)
  93. {
  94. MutexLocker locker(m_lock);
  95. VERIFY(m_current_request.is_null());
  96. dbgln_if(PATA_DEBUG, "IDEChannel::start_request");
  97. m_current_request = request;
  98. m_current_request_block_index = 0;
  99. m_current_request_flushing_cache = false;
  100. if (request.request_type() == AsyncBlockDeviceRequest::Read)
  101. ata_read_sectors(is_slave, capabilities);
  102. else
  103. ata_write_sectors(is_slave, capabilities);
  104. }
  105. void IDEChannel::complete_current_request(AsyncDeviceRequest::RequestResult result)
  106. {
  107. // NOTE: this may be called from the interrupt handler!
  108. VERIFY(m_current_request);
  109. VERIFY(m_request_lock.is_locked());
  110. // Now schedule reading back the buffer as soon as we leave the irq handler.
  111. // This is important so that we can safely write the buffer back,
  112. // which could cause page faults. Note that this may be called immediately
  113. // before Processor::deferred_call_queue returns!
  114. g_io_work->queue([this, result]() {
  115. dbgln_if(PATA_DEBUG, "IDEChannel::complete_current_request result: {}", (int)result);
  116. MutexLocker locker(m_lock);
  117. VERIFY(m_current_request);
  118. auto current_request = m_current_request;
  119. m_current_request.clear();
  120. current_request->complete(result);
  121. });
  122. }
  123. static void print_ide_status(u8 status)
  124. {
  125. dbgln("IDEChannel: print_ide_status: DRQ={} BSY={}, DRDY={}, DSC={}, DF={}, CORR={}, IDX={}, ERR={}",
  126. (status & ATA_SR_DRQ) != 0,
  127. (status & ATA_SR_BSY) != 0,
  128. (status & ATA_SR_DRDY) != 0,
  129. (status & ATA_SR_DSC) != 0,
  130. (status & ATA_SR_DF) != 0,
  131. (status & ATA_SR_CORR) != 0,
  132. (status & ATA_SR_IDX) != 0,
  133. (status & ATA_SR_ERR) != 0);
  134. }
  135. void IDEChannel::try_disambiguate_error()
  136. {
  137. VERIFY(m_lock.is_locked());
  138. dbgln("IDEChannel: Error cause:");
  139. switch (m_device_error) {
  140. case ATA_ER_BBK:
  141. dbgln("IDEChannel: - Bad block");
  142. break;
  143. case ATA_ER_UNC:
  144. dbgln("IDEChannel: - Uncorrectable data");
  145. break;
  146. case ATA_ER_MC:
  147. dbgln("IDEChannel: - Media changed");
  148. break;
  149. case ATA_ER_IDNF:
  150. dbgln("IDEChannel: - ID mark not found");
  151. break;
  152. case ATA_ER_MCR:
  153. dbgln("IDEChannel: - Media change request");
  154. break;
  155. case ATA_ER_ABRT:
  156. dbgln("IDEChannel: - Command aborted");
  157. break;
  158. case ATA_ER_TK0NF:
  159. dbgln("IDEChannel: - Track 0 not found");
  160. break;
  161. case ATA_ER_AMNF:
  162. dbgln("IDEChannel: - No address mark");
  163. break;
  164. default:
  165. dbgln("IDEChannel: - No one knows");
  166. break;
  167. }
  168. }
  169. bool IDEChannel::handle_irq(const RegisterState&)
  170. {
  171. u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  172. m_entropy_source.add_random_event(status);
  173. SpinlockLocker lock(m_request_lock);
  174. dbgln_if(PATA_DEBUG, "IDEChannel: interrupt: DRQ={}, BSY={}, DRDY={}",
  175. (status & ATA_SR_DRQ) != 0,
  176. (status & ATA_SR_BSY) != 0,
  177. (status & ATA_SR_DRDY) != 0);
  178. if (!m_current_request) {
  179. dbgln("IDEChannel: IRQ but no pending request!");
  180. return false;
  181. }
  182. if (status & ATA_SR_ERR) {
  183. print_ide_status(status);
  184. m_device_error = m_io_group.io_base().offset(ATA_REG_ERROR).in<u8>();
  185. dbgln("IDEChannel: Error {:#02x}!", (u8)m_device_error);
  186. try_disambiguate_error();
  187. complete_current_request(AsyncDeviceRequest::Failure);
  188. return true;
  189. }
  190. m_device_error = 0;
  191. // Now schedule reading/writing the buffer as soon as we leave the irq handler.
  192. // This is important so that we can safely access the buffers, which could
  193. // trigger page faults
  194. g_io_work->queue([this]() {
  195. MutexLocker locker(m_lock);
  196. SpinlockLocker lock(m_request_lock);
  197. if (m_current_request->request_type() == AsyncBlockDeviceRequest::Read) {
  198. dbgln_if(PATA_DEBUG, "IDEChannel: Read block {}/{}", m_current_request_block_index, m_current_request->block_count());
  199. if (ata_do_read_sector()) {
  200. if (++m_current_request_block_index >= m_current_request->block_count()) {
  201. complete_current_request(AsyncDeviceRequest::Success);
  202. return;
  203. }
  204. // Wait for the next block
  205. enable_irq();
  206. }
  207. } else {
  208. if (!m_current_request_flushing_cache) {
  209. dbgln_if(PATA_DEBUG, "IDEChannel: Wrote block {}/{}", m_current_request_block_index, m_current_request->block_count());
  210. if (++m_current_request_block_index >= m_current_request->block_count()) {
  211. // We read the last block, flush cache
  212. VERIFY(!m_current_request_flushing_cache);
  213. m_current_request_flushing_cache = true;
  214. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_CACHE_FLUSH);
  215. } else {
  216. // Read next block
  217. ata_do_write_sector();
  218. }
  219. } else {
  220. complete_current_request(AsyncDeviceRequest::Success);
  221. }
  222. }
  223. });
  224. return true;
  225. }
  226. static void io_delay()
  227. {
  228. for (int i = 0; i < 4; ++i)
  229. IO::in8(0x3f6);
  230. }
  231. bool IDEChannel::select_device_and_wait_until_not_busy(DeviceType device_type, size_t milliseconds_timeout)
  232. {
  233. IO::delay(20);
  234. u8 slave = device_type == DeviceType::Slave;
  235. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (slave << 4));
  236. IO::delay(20);
  237. size_t time_elapsed = 0;
  238. while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
  239. IO::delay(1000);
  240. time_elapsed++;
  241. }
  242. return time_elapsed <= milliseconds_timeout;
  243. }
  244. bool IDEChannel::wait_until_not_busy(size_t milliseconds_timeout)
  245. {
  246. size_t time_elapsed = 0;
  247. while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
  248. IO::delay(1000);
  249. time_elapsed++;
  250. }
  251. return time_elapsed <= milliseconds_timeout;
  252. }
  253. StringView IDEChannel::channel_type_string() const
  254. {
  255. if (m_channel_type == ChannelType::Primary)
  256. return "Primary"sv;
  257. return "Secondary"sv;
  258. }
  259. UNMAP_AFTER_INIT void IDEChannel::detect_disks()
  260. {
  261. auto channel_string = [](u8 i) -> StringView {
  262. if (i == 0)
  263. return "master"sv;
  264. return "slave"sv;
  265. };
  266. // There are only two possible disks connected to a channel
  267. for (auto i = 0; i < 2; i++) {
  268. if (!select_device_and_wait_until_not_busy(i == 0 ? DeviceType::Master : DeviceType::Slave, 32000)) {
  269. dbgln("IDEChannel: Timeout waiting for busy flag to clear during {} {} detection", channel_type_string(), channel_string(i));
  270. continue;
  271. }
  272. auto status = m_io_group.control_base().in<u8>();
  273. if (status == 0x0) {
  274. dbgln_if(PATA_DEBUG, "IDEChannel: No {} {} disk detected!", channel_type_string(), channel_string(i));
  275. continue;
  276. }
  277. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  278. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  279. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  280. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  281. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
  282. // Wait 10 second for the BSY flag to clear
  283. if (!wait_until_not_busy(2000)) {
  284. dbgln_if(PATA_DEBUG, "IDEChannel: No {} {} disk detected, BSY flag was not reset!", channel_type_string(), channel_string(i));
  285. continue;
  286. }
  287. bool check_for_atapi = false;
  288. bool device_presence = true;
  289. bool command_set_is_atapi = false;
  290. size_t milliseconds_elapsed = 0;
  291. for (;;) {
  292. // Wait about 10 seconds
  293. if (milliseconds_elapsed > 2000)
  294. break;
  295. u8 status = m_io_group.control_base().in<u8>();
  296. if (status & ATA_SR_ERR) {
  297. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device is not ATA. Will check for ATAPI.", channel_type_string(), channel_string(i));
  298. check_for_atapi = true;
  299. break;
  300. }
  301. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) {
  302. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device appears to be ATA.", channel_type_string(), channel_string(i));
  303. break;
  304. }
  305. if (status == 0 || status == 0xFF) {
  306. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device presence - none.", channel_type_string(), channel_string(i));
  307. device_presence = false;
  308. break;
  309. }
  310. IO::delay(1000);
  311. milliseconds_elapsed++;
  312. }
  313. if (!device_presence) {
  314. continue;
  315. }
  316. if (milliseconds_elapsed > 10000) {
  317. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device state unknown. Timeout exceeded.", channel_type_string(), channel_string(i));
  318. continue;
  319. }
  320. if (check_for_atapi) {
  321. u8 cl = m_io_group.io_base().offset(ATA_REG_LBA1).in<u8>();
  322. u8 ch = m_io_group.io_base().offset(ATA_REG_LBA2).in<u8>();
  323. if ((cl == 0x14 && ch == 0xEB) || (cl == 0x69 && ch == 0x96)) {
  324. command_set_is_atapi = true;
  325. dbgln("IDEChannel: {} {} device appears to be ATAPI. We're going to ignore it for now as we don't support it.", channel_type_string(), channel_string(i));
  326. continue;
  327. } else {
  328. dbgln("IDEChannel: {} {} device doesn't appear to be ATA or ATAPI. Ignoring it.", channel_type_string(), channel_string(i));
  329. continue;
  330. }
  331. }
  332. // FIXME: Handle possible OOM situation here.
  333. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value();
  334. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value();
  335. u8* b = bbuf.data();
  336. u16* w = (u16*)wbuf.data();
  337. for (u32 i = 0; i < 256; ++i) {
  338. u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
  339. *(w++) = data;
  340. *(b++) = MSB(data);
  341. *(b++) = LSB(data);
  342. }
  343. // "Unpad" the device name string.
  344. for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
  345. bbuf[i] = 0;
  346. volatile ATAIdentifyBlock& identify_block = (volatile ATAIdentifyBlock&)(*wbuf.data());
  347. u16 capabilities = identify_block.capabilities[0];
  348. // If the drive is so old that it doesn't support LBA, ignore it.
  349. if (!(capabilities & ATA_CAP_LBA))
  350. continue;
  351. u64 max_addressable_block = identify_block.max_28_bit_addressable_logical_sector;
  352. // if we support 48-bit LBA, use that value instead.
  353. if (identify_block.commands_and_feature_sets_supported[1] & (1 << 10))
  354. max_addressable_block = identify_block.user_addressable_logical_sectors_count;
  355. dbgln("IDEChannel: {} {} {} device found: Name={}, Capacity={}, Capabilities={:#04x}", channel_type_string(), channel_string(i), !command_set_is_atapi ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), max_addressable_block * 512, capabilities);
  356. // FIXME: Don't assume all drives will have logical sector size of 512 bytes.
  357. ATADevice::Address address = { m_channel_type == ChannelType::Primary ? static_cast<u8>(0) : static_cast<u8>(1), static_cast<u8>(i) };
  358. if (i == 0) {
  359. m_master = ATADiskDevice::create(m_parent_controller, address, capabilities, 512, max_addressable_block);
  360. } else {
  361. m_slave = ATADiskDevice::create(m_parent_controller, address, capabilities, 512, max_addressable_block);
  362. }
  363. }
  364. }
  365. void IDEChannel::ata_access(Direction direction, bool slave_request, u64 lba, u8 block_count, u16 capabilities)
  366. {
  367. VERIFY(m_lock.is_locked());
  368. VERIFY(m_request_lock.is_locked());
  369. LBAMode lba_mode;
  370. u8 head = 0;
  371. VERIFY(capabilities & ATA_CAP_LBA);
  372. if (lba >= 0x10000000) {
  373. lba_mode = LBAMode::FortyEightBit;
  374. head = 0;
  375. } else {
  376. lba_mode = LBAMode::TwentyEightBit;
  377. head = (lba & 0xF000000) >> 24;
  378. }
  379. // Wait 1 second
  380. wait_until_not_busy(1000);
  381. // We need to select the drive and then we wait 20 microseconds... and it doesn't hurt anything so let's just do it.
  382. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xE0 | (static_cast<u8>(slave_request) << 4) | head);
  383. IO::delay(20);
  384. if (lba_mode == LBAMode::FortyEightBit) {
  385. m_io_group.io_base().offset(ATA_REG_SECCOUNT1).out<u8>(0);
  386. m_io_group.io_base().offset(ATA_REG_LBA3).out<u8>((lba & 0xFF000000) >> 24);
  387. m_io_group.io_base().offset(ATA_REG_LBA4).out<u8>((lba & 0xFF00000000ull) >> 32);
  388. m_io_group.io_base().offset(ATA_REG_LBA5).out<u8>((lba & 0xFF0000000000ull) >> 40);
  389. }
  390. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(block_count);
  391. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000FF) >> 0);
  392. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000FF00) >> 8);
  393. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00FF0000) >> 16);
  394. for (;;) {
  395. auto status = m_io_group.control_base().in<u8>();
  396. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  397. break;
  398. }
  399. send_ata_io_command(lba_mode, direction);
  400. enable_irq();
  401. }
  402. void IDEChannel::send_ata_io_command(LBAMode lba_mode, Direction direction) const
  403. {
  404. if (lba_mode != LBAMode::FortyEightBit) {
  405. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(direction == Direction::Read ? ATA_CMD_READ_PIO : ATA_CMD_WRITE_PIO);
  406. } else {
  407. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(direction == Direction::Read ? ATA_CMD_READ_PIO_EXT : ATA_CMD_WRITE_PIO_EXT);
  408. }
  409. }
  410. bool IDEChannel::ata_do_read_sector()
  411. {
  412. VERIFY(m_lock.is_locked());
  413. VERIFY(m_request_lock.is_locked());
  414. VERIFY(!m_current_request.is_null());
  415. dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector");
  416. auto& request = *m_current_request;
  417. auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
  418. auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](Bytes bytes) {
  419. for (size_t i = 0; i < bytes.size(); i += sizeof(u16))
  420. *(u16*)bytes.offset_pointer(i) = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
  421. return bytes.size();
  422. });
  423. if (result.is_error()) {
  424. // TODO: Do we need to abort the PATA read if this wasn't the last block?
  425. complete_current_request(AsyncDeviceRequest::MemoryFault);
  426. return false;
  427. }
  428. return true;
  429. }
  430. // FIXME: This doesn't quite work and locks up reading LBA 3.
  431. void IDEChannel::ata_read_sectors(bool slave_request, u16 capabilities)
  432. {
  433. VERIFY(m_lock.is_locked());
  434. VERIFY(!m_current_request.is_null());
  435. VERIFY(m_current_request->block_count() <= 256);
  436. SpinlockLocker m_lock(m_request_lock);
  437. dbgln_if(PATA_DEBUG, "IDEChannel::ata_read_sectors");
  438. dbgln_if(PATA_DEBUG, "IDEChannel: Reading {} sector(s) @ LBA {}", m_current_request->block_count(), m_current_request->block_index());
  439. ata_access(Direction::Read, slave_request, m_current_request->block_index(), m_current_request->block_count(), capabilities);
  440. }
  441. void IDEChannel::ata_do_write_sector()
  442. {
  443. VERIFY(m_lock.is_locked());
  444. VERIFY(m_request_lock.is_locked());
  445. VERIFY(!m_current_request.is_null());
  446. auto& request = *m_current_request;
  447. io_delay();
  448. while ((m_io_group.control_base().in<u8>() & ATA_SR_BSY) || !(m_io_group.control_base().in<u8>() & ATA_SR_DRQ))
  449. ;
  450. u8 status = m_io_group.control_base().in<u8>();
  451. VERIFY(status & ATA_SR_DRQ);
  452. auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
  453. dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
  454. auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](ReadonlyBytes readonly_bytes) {
  455. for (size_t i = 0; i < readonly_bytes.size(); i += sizeof(u16))
  456. IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)readonly_bytes.offset(i));
  457. return readonly_bytes.size();
  458. });
  459. if (result.is_error())
  460. complete_current_request(AsyncDeviceRequest::MemoryFault);
  461. }
  462. // FIXME: I'm assuming this doesn't work based on the fact PIO read doesn't work.
  463. void IDEChannel::ata_write_sectors(bool slave_request, u16 capabilities)
  464. {
  465. VERIFY(m_lock.is_locked());
  466. VERIFY(!m_current_request.is_null());
  467. VERIFY(m_current_request->block_count() <= 256);
  468. SpinlockLocker m_lock(m_request_lock);
  469. dbgln_if(PATA_DEBUG, "IDEChannel: Writing {} sector(s) @ LBA {}", m_current_request->block_count(), m_current_request->block_index());
  470. ata_access(Direction::Write, slave_request, m_current_request->block_index(), m_current_request->block_count(), capabilities);
  471. ata_do_write_sector();
  472. }
  473. }