IDEChannel.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/FileSystem/ProcFS.h>
  30. #include <Kernel/IO.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/Storage/IDEChannel.h>
  33. #include <Kernel/Storage/IDEController.h>
  34. #include <Kernel/Storage/PATADiskDevice.h>
  35. #include <Kernel/VM/MemoryManager.h>
  36. namespace Kernel {
  37. #define PATA_PRIMARY_IRQ 14
  38. #define PATA_SECONDARY_IRQ 15
  39. //#define PATA_DEBUG
  40. #define ATA_SR_BSY 0x80
  41. #define ATA_SR_DRDY 0x40
  42. #define ATA_SR_DF 0x20
  43. #define ATA_SR_DSC 0x10
  44. #define ATA_SR_DRQ 0x08
  45. #define ATA_SR_CORR 0x04
  46. #define ATA_SR_IDX 0x02
  47. #define ATA_SR_ERR 0x01
  48. #define ATA_ER_BBK 0x80
  49. #define ATA_ER_UNC 0x40
  50. #define ATA_ER_MC 0x20
  51. #define ATA_ER_IDNF 0x10
  52. #define ATA_ER_MCR 0x08
  53. #define ATA_ER_ABRT 0x04
  54. #define ATA_ER_TK0NF 0x02
  55. #define ATA_ER_AMNF 0x01
  56. #define ATA_CMD_READ_PIO 0x20
  57. #define ATA_CMD_READ_PIO_EXT 0x24
  58. #define ATA_CMD_READ_DMA 0xC8
  59. #define ATA_CMD_READ_DMA_EXT 0x25
  60. #define ATA_CMD_WRITE_PIO 0x30
  61. #define ATA_CMD_WRITE_PIO_EXT 0x34
  62. #define ATA_CMD_WRITE_DMA 0xCA
  63. #define ATA_CMD_WRITE_DMA_EXT 0x35
  64. #define ATA_CMD_CACHE_FLUSH 0xE7
  65. #define ATA_CMD_CACHE_FLUSH_EXT 0xEA
  66. #define ATA_CMD_PACKET 0xA0
  67. #define ATA_CMD_IDENTIFY_PACKET 0xA1
  68. #define ATA_CMD_IDENTIFY 0xEC
  69. #define ATAPI_CMD_READ 0xA8
  70. #define ATAPI_CMD_EJECT 0x1B
  71. #define ATA_IDENT_DEVICETYPE 0
  72. #define ATA_IDENT_CYLINDERS 2
  73. #define ATA_IDENT_HEADS 6
  74. #define ATA_IDENT_SECTORS 12
  75. #define ATA_IDENT_SERIAL 20
  76. #define ATA_IDENT_MODEL 54
  77. #define ATA_IDENT_CAPABILITIES 98
  78. #define ATA_IDENT_FIELDVALID 106
  79. #define ATA_IDENT_MAX_LBA 120
  80. #define ATA_IDENT_COMMANDSETS 164
  81. #define ATA_IDENT_MAX_LBA_EXT 200
  82. #define IDE_ATA 0x00
  83. #define IDE_ATAPI 0x01
  84. #define ATA_REG_DATA 0x00
  85. #define ATA_REG_ERROR 0x01
  86. #define ATA_REG_FEATURES 0x01
  87. #define ATA_REG_SECCOUNT0 0x02
  88. #define ATA_REG_LBA0 0x03
  89. #define ATA_REG_LBA1 0x04
  90. #define ATA_REG_LBA2 0x05
  91. #define ATA_REG_HDDEVSEL 0x06
  92. #define ATA_REG_COMMAND 0x07
  93. #define ATA_REG_STATUS 0x07
  94. #define ATA_CTL_CONTROL 0x00
  95. #define ATA_CTL_ALTSTATUS 0x00
  96. #define ATA_CTL_DEVADDRESS 0x01
  97. #define PCI_Mass_Storage_Class 0x1
  98. #define PCI_IDE_Controller_Subclass 0x1
  99. NonnullOwnPtr<IDEChannel> IDEChannel::create(const IDEController& controller, IOAddressGroup io_group, ChannelType type, bool force_pio)
  100. {
  101. return make<IDEChannel>(controller, io_group, type, force_pio);
  102. }
  103. RefPtr<StorageDevice> IDEChannel::master_device() const
  104. {
  105. return m_master;
  106. }
  107. RefPtr<StorageDevice> IDEChannel::slave_device() const
  108. {
  109. return m_slave;
  110. }
  111. IDEChannel::IDEChannel(const IDEController& controller, IOAddressGroup io_group, ChannelType type, bool force_pio)
  112. : IRQHandler(type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ)
  113. , m_channel_number((type == ChannelType::Primary ? 0 : 1))
  114. , m_io_group(io_group)
  115. , m_parent_controller(controller)
  116. {
  117. disable_irq();
  118. m_dma_enabled.resource() = !force_pio;
  119. ProcFS::add_sys_bool("ide_dma", m_dma_enabled);
  120. initialize(force_pio);
  121. detect_disks();
  122. enable_irq();
  123. }
  124. IDEChannel::~IDEChannel()
  125. {
  126. }
  127. void IDEChannel::start_request(AsyncBlockDeviceRequest& request, bool use_dma, bool is_slave)
  128. {
  129. ScopedSpinLock lock(m_request_lock);
  130. #ifdef PATA_DEBUG
  131. dbgln("IDEChannel::start_request");
  132. #endif
  133. m_current_request = &request;
  134. m_current_request_block_index = 0;
  135. m_current_request_uses_dma = use_dma;
  136. m_current_request_flushing_cache = false;
  137. if (request.request_type() == AsyncBlockDeviceRequest::Read) {
  138. if (use_dma)
  139. ata_read_sectors_with_dma(is_slave);
  140. else
  141. ata_read_sectors(is_slave);
  142. } else {
  143. if (use_dma)
  144. ata_write_sectors_with_dma(is_slave);
  145. else
  146. ata_write_sectors(is_slave);
  147. }
  148. }
  149. void IDEChannel::complete_current_request(AsyncDeviceRequest::RequestResult result)
  150. {
  151. // NOTE: this may be called from the interrupt handler!
  152. ASSERT(m_current_request);
  153. ASSERT(m_request_lock.is_locked());
  154. // Now schedule reading back the buffer as soon as we leave the irq handler.
  155. // This is important so that we can safely write the buffer back,
  156. // which could cause page faults. Note that this may be called immediately
  157. // before Processor::deferred_call_queue returns!
  158. Processor::deferred_call_queue([this, result]() {
  159. #ifdef PATA_DEBUG
  160. dbg() << "IDEChannel::complete_current_request result: " << result;
  161. #endif
  162. ASSERT(m_current_request);
  163. auto& request = *m_current_request;
  164. m_current_request = nullptr;
  165. if (m_current_request_uses_dma) {
  166. if (result == AsyncDeviceRequest::Success) {
  167. if (request.request_type() == AsyncBlockDeviceRequest::Read) {
  168. if (!request.write_to_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
  169. request.complete(AsyncDeviceRequest::MemoryFault);
  170. return;
  171. }
  172. }
  173. // I read somewhere that this may trigger a cache flush so let's do it.
  174. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  175. }
  176. }
  177. request.complete(result);
  178. });
  179. }
  180. void IDEChannel::initialize(bool force_pio)
  181. {
  182. m_parent_controller->enable_pin_based_interrupts();
  183. if (force_pio) {
  184. klog() << "IDEChannel: Requested to force PIO mode; not setting up DMA";
  185. return;
  186. }
  187. // Let's try to set up DMA transfers.
  188. PCI::enable_bus_mastering(m_parent_controller->pci_address());
  189. m_prdt_page = MM.allocate_supervisor_physical_page();
  190. prdt().end_of_table = 0x8000;
  191. m_dma_buffer_page = MM.allocate_supervisor_physical_page();
  192. klog() << "IDEChannel: Bus master IDE: " << m_io_group.bus_master_base();
  193. }
  194. static void print_ide_status(u8 status)
  195. {
  196. klog() << "IDEChannel: print_ide_status: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0) << " DSC=" << ((status & ATA_SR_DSC) != 0) << " DF=" << ((status & ATA_SR_DF) != 0) << " CORR=" << ((status & ATA_SR_CORR) != 0) << " IDX=" << ((status & ATA_SR_IDX) != 0) << " ERR=" << ((status & ATA_SR_ERR) != 0);
  197. }
  198. void IDEChannel::handle_irq(const RegisterState&)
  199. {
  200. u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  201. m_entropy_source.add_random_event(status);
  202. u8 bstatus = m_io_group.bus_master_base().offset(2).in<u8>();
  203. if (!(bstatus & 0x4)) {
  204. // interrupt not from this device, ignore
  205. #ifdef PATA_DEBUG
  206. klog() << "IDEChannel: ignore interrupt";
  207. #endif
  208. return;
  209. }
  210. ScopedSpinLock lock(m_request_lock);
  211. #ifdef PATA_DEBUG
  212. klog() << "IDEChannel: interrupt: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0);
  213. #endif
  214. if (!m_current_request) {
  215. #ifdef PATA_DEBUG
  216. dbgln("IDEChannel: IRQ but no pending request!");
  217. #endif
  218. return;
  219. }
  220. bool received_all_irqs = m_current_request_uses_dma || m_current_request_block_index + 1 >= m_current_request->block_count();
  221. if (status & ATA_SR_ERR) {
  222. print_ide_status(status);
  223. m_device_error = m_io_group.io_base().offset(ATA_REG_ERROR).in<u8>();
  224. dbgln("IDEChannel: Error {:#02x}!", (u8)m_device_error);
  225. complete_current_request(AsyncDeviceRequest::Failure);
  226. return;
  227. }
  228. m_device_error = 0;
  229. if (received_all_irqs) {
  230. complete_current_request(AsyncDeviceRequest::Success);
  231. } else {
  232. ASSERT(!m_current_request_uses_dma);
  233. // Now schedule reading/writing the buffer as soon as we leave the irq handler.
  234. // This is important so that we can safely access the buffers, which could
  235. // trigger page faults
  236. Processor::deferred_call_queue([this]() {
  237. if (m_current_request->request_type() == AsyncBlockDeviceRequest::Read) {
  238. dbgln("IDEChannel: Read block {}/{}", m_current_request_block_index, m_current_request->block_count());
  239. if (ata_do_read_sector()) {
  240. if (++m_current_request_block_index >= m_current_request->block_count()) {
  241. complete_current_request(AsyncDeviceRequest::Success);
  242. return;
  243. }
  244. // Wait for the next block
  245. enable_irq();
  246. }
  247. } else {
  248. if (!m_current_request_flushing_cache) {
  249. dbgln("IDEChannel: Wrote block {}/{}", m_current_request_block_index, m_current_request->block_count());
  250. if (++m_current_request_block_index >= m_current_request->block_count()) {
  251. // We read the last block, flush cache
  252. ASSERT(!m_current_request_flushing_cache);
  253. m_current_request_flushing_cache = true;
  254. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_CACHE_FLUSH);
  255. } else {
  256. // Read next block
  257. ata_do_write_sector();
  258. }
  259. } else {
  260. complete_current_request(AsyncDeviceRequest::Success);
  261. }
  262. }
  263. });
  264. }
  265. }
  266. static void io_delay()
  267. {
  268. for (int i = 0; i < 4; ++i)
  269. IO::in8(0x3f6);
  270. }
  271. void IDEChannel::detect_disks()
  272. {
  273. // There are only two possible disks connected to a channel
  274. for (auto i = 0; i < 2; i++) {
  275. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (i << 4)); // First, we need to select the drive itself
  276. // Apparently these need to be 0 before sending IDENTIFY?!
  277. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0x00);
  278. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0x00);
  279. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0x00);
  280. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0x00);
  281. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
  282. // Wait for the BSY flag to be reset
  283. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  284. ;
  285. if (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() == 0x00) {
  286. #ifdef PATA_DEBUG
  287. klog() << "IDEChannel: No " << (i == 0 ? "master" : "slave") << " disk detected!";
  288. #endif
  289. continue;
  290. }
  291. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  292. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  293. u8* b = bbuf.data();
  294. u16* w = (u16*)wbuf.data();
  295. const u16* wbufbase = (u16*)wbuf.data();
  296. for (u32 i = 0; i < 256; ++i) {
  297. u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
  298. *(w++) = data;
  299. *(b++) = MSB(data);
  300. *(b++) = LSB(data);
  301. }
  302. // "Unpad" the device name string.
  303. for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
  304. bbuf[i] = 0;
  305. u8 cyls = wbufbase[1];
  306. u8 heads = wbufbase[3];
  307. u8 spt = wbufbase[6];
  308. if (cyls == 0 || heads == 0 || spt == 0)
  309. continue;
  310. klog() << "IDEChannel: Name=" << ((char*)bbuf.data() + 54) << ", C/H/Spt=" << cyls << "/" << heads << "/" << spt;
  311. if (i == 0) {
  312. m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, cyls, heads, spt, 3, (m_channel_number == 0) ? 0 : 2);
  313. } else {
  314. m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, cyls, heads, spt, 3, (m_channel_number == 0) ? 1 : 3);
  315. }
  316. }
  317. }
  318. void IDEChannel::ata_read_sectors_with_dma(bool slave_request)
  319. {
  320. auto& request = *m_current_request;
  321. u32 lba = request.block_index();
  322. #ifdef PATA_DEBUG
  323. dbg() << "IDEChannel::ata_read_sectors_with_dma (" << lba << " x" << request.block_count() << ")";
  324. #endif
  325. prdt().offset = m_dma_buffer_page->paddr();
  326. prdt().size = 512 * request.block_count();
  327. ASSERT(prdt().size <= PAGE_SIZE);
  328. // Stop bus master
  329. m_io_group.bus_master_base().out<u8>(0);
  330. // Write the PRDT location
  331. m_io_group.bus_master_base().offset(4).out(m_prdt_page->paddr().get());
  332. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  333. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  334. // Set transfer direction
  335. m_io_group.bus_master_base().out<u8>(0x8);
  336. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  337. ;
  338. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  339. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
  340. io_delay();
  341. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
  342. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  343. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  344. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  345. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  346. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  347. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  348. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  349. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  350. for (;;) {
  351. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  352. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  353. break;
  354. }
  355. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_DMA_EXT);
  356. io_delay();
  357. enable_irq();
  358. // Start bus master
  359. m_io_group.bus_master_base().out<u8>(0x9);
  360. }
  361. bool IDEChannel::ata_do_read_sector()
  362. {
  363. auto& request = *m_current_request;
  364. auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
  365. ssize_t nwritten = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) {
  366. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  367. *(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
  368. return (ssize_t)buffer_bytes;
  369. });
  370. if (nwritten < 0) {
  371. // TODO: Do we need to abort the PATA read if this wasn't the last block?
  372. complete_current_request(AsyncDeviceRequest::MemoryFault);
  373. return false;
  374. }
  375. return true;
  376. }
  377. void IDEChannel::ata_read_sectors(bool slave_request)
  378. {
  379. auto& request = *m_current_request;
  380. ASSERT(request.block_count() <= 256);
  381. #ifdef PATA_DEBUG
  382. dbgln("IDEChannel::ata_read_sectors");
  383. #endif
  384. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  385. ;
  386. auto lba = request.block_index();
  387. #ifdef PATA_DEBUG
  388. klog() << "IDEChannel: Reading " << request.block_count() << " sector(s) @ LBA " << lba;
  389. #endif
  390. u8 devsel = 0xe0;
  391. if (slave_request)
  392. devsel |= 0x10;
  393. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  394. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | (static_cast<u8>(slave_request) << 4) | 0x40);
  395. io_delay();
  396. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u8>(0);
  397. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  398. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  399. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  400. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  401. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  402. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  403. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  404. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  405. for (;;) {
  406. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  407. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  408. break;
  409. }
  410. enable_irq();
  411. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_PIO);
  412. }
  413. void IDEChannel::ata_write_sectors_with_dma(bool slave_request)
  414. {
  415. auto& request = *m_current_request;
  416. u32 lba = request.block_index();
  417. #ifdef PATA_DEBUG
  418. dbg() << "IDEChannel::ata_write_sectors_with_dma (" << lba << " x" << request.block_count() << ")";
  419. #endif
  420. prdt().offset = m_dma_buffer_page->paddr();
  421. prdt().size = 512 * request.block_count();
  422. if (!request.read_from_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
  423. complete_current_request(AsyncDeviceRequest::MemoryFault);
  424. return;
  425. }
  426. ASSERT(prdt().size <= PAGE_SIZE);
  427. // Stop bus master
  428. m_io_group.bus_master_base().out<u8>(0);
  429. // Write the PRDT location
  430. m_io_group.bus_master_base().offset(4).out<u32>(m_prdt_page->paddr().get());
  431. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  432. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  433. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  434. ;
  435. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  436. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
  437. io_delay();
  438. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
  439. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  440. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  441. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  442. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  443. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  444. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  445. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  446. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  447. for (;;) {
  448. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  449. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  450. break;
  451. }
  452. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_DMA_EXT);
  453. io_delay();
  454. enable_irq();
  455. // Start bus master
  456. m_io_group.bus_master_base().out<u8>(0x1);
  457. }
  458. void IDEChannel::ata_do_write_sector()
  459. {
  460. auto& request = *m_current_request;
  461. io_delay();
  462. while ((m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY) || !(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRQ))
  463. ;
  464. u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  465. ASSERT(status & ATA_SR_DRQ);
  466. auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
  467. #ifndef PATA_DEBUG
  468. dbgln("IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
  469. #endif
  470. ssize_t nread = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](const u8* buffer, size_t buffer_bytes) {
  471. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  472. IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]);
  473. return (ssize_t)buffer_bytes;
  474. });
  475. if (nread < 0)
  476. complete_current_request(AsyncDeviceRequest::MemoryFault);
  477. }
  478. void IDEChannel::ata_write_sectors(bool slave_request)
  479. {
  480. auto& request = *m_current_request;
  481. ASSERT(request.block_count() <= 256);
  482. u32 start_sector = request.block_index();
  483. u32 count = request.block_count();
  484. #ifdef PATA_DEBUG
  485. klog() << "IDEChannel::ata_write_sectors request (" << count << " sector(s) @ " << start_sector << ")";
  486. #endif
  487. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  488. ;
  489. #ifdef PATA_DEBUG
  490. klog() << "IDEChannel: Writing " << count << " sector(s) @ LBA " << start_sector;
  491. #endif
  492. u8 devsel = 0xe0;
  493. if (slave_request)
  494. devsel |= 0x10;
  495. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(count == 256 ? 0 : LSB(count));
  496. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(start_sector & 0xff);
  497. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((start_sector >> 8) & 0xff);
  498. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((start_sector >> 16) & 0xff);
  499. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | ((start_sector >> 24) & 0xf));
  500. IO::out8(0x3F6, 0x08);
  501. while (!(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRDY))
  502. ;
  503. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_PIO);
  504. io_delay();
  505. while ((m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY) || !(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRQ))
  506. ;
  507. enable_irq();
  508. ata_do_write_sector();
  509. }
  510. }