IDEChannel.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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/IO.h>
  10. #include <Kernel/Memory/MemoryManager.h>
  11. #include <Kernel/Process.h>
  12. #include <Kernel/Sections.h>
  13. #include <Kernel/Storage/ATA.h>
  14. #include <Kernel/Storage/IDEChannel.h>
  15. #include <Kernel/Storage/IDEController.h>
  16. #include <Kernel/Storage/PATADiskDevice.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 (!wait_until_not_busy(false, 30000)) {
  57. dbgln("IDEChannel: reset failed, busy flag on master stuck");
  58. return;
  59. }
  60. // Wait up to 30 seconds before failing
  61. if (!wait_until_not_busy(true, 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::wait_until_not_busy(bool slave, size_t milliseconds_timeout)
  232. {
  233. IO::delay(20);
  234. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (slave << 4)); // First, we need to select the drive itself
  235. IO::delay(20);
  236. size_t time_elapsed = 0;
  237. while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
  238. IO::delay(1000);
  239. time_elapsed++;
  240. }
  241. return time_elapsed <= milliseconds_timeout;
  242. }
  243. bool IDEChannel::wait_until_not_busy(size_t milliseconds_timeout)
  244. {
  245. size_t time_elapsed = 0;
  246. while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
  247. IO::delay(1000);
  248. time_elapsed++;
  249. }
  250. return time_elapsed <= milliseconds_timeout;
  251. }
  252. String IDEChannel::channel_type_string() const
  253. {
  254. if (m_channel_type == ChannelType::Primary)
  255. return "Primary";
  256. return "Secondary";
  257. }
  258. UNMAP_AFTER_INIT void IDEChannel::detect_disks()
  259. {
  260. auto channel_string = [](u8 i) -> const char* {
  261. if (i == 0)
  262. return "master";
  263. return "slave";
  264. };
  265. // There are only two possible disks connected to a channel
  266. for (auto i = 0; i < 2; i++) {
  267. // We need to select the drive and then we wait 20 microseconds... and it doesn't hurt anything so let's just do it.
  268. IO::delay(20);
  269. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (i << 4)); // First, we need to select the drive itself
  270. IO::delay(20);
  271. auto status = m_io_group.control_base().in<u8>();
  272. if (status == 0x0) {
  273. dbgln_if(PATA_DEBUG, "IDEChannel: No {} {} disk detected!", channel_type_string().to_lowercase(), channel_string(i));
  274. continue;
  275. }
  276. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  277. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  278. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  279. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  280. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
  281. // Wait 10 second for the BSY flag to clear
  282. if (!wait_until_not_busy(2000)) {
  283. dbgln_if(PATA_DEBUG, "IDEChannel: No {} {} disk detected, BSY flag was not reset!", channel_type_string().to_lowercase(), channel_string(i));
  284. continue;
  285. }
  286. bool check_for_atapi = false;
  287. bool device_presence = true;
  288. PATADiskDevice::InterfaceType interface_type = PATADiskDevice::InterfaceType::ATA;
  289. size_t milliseconds_elapsed = 0;
  290. for (;;) {
  291. // Wait about 10 seconds
  292. if (milliseconds_elapsed > 2000)
  293. break;
  294. u8 status = m_io_group.control_base().in<u8>();
  295. if (status & ATA_SR_ERR) {
  296. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device is not ATA. Will check for ATAPI.", channel_type_string(), channel_string(i));
  297. check_for_atapi = true;
  298. break;
  299. }
  300. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) {
  301. dbgln_if(PATA_DEBUG, "IDEChannel: {} {} device appears to be ATA.", channel_type_string(), channel_string(i));
  302. interface_type = PATADiskDevice::InterfaceType::ATA;
  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. interface_type = PATADiskDevice::InterfaceType::ATAPI;
  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. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  333. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  334. u8* b = bbuf.data();
  335. u16* w = (u16*)wbuf.data();
  336. for (u32 i = 0; i < 256; ++i) {
  337. u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
  338. *(w++) = data;
  339. *(b++) = MSB(data);
  340. *(b++) = LSB(data);
  341. }
  342. // "Unpad" the device name string.
  343. for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
  344. bbuf[i] = 0;
  345. volatile ATAIdentifyBlock& identify_block = (volatile ATAIdentifyBlock&)(*wbuf.data());
  346. u16 capabilities = identify_block.capabilities[0];
  347. // If the drive is so old that it doesn't support LBA, ignore it.
  348. if (!(capabilities & ATA_CAP_LBA))
  349. continue;
  350. u64 max_addressable_block = identify_block.max_28_bit_addressable_logical_sector;
  351. // if we support 48-bit LBA, use that value instead.
  352. if (identify_block.commands_and_feature_sets_supported[1] & (1 << 10))
  353. max_addressable_block = identify_block.user_addressable_logical_sectors_count;
  354. dbgln("IDEChannel: {} {} {} device found: Name={}, Capacity={}, Capabilities={:#04x}", channel_type_string(), channel_string(i), interface_type == PATADiskDevice::InterfaceType::ATA ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), max_addressable_block * 512, capabilities);
  355. if (i == 0) {
  356. m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, interface_type, capabilities, max_addressable_block);
  357. } else {
  358. m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, interface_type, capabilities, max_addressable_block);
  359. }
  360. }
  361. }
  362. void IDEChannel::ata_access(Direction direction, bool slave_request, u64 lba, u8 block_count, u16 capabilities)
  363. {
  364. VERIFY(m_lock.is_locked());
  365. VERIFY(m_request_lock.is_locked());
  366. LBAMode lba_mode;
  367. u8 head = 0;
  368. VERIFY(capabilities & ATA_CAP_LBA);
  369. if (lba >= 0x10000000) {
  370. lba_mode = LBAMode::FortyEightBit;
  371. head = 0;
  372. } else {
  373. lba_mode = LBAMode::TwentyEightBit;
  374. head = (lba & 0xF000000) >> 24;
  375. }
  376. // Wait 1 second
  377. wait_until_not_busy(1000);
  378. // We need to select the drive and then we wait 20 microseconds... and it doesn't hurt anything so let's just do it.
  379. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xE0 | (static_cast<u8>(slave_request) << 4) | head);
  380. IO::delay(20);
  381. if (lba_mode == LBAMode::FortyEightBit) {
  382. m_io_group.io_base().offset(ATA_REG_SECCOUNT1).out<u8>(0);
  383. m_io_group.io_base().offset(ATA_REG_LBA3).out<u8>((lba & 0xFF000000) >> 24);
  384. m_io_group.io_base().offset(ATA_REG_LBA4).out<u8>((lba & 0xFF00000000ull) >> 32);
  385. m_io_group.io_base().offset(ATA_REG_LBA5).out<u8>((lba & 0xFF0000000000ull) >> 40);
  386. }
  387. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(block_count);
  388. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000FF) >> 0);
  389. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000FF00) >> 8);
  390. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00FF0000) >> 16);
  391. for (;;) {
  392. auto status = m_io_group.control_base().in<u8>();
  393. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  394. break;
  395. }
  396. send_ata_io_command(lba_mode, direction);
  397. enable_irq();
  398. }
  399. void IDEChannel::send_ata_io_command(LBAMode lba_mode, Direction direction) const
  400. {
  401. if (lba_mode != LBAMode::FortyEightBit) {
  402. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(direction == Direction::Read ? ATA_CMD_READ_PIO : ATA_CMD_WRITE_PIO);
  403. } else {
  404. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(direction == Direction::Read ? ATA_CMD_READ_PIO_EXT : ATA_CMD_WRITE_PIO_EXT);
  405. }
  406. }
  407. bool IDEChannel::ata_do_read_sector()
  408. {
  409. VERIFY(m_lock.is_locked());
  410. VERIFY(m_request_lock.is_locked());
  411. VERIFY(!m_current_request.is_null());
  412. dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector");
  413. auto& request = *m_current_request;
  414. auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
  415. auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) {
  416. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  417. *(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
  418. return buffer_bytes;
  419. });
  420. if (result.is_error()) {
  421. // TODO: Do we need to abort the PATA read if this wasn't the last block?
  422. complete_current_request(AsyncDeviceRequest::MemoryFault);
  423. return false;
  424. }
  425. return true;
  426. }
  427. // FIXME: This doesn't quite work and locks up reading LBA 3.
  428. void IDEChannel::ata_read_sectors(bool slave_request, u16 capabilities)
  429. {
  430. VERIFY(m_lock.is_locked());
  431. VERIFY(!m_current_request.is_null());
  432. VERIFY(m_current_request->block_count() <= 256);
  433. SpinlockLocker m_lock(m_request_lock);
  434. dbgln_if(PATA_DEBUG, "IDEChannel::ata_read_sectors");
  435. dbgln_if(PATA_DEBUG, "IDEChannel: Reading {} sector(s) @ LBA {}", m_current_request->block_count(), m_current_request->block_index());
  436. ata_access(Direction::Read, slave_request, m_current_request->block_index(), m_current_request->block_count(), capabilities);
  437. }
  438. void IDEChannel::ata_do_write_sector()
  439. {
  440. VERIFY(m_lock.is_locked());
  441. VERIFY(m_request_lock.is_locked());
  442. VERIFY(!m_current_request.is_null());
  443. auto& request = *m_current_request;
  444. io_delay();
  445. while ((m_io_group.control_base().in<u8>() & ATA_SR_BSY) || !(m_io_group.control_base().in<u8>() & ATA_SR_DRQ))
  446. ;
  447. u8 status = m_io_group.control_base().in<u8>();
  448. VERIFY(status & ATA_SR_DRQ);
  449. auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
  450. dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
  451. auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](u8 const* buffer, size_t buffer_bytes) {
  452. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  453. IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]);
  454. return buffer_bytes;
  455. });
  456. if (result.is_error())
  457. complete_current_request(AsyncDeviceRequest::MemoryFault);
  458. }
  459. // FIXME: I'm assuming this doesn't work based on the fact PIO read doesn't work.
  460. void IDEChannel::ata_write_sectors(bool slave_request, u16 capabilities)
  461. {
  462. VERIFY(m_lock.is_locked());
  463. VERIFY(!m_current_request.is_null());
  464. VERIFY(m_current_request->block_count() <= 256);
  465. SpinlockLocker m_lock(m_request_lock);
  466. dbgln_if(PATA_DEBUG, "IDEChannel: Writing {} sector(s) @ LBA {}", m_current_request->block_count(), m_current_request->block_index());
  467. ata_access(Direction::Write, slave_request, m_current_request->block_index(), m_current_request->block_count(), capabilities);
  468. ata_do_write_sector();
  469. }
  470. }