IDEChannel.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. dbgln<debug_pata>("IDEChannel::complete_current_request result: {}", (int)result);
  160. ASSERT(m_current_request);
  161. auto& request = *m_current_request;
  162. m_current_request = nullptr;
  163. if (m_current_request_uses_dma) {
  164. if (result == AsyncDeviceRequest::Success) {
  165. if (request.request_type() == AsyncBlockDeviceRequest::Read) {
  166. if (!request.write_to_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
  167. request.complete(AsyncDeviceRequest::MemoryFault);
  168. return;
  169. }
  170. }
  171. // I read somewhere that this may trigger a cache flush so let's do it.
  172. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  173. }
  174. }
  175. request.complete(result);
  176. });
  177. }
  178. void IDEChannel::initialize(bool force_pio)
  179. {
  180. m_parent_controller->enable_pin_based_interrupts();
  181. if (force_pio) {
  182. klog() << "IDEChannel: Requested to force PIO mode; not setting up DMA";
  183. return;
  184. }
  185. // Let's try to set up DMA transfers.
  186. PCI::enable_bus_mastering(m_parent_controller->pci_address());
  187. m_prdt_page = MM.allocate_supervisor_physical_page();
  188. prdt().end_of_table = 0x8000;
  189. m_dma_buffer_page = MM.allocate_supervisor_physical_page();
  190. klog() << "IDEChannel: Bus master IDE: " << m_io_group.bus_master_base();
  191. }
  192. static void print_ide_status(u8 status)
  193. {
  194. 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);
  195. }
  196. void IDEChannel::handle_irq(const RegisterState&)
  197. {
  198. u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  199. m_entropy_source.add_random_event(status);
  200. u8 bstatus = m_io_group.bus_master_base().offset(2).in<u8>();
  201. if (!(bstatus & 0x4)) {
  202. // interrupt not from this device, ignore
  203. #ifdef PATA_DEBUG
  204. klog() << "IDEChannel: ignore interrupt";
  205. #endif
  206. return;
  207. }
  208. ScopedSpinLock lock(m_request_lock);
  209. #ifdef PATA_DEBUG
  210. klog() << "IDEChannel: interrupt: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0);
  211. #endif
  212. if (!m_current_request) {
  213. #ifdef PATA_DEBUG
  214. dbgln("IDEChannel: IRQ but no pending request!");
  215. #endif
  216. return;
  217. }
  218. bool received_all_irqs = m_current_request_uses_dma || m_current_request_block_index + 1 >= m_current_request->block_count();
  219. if (status & ATA_SR_ERR) {
  220. print_ide_status(status);
  221. m_device_error = m_io_group.io_base().offset(ATA_REG_ERROR).in<u8>();
  222. dbgln("IDEChannel: Error {:#02x}!", (u8)m_device_error);
  223. complete_current_request(AsyncDeviceRequest::Failure);
  224. return;
  225. }
  226. m_device_error = 0;
  227. if (received_all_irqs) {
  228. complete_current_request(AsyncDeviceRequest::Success);
  229. } else {
  230. ASSERT(!m_current_request_uses_dma);
  231. // Now schedule reading/writing the buffer as soon as we leave the irq handler.
  232. // This is important so that we can safely access the buffers, which could
  233. // trigger page faults
  234. Processor::deferred_call_queue([this]() {
  235. if (m_current_request->request_type() == AsyncBlockDeviceRequest::Read) {
  236. dbgln("IDEChannel: Read block {}/{}", m_current_request_block_index, m_current_request->block_count());
  237. if (ata_do_read_sector()) {
  238. if (++m_current_request_block_index >= m_current_request->block_count()) {
  239. complete_current_request(AsyncDeviceRequest::Success);
  240. return;
  241. }
  242. // Wait for the next block
  243. enable_irq();
  244. }
  245. } else {
  246. if (!m_current_request_flushing_cache) {
  247. dbgln("IDEChannel: Wrote block {}/{}", m_current_request_block_index, m_current_request->block_count());
  248. if (++m_current_request_block_index >= m_current_request->block_count()) {
  249. // We read the last block, flush cache
  250. ASSERT(!m_current_request_flushing_cache);
  251. m_current_request_flushing_cache = true;
  252. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_CACHE_FLUSH);
  253. } else {
  254. // Read next block
  255. ata_do_write_sector();
  256. }
  257. } else {
  258. complete_current_request(AsyncDeviceRequest::Success);
  259. }
  260. }
  261. });
  262. }
  263. }
  264. static void io_delay()
  265. {
  266. for (int i = 0; i < 4; ++i)
  267. IO::in8(0x3f6);
  268. }
  269. void IDEChannel::detect_disks()
  270. {
  271. // There are only two possible disks connected to a channel
  272. for (auto i = 0; i < 2; i++) {
  273. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (i << 4)); // First, we need to select the drive itself
  274. // Apparently these need to be 0 before sending IDENTIFY?!
  275. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0x00);
  276. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0x00);
  277. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0x00);
  278. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0x00);
  279. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
  280. // Wait for the BSY flag to be reset
  281. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  282. ;
  283. if (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() == 0x00) {
  284. #ifdef PATA_DEBUG
  285. klog() << "IDEChannel: No " << (i == 0 ? "master" : "slave") << " disk detected!";
  286. #endif
  287. continue;
  288. }
  289. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  290. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  291. u8* b = bbuf.data();
  292. u16* w = (u16*)wbuf.data();
  293. const u16* wbufbase = (u16*)wbuf.data();
  294. for (u32 i = 0; i < 256; ++i) {
  295. u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
  296. *(w++) = data;
  297. *(b++) = MSB(data);
  298. *(b++) = LSB(data);
  299. }
  300. // "Unpad" the device name string.
  301. for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
  302. bbuf[i] = 0;
  303. u8 cyls = wbufbase[1];
  304. u8 heads = wbufbase[3];
  305. u8 spt = wbufbase[6];
  306. if (cyls == 0 || heads == 0 || spt == 0)
  307. continue;
  308. klog() << "IDEChannel: Name=" << ((char*)bbuf.data() + 54) << ", C/H/Spt=" << cyls << "/" << heads << "/" << spt;
  309. if (i == 0) {
  310. m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, cyls, heads, spt, 3, (m_channel_number == 0) ? 0 : 2);
  311. } else {
  312. m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, cyls, heads, spt, 3, (m_channel_number == 0) ? 1 : 3);
  313. }
  314. }
  315. }
  316. void IDEChannel::ata_read_sectors_with_dma(bool slave_request)
  317. {
  318. auto& request = *m_current_request;
  319. u32 lba = request.block_index();
  320. dbgln<debug_pata>("IDEChannel::ata_read_sectors_with_dma ({} x {})", lba, request.block_count());
  321. prdt().offset = m_dma_buffer_page->paddr();
  322. prdt().size = 512 * request.block_count();
  323. ASSERT(prdt().size <= PAGE_SIZE);
  324. // Stop bus master
  325. m_io_group.bus_master_base().out<u8>(0);
  326. // Write the PRDT location
  327. m_io_group.bus_master_base().offset(4).out(m_prdt_page->paddr().get());
  328. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  329. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  330. // Set transfer direction
  331. m_io_group.bus_master_base().out<u8>(0x8);
  332. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  333. ;
  334. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  335. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
  336. io_delay();
  337. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
  338. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  339. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  340. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  341. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  342. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  343. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  344. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  345. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  346. for (;;) {
  347. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  348. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  349. break;
  350. }
  351. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_DMA_EXT);
  352. io_delay();
  353. enable_irq();
  354. // Start bus master
  355. m_io_group.bus_master_base().out<u8>(0x9);
  356. }
  357. bool IDEChannel::ata_do_read_sector()
  358. {
  359. auto& request = *m_current_request;
  360. auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
  361. ssize_t nwritten = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) {
  362. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  363. *(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
  364. return (ssize_t)buffer_bytes;
  365. });
  366. if (nwritten < 0) {
  367. // TODO: Do we need to abort the PATA read if this wasn't the last block?
  368. complete_current_request(AsyncDeviceRequest::MemoryFault);
  369. return false;
  370. }
  371. return true;
  372. }
  373. void IDEChannel::ata_read_sectors(bool slave_request)
  374. {
  375. auto& request = *m_current_request;
  376. ASSERT(request.block_count() <= 256);
  377. #ifdef PATA_DEBUG
  378. dbgln("IDEChannel::ata_read_sectors");
  379. #endif
  380. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  381. ;
  382. auto lba = request.block_index();
  383. #ifdef PATA_DEBUG
  384. klog() << "IDEChannel: Reading " << request.block_count() << " sector(s) @ LBA " << lba;
  385. #endif
  386. u8 devsel = 0xe0;
  387. if (slave_request)
  388. devsel |= 0x10;
  389. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  390. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | (static_cast<u8>(slave_request) << 4) | 0x40);
  391. io_delay();
  392. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u8>(0);
  393. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  394. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  395. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  396. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  397. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  398. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  399. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  400. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  401. for (;;) {
  402. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  403. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  404. break;
  405. }
  406. enable_irq();
  407. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_PIO);
  408. }
  409. void IDEChannel::ata_write_sectors_with_dma(bool slave_request)
  410. {
  411. auto& request = *m_current_request;
  412. u32 lba = request.block_index();
  413. dbgln<debug_pata>("IDEChannel::ata_write_sectors_with_dma ({} x {})", lba, request.block_count());
  414. prdt().offset = m_dma_buffer_page->paddr();
  415. prdt().size = 512 * request.block_count();
  416. if (!request.read_from_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
  417. complete_current_request(AsyncDeviceRequest::MemoryFault);
  418. return;
  419. }
  420. ASSERT(prdt().size <= PAGE_SIZE);
  421. // Stop bus master
  422. m_io_group.bus_master_base().out<u8>(0);
  423. // Write the PRDT location
  424. m_io_group.bus_master_base().offset(4).out<u32>(m_prdt_page->paddr().get());
  425. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  426. m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
  427. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  428. ;
  429. m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
  430. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
  431. io_delay();
  432. m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
  433. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
  434. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
  435. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
  436. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
  437. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
  438. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  439. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  440. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  441. for (;;) {
  442. auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  443. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  444. break;
  445. }
  446. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_DMA_EXT);
  447. io_delay();
  448. enable_irq();
  449. // Start bus master
  450. m_io_group.bus_master_base().out<u8>(0x1);
  451. }
  452. void IDEChannel::ata_do_write_sector()
  453. {
  454. auto& request = *m_current_request;
  455. io_delay();
  456. 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))
  457. ;
  458. u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
  459. ASSERT(status & ATA_SR_DRQ);
  460. auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
  461. #ifndef PATA_DEBUG
  462. dbgln("IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
  463. #endif
  464. ssize_t nread = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](const u8* buffer, size_t buffer_bytes) {
  465. for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
  466. IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]);
  467. return (ssize_t)buffer_bytes;
  468. });
  469. if (nread < 0)
  470. complete_current_request(AsyncDeviceRequest::MemoryFault);
  471. }
  472. void IDEChannel::ata_write_sectors(bool slave_request)
  473. {
  474. auto& request = *m_current_request;
  475. ASSERT(request.block_count() <= 256);
  476. u32 start_sector = request.block_index();
  477. u32 count = request.block_count();
  478. #ifdef PATA_DEBUG
  479. klog() << "IDEChannel::ata_write_sectors request (" << count << " sector(s) @ " << start_sector << ")";
  480. #endif
  481. while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  482. ;
  483. #ifdef PATA_DEBUG
  484. klog() << "IDEChannel: Writing " << count << " sector(s) @ LBA " << start_sector;
  485. #endif
  486. u8 devsel = 0xe0;
  487. if (slave_request)
  488. devsel |= 0x10;
  489. m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(count == 256 ? 0 : LSB(count));
  490. m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(start_sector & 0xff);
  491. m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((start_sector >> 8) & 0xff);
  492. m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((start_sector >> 16) & 0xff);
  493. m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | ((start_sector >> 24) & 0xf));
  494. IO::out8(0x3F6, 0x08);
  495. while (!(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRDY))
  496. ;
  497. m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_PIO);
  498. io_delay();
  499. 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))
  500. ;
  501. enable_irq();
  502. ata_do_write_sector();
  503. }
  504. }