PATADiskDevice.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. //#define PATA_DEVICE_DEBUG
  27. #include <AK/Memory.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/Devices/PATAChannel.h>
  30. #include <Kernel/Devices/PATADiskDevice.h>
  31. #include <Kernel/FileSystem/FileDescription.h>
  32. namespace Kernel {
  33. NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(PATAChannel& channel, DriveType type, int major, int minor)
  34. {
  35. return adopt(*new PATADiskDevice(channel, type, major, minor));
  36. }
  37. PATADiskDevice::PATADiskDevice(PATAChannel& channel, DriveType type, int major, int minor)
  38. : BlockDevice(major, minor, 512)
  39. , m_drive_type(type)
  40. , m_channel(channel)
  41. {
  42. }
  43. PATADiskDevice::~PATADiskDevice()
  44. {
  45. }
  46. const char* PATADiskDevice::class_name() const
  47. {
  48. return "PATADiskDevice";
  49. }
  50. bool PATADiskDevice::read_blocks(unsigned index, u16 count, UserOrKernelBuffer& out)
  51. {
  52. if (!m_channel.m_bus_master_base.is_null() && m_channel.m_dma_enabled.resource())
  53. return read_sectors_with_dma(index, count, out);
  54. return read_sectors(index, count, out);
  55. }
  56. bool PATADiskDevice::write_blocks(unsigned index, u16 count, const UserOrKernelBuffer& data)
  57. {
  58. if (!m_channel.m_bus_master_base.is_null() && m_channel.m_dma_enabled.resource())
  59. return write_sectors_with_dma(index, count, data);
  60. for (unsigned i = 0; i < count; ++i) {
  61. if (!write_sectors(index + i, 1, data.offset(i * 512)))
  62. return false;
  63. }
  64. return true;
  65. }
  66. void PATADiskDevice::set_drive_geometry(u16 cyls, u16 heads, u16 spt)
  67. {
  68. m_cylinders = cyls;
  69. m_heads = heads;
  70. m_sectors_per_track = spt;
  71. }
  72. KResultOr<size_t> PATADiskDevice::read(FileDescription&, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
  73. {
  74. unsigned index = offset / block_size();
  75. u16 whole_blocks = len / block_size();
  76. ssize_t remaining = len % block_size();
  77. unsigned blocks_per_page = PAGE_SIZE / block_size();
  78. // PATAChannel will chuck a wobbly if we try to read more than PAGE_SIZE
  79. // at a time, because it uses a single page for its DMA buffer.
  80. if (whole_blocks >= blocks_per_page) {
  81. whole_blocks = blocks_per_page;
  82. remaining = 0;
  83. }
  84. #ifdef PATA_DEVICE_DEBUG
  85. klog() << "PATADiskDevice::read() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
  86. #endif
  87. if (whole_blocks > 0) {
  88. if (!read_blocks(index, whole_blocks, outbuf))
  89. return -1;
  90. }
  91. off_t pos = whole_blocks * block_size();
  92. if (remaining > 0) {
  93. auto data = ByteBuffer::create_uninitialized(block_size());
  94. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
  95. if (!read_blocks(index + whole_blocks, 1, data_buffer))
  96. return pos;
  97. if (!outbuf.write(data.data(), pos, remaining))
  98. return KResult(-EFAULT);
  99. }
  100. return pos + remaining;
  101. }
  102. bool PATADiskDevice::can_read(const FileDescription&, size_t offset) const
  103. {
  104. return offset < (m_cylinders * m_heads * m_sectors_per_track * block_size());
  105. }
  106. KResultOr<size_t> PATADiskDevice::write(FileDescription&, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
  107. {
  108. unsigned index = offset / block_size();
  109. u16 whole_blocks = len / block_size();
  110. ssize_t remaining = len % block_size();
  111. unsigned blocks_per_page = PAGE_SIZE / block_size();
  112. // PATAChannel will chuck a wobbly if we try to write more than PAGE_SIZE
  113. // at a time, because it uses a single page for its DMA buffer.
  114. if (whole_blocks >= blocks_per_page) {
  115. whole_blocks = blocks_per_page;
  116. remaining = 0;
  117. }
  118. #ifdef PATA_DEVICE_DEBUG
  119. klog() << "PATADiskDevice::write() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
  120. #endif
  121. if (whole_blocks > 0) {
  122. if (!write_blocks(index, whole_blocks, inbuf))
  123. return -1;
  124. }
  125. off_t pos = whole_blocks * block_size();
  126. // since we can only write in block_size() increments, if we want to do a
  127. // partial write, we have to read the block's content first, modify it,
  128. // then write the whole block back to the disk.
  129. if (remaining > 0) {
  130. auto data = ByteBuffer::create_zeroed(block_size());
  131. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
  132. if (!read_blocks(index + whole_blocks, 1, data_buffer))
  133. return pos;
  134. if (!inbuf.read(data.data(), pos, remaining))
  135. return KResult(-EFAULT);
  136. if (!write_blocks(index + whole_blocks, 1, data_buffer))
  137. return pos;
  138. }
  139. return pos + remaining;
  140. }
  141. bool PATADiskDevice::can_write(const FileDescription&, size_t offset) const
  142. {
  143. return offset < (m_cylinders * m_heads * m_sectors_per_track * block_size());
  144. }
  145. bool PATADiskDevice::read_sectors_with_dma(u32 lba, u16 count, UserOrKernelBuffer& outbuf)
  146. {
  147. return m_channel.ata_read_sectors_with_dma(lba, count, outbuf, is_slave());
  148. }
  149. bool PATADiskDevice::read_sectors(u32 start_sector, u16 count, UserOrKernelBuffer& outbuf)
  150. {
  151. return m_channel.ata_read_sectors(start_sector, count, outbuf, is_slave());
  152. }
  153. bool PATADiskDevice::write_sectors_with_dma(u32 lba, u16 count, const UserOrKernelBuffer& inbuf)
  154. {
  155. return m_channel.ata_write_sectors_with_dma(lba, count, inbuf, is_slave());
  156. }
  157. bool PATADiskDevice::write_sectors(u32 start_sector, u16 count, const UserOrKernelBuffer& inbuf)
  158. {
  159. return m_channel.ata_write_sectors(start_sector, count, inbuf, is_slave());
  160. }
  161. bool PATADiskDevice::is_slave() const
  162. {
  163. return m_drive_type == DriveType::Slave;
  164. }
  165. }