Skip to content

Commit 9c003a8

Browse files
scsiblockcmds: don't inherit from BlockStorageDevice.
1 parent 00068dd commit 9c003a8

4 files changed

Lines changed: 34 additions & 23 deletions

File tree

devices/common/scsi/scsiblockcmds.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2323
#include <loguru.hpp>
2424
#include <memaccess.h>
2525

26-
ScsiBlockCmds::ScsiBlockCmds(int cache_blocks) : BlockStorageDevice(cache_blocks) {
26+
ScsiBlockCmds::ScsiBlockCmds() {
2727
this->enable_cmd(ScsiCommand::READ_6);
2828
this->enable_cmd(ScsiCommand::READ_10);
2929
this->enable_cmd(ScsiCommand::READ_12);
@@ -44,21 +44,21 @@ void ScsiBlockCmds::init_block_device(uint8_t medium_type, uint8_t dev_flags) {
4444

4545
phy_impl->set_read_more_data_cb(
4646
[this](int* dsize, uint8_t** dptr) -> bool {
47-
if (this->remain_size) {
48-
*dsize = this->read_more();
49-
*dptr = (uint8_t *)this->data_cache.get();
47+
if (this->blk_dev->get_remaining_size()) {
48+
*dsize = this->blk_dev->read_more();
49+
*dptr = this->blk_dev->get_cache_ptr();
5050
return true;
5151
} else
5252
return false;
5353
}
5454
);
5555

56-
if (this->is_writeable) {
56+
if (this->blk_dev->medium_writable()) {
5757
phy_impl->set_write_more_data_cb(
5858
[this](int* dsize, uint8_t** dptr) -> bool {
59-
if (this->remain_size) {
60-
*dsize = this->write_more();
61-
*dptr = (uint8_t *)this->data_cache.get();
59+
if (this->blk_dev->get_remaining_size()) {
60+
*dsize = this->blk_dev->write_more();
61+
*dptr = this->blk_dev->get_cache_ptr();
6262
return true;
6363
} else
6464
return false;
@@ -131,9 +131,9 @@ int ScsiBlockCmds::read_new() {
131131
return ScsiPhase::STATUS;
132132
}
133133

134-
this->set_fpos(this->get_lba());
135-
phy_impl->set_xfer_len(this->read_begin(nblocks));
136-
phy_impl->set_buffer((uint8_t *)this->data_cache.get());
134+
this->blk_dev->set_fpos(this->get_lba());
135+
phy_impl->set_xfer_len(this->blk_dev->read_begin(nblocks));
136+
phy_impl->set_buffer(this->blk_dev->get_cache_ptr());
137137

138138
return ScsiPhase::DATA_IN;
139139
}
@@ -161,12 +161,12 @@ int ScsiBlockCmds::write_new() {
161161
return ScsiPhase::STATUS;
162162
}
163163

164-
this->set_fpos(this->get_lba());
165-
phy_impl->set_xfer_len(this->write_begin(nblocks));
166-
phy_impl->set_buffer((uint8_t *)this->data_cache.get());
164+
this->blk_dev->set_fpos(this->get_lba());
165+
phy_impl->set_xfer_len(this->blk_dev->write_begin(nblocks));
166+
phy_impl->set_buffer(this->blk_dev->get_cache_ptr());
167167

168168
phy_impl->set_post_xfer_action([this]() {
169-
this->write_cache();
169+
this->blk_dev->write_cache();
170170
}
171171
);
172172

@@ -203,8 +203,8 @@ int ScsiBlockCmds::read_capacity() {
203203
return ScsiPhase::STATUS;
204204
}
205205

206-
uint32_t last_lba = this->size_blocks - 1;
207-
uint32_t blk_len = this->block_size;
206+
uint32_t last_lba = this->blk_dev->get_size_in_blocks() - 1;
207+
uint32_t blk_len = this->blk_dev->get_block_size();
208208

209209
WRITE_DWORD_BE_A(&this->buf_ptr[0], last_lba);
210210
WRITE_DWORD_BE_A(&this->buf_ptr[4], blk_len);
@@ -217,10 +217,12 @@ int ScsiBlockCmds::read_capacity() {
217217
int ScsiBlockCmds::format_block_descriptors(uint8_t* out_ptr) {
218218
uint8_t density_code = 0;
219219

220-
uint32_t nblocks = std::min((int64_t)this->size_blocks, (int64_t)0xFFFFFFFFUL);
220+
uint32_t nblocks = std::min((int64_t)this->blk_dev->get_size_in_blocks(),
221+
(int64_t)0xFFFFFFFFUL);
221222

222223
WRITE_DWORD_BE_A(&out_ptr[0], nblocks);
223-
WRITE_DWORD_BE_A(&out_ptr[4], (density_code << 24) | (this->block_size & 0xFFFFFF));
224+
WRITE_DWORD_BE_A(&out_ptr[4], (density_code << 24) |
225+
(this->blk_dev->get_block_size() & 0xFFFFFF));
224226

225227
return 8;
226228
}

devices/common/scsi/scsiblockcmds.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2929
#include <cinttypes>
3030

3131
/** Base class for SCSI block device commands. */
32-
class ScsiBlockCmds : public ScsiCommonCmds, public BlockStorageDevice {
32+
class ScsiBlockCmds : public ScsiCommonCmds {
3333
public:
34-
ScsiBlockCmds(int cache_blocks = 256);
34+
ScsiBlockCmds();
3535
~ScsiBlockCmds() = default;
3636

37+
void set_phys_block_dev(BlockStorageDevice *blk_dev_obj) {
38+
this->blk_dev = blk_dev_obj;
39+
}
40+
3741
void init_block_device(uint8_t medium_type, uint8_t dev_flags);
3842

3943
virtual int read_new();
@@ -56,6 +60,8 @@ class ScsiBlockCmds : public ScsiCommonCmds, public BlockStorageDevice {
5660

5761
uint32_t get_lba();
5862

63+
BlockStorageDevice* blk_dev = nullptr;
64+
5965
uint8_t medium_type = 0;
6066
uint8_t device_flags = 0;
6167
};

devices/common/scsi/scsihd.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ static char my_vendor_id[] = "QUANTUM ";
3232
static char my_product_id[] = "Emulated Disk ";
3333
static char my_revision_id[] = "di01";
3434

35-
ScsiHardDisk::ScsiHardDisk(std::string name, int my_id) : ScsiPhysDevice(name, my_id)
35+
ScsiHardDisk::ScsiHardDisk(std::string name, int my_id) : ScsiPhysDevice(name, my_id),
36+
BlockStorageDevice(256)
3637
{
3738
this->set_phys_dev(this);
39+
this->set_phys_block_dev(this);
3840
this->set_cdb_ptr(this->cmd_buf);
3941
this->set_buf_ptr(this->data_buf);
4042
this->set_buffer(this->data_buf);

devices/common/scsi/scsihd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2626

2727
#include <devices/common/scsi/scsi.h>
2828
#include <devices/common/scsi/scsiblockcmds.h>
29+
#include <devices/storage/blockstoragedevice.h>
2930

3031
#include <cinttypes>
3132
#include <string>
3233

33-
class ScsiHardDisk : public ScsiPhysDevice, public ScsiBlockCmds {
34+
class ScsiHardDisk : public ScsiPhysDevice, public BlockStorageDevice, public ScsiBlockCmds {
3435
public:
3536
ScsiHardDisk(std::string name, int my_id);
3637
~ScsiHardDisk() = default;

0 commit comments

Comments
 (0)