PATADiskDevice.cpp 6.3 KB

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