HelenOS sources
This source file includes following definitions.
- usbmast_scsi_dev_type_str
- usbmast_dump_sense
- usb_massstor_unit_ready
- usbmast_run_cmd
- usbmast_inquiry
- usbmast_request_sense
- usbmast_read_capacity
- usbmast_read
- usbmast_write
- usbmast_sync_cache
#include <bitops.h>
#include <byteorder.h>
#include <inttypes.h>
#include <macros.h>
#include <usb/dev/driver.h>
#include <usb/debug.h>
#include <errno.h>
#include <str_error.h>
#include <str.h>
#include <scsi/sbc.h>
#include <scsi/spc.h>
#include "cmdw.h"
#include "bo_trans.h"
#include "scsi_ms.h"
#include "usbmast.h"
const char *usbmast_scsi_dev_type_str(unsigned type)
{
return scsi_get_dev_type_str(type);
}
static void usbmast_dump_sense(scsi_sense_data_t *sense_buf)
{
const unsigned sense_key = sense_buf->flags_key & 0x0f;
printf("Got sense data. Sense key: 0x%x (%s), ASC 0x%02x, "
"ASCQ 0x%02x.\n", sense_key,
scsi_get_sense_key_str(sense_key),
sense_buf->additional_code,
sense_buf->additional_cqual);
}
static errno_t usb_massstor_unit_ready(usbmast_fun_t *mfun)
{
scsi_cmd_t cmd;
scsi_cdb_test_unit_ready_t cdb;
errno_t rc;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_TEST_UNIT_READY;
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
if (rc != EOK) {
usb_log_error("Test Unit Ready failed on device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD)
usb_log_warning("Test Unit Ready command failed on device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EOK;
}
static errno_t usbmast_run_cmd(usbmast_fun_t *mfun, scsi_cmd_t *cmd)
{
uint8_t sense_key;
scsi_sense_data_t sense_buf;
errno_t rc;
do {
rc = usb_massstor_unit_ready(mfun);
if (rc != EOK) {
usb_log_error("Inquiry transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
rc = usb_massstor_cmd(mfun, 0xDEADBEEF, cmd);
if (rc != EOK) {
usb_log_error("Inquiry transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd->status == CMDS_GOOD)
return EOK;
usb_log_error("SCSI command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
rc = usbmast_request_sense(mfun, &sense_buf, sizeof(sense_buf));
if (rc != EOK) {
usb_log_error("Failed to read sense data.");
return EIO;
}
usbmast_dump_sense(&sense_buf);
sense_key = sense_buf.flags_key & 0x0f;
if (sense_key == SCSI_SK_UNIT_ATTENTION) {
printf("Got unit attention. Re-trying command.\n");
}
} while (sense_key == SCSI_SK_UNIT_ATTENTION);
return EOK;
}
errno_t usbmast_inquiry(usbmast_fun_t *mfun, usbmast_inquiry_data_t *inq_res)
{
scsi_std_inquiry_data_t inq_data;
scsi_cmd_t cmd;
scsi_cdb_inquiry_t cdb;
errno_t rc;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_INQUIRY;
cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
cmd.data_in = &inq_data;
cmd.data_in_size = sizeof(inq_data);
rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
if (rc != EOK) {
usb_log_error("Inquiry transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD) {
usb_log_error("Inquiry command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EIO;
}
if (cmd.rcvd_size < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
usb_log_error("SCSI Inquiry response too short (%zu).",
cmd.rcvd_size);
return EIO;
}
memset(inq_res, 0, sizeof(*inq_res));
inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
inq_data.vendor, sizeof(inq_data.vendor));
spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
inq_data.product, sizeof(inq_data.product));
spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
inq_data.revision, sizeof(inq_data.revision));
return EOK;
}
errno_t usbmast_request_sense(usbmast_fun_t *mfun, void *buf, size_t size)
{
scsi_cmd_t cmd;
scsi_cdb_request_sense_t cdb;
errno_t rc;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_REQUEST_SENSE;
cdb.alloc_len = min(size, SCSI_SENSE_DATA_MAX_SIZE);
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
cmd.data_in = buf;
cmd.data_in_size = size;
rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
if (rc != EOK || cmd.status != CMDS_GOOD) {
usb_log_error("Request Sense failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.rcvd_size < SCSI_SENSE_DATA_MIN_SIZE) {
memset((uint8_t *)buf + cmd.rcvd_size, 0,
SCSI_SENSE_DATA_MIN_SIZE - cmd.rcvd_size);
}
return EOK;
}
errno_t usbmast_read_capacity(usbmast_fun_t *mfun, uint32_t *nblocks,
uint32_t *block_size)
{
scsi_cmd_t cmd;
scsi_cdb_read_capacity_10_t cdb;
scsi_read_capacity_10_data_t data;
errno_t rc;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
cmd.data_in = &data;
cmd.data_in_size = sizeof(data);
rc = usbmast_run_cmd(mfun, &cmd);
if (rc != EOK) {
usb_log_error("Read Capacity (10) transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD) {
usb_log_error("Read Capacity (10) command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EIO;
}
if (cmd.rcvd_size < sizeof(data)) {
usb_log_error("SCSI Read Capacity response too short (%zu).",
cmd.rcvd_size);
return EIO;
}
*nblocks = uint32_t_be2host(data.last_lba) + 1;
*block_size = uint32_t_be2host(data.block_size);
return EOK;
}
errno_t usbmast_read(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks, void *buf)
{
scsi_cmd_t cmd;
scsi_cdb_read_10_t cdb;
errno_t rc;
if (ba > UINT32_MAX)
return ELIMIT;
if (nblocks > UINT16_MAX)
return ELIMIT;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_READ_10;
cdb.lba = host2uint32_t_be(ba);
cdb.xfer_len = host2uint16_t_be(nblocks);
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
cmd.data_in = buf;
cmd.data_in_size = nblocks * mfun->block_size;
rc = usbmast_run_cmd(mfun, &cmd);
if (rc != EOK) {
usb_log_error("Read (10) transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD) {
usb_log_error("Read (10) command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EIO;
}
if (cmd.rcvd_size < nblocks * mfun->block_size) {
usb_log_error("SCSI Read response too short (%zu).",
cmd.rcvd_size);
return EIO;
}
return EOK;
}
errno_t usbmast_write(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks,
const void *data)
{
scsi_cmd_t cmd;
scsi_cdb_write_10_t cdb;
errno_t rc;
if (ba > UINT32_MAX)
return ELIMIT;
if (nblocks > UINT16_MAX)
return ELIMIT;
memset(&cdb, 0, sizeof(cdb));
cdb.op_code = SCSI_CMD_WRITE_10;
cdb.lba = host2uint32_t_be(ba);
cdb.xfer_len = host2uint16_t_be(nblocks);
memset(&cmd, 0, sizeof(cmd));
cmd.cdb = &cdb;
cmd.cdb_size = sizeof(cdb);
cmd.data_out = data;
cmd.data_out_size = nblocks * mfun->block_size;
rc = usbmast_run_cmd(mfun, &cmd);
if (rc != EOK) {
usb_log_error("Write (10) transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD) {
usb_log_error("Write (10) command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EIO;
}
return EOK;
}
errno_t usbmast_sync_cache(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks)
{
if (ba > UINT32_MAX)
return ELIMIT;
if (nblocks > UINT16_MAX)
return ELIMIT;
const scsi_cdb_sync_cache_10_t cdb = {
.op_code = SCSI_CMD_SYNC_CACHE_10,
.lba = host2uint32_t_be(ba),
.numlb = host2uint16_t_be(nblocks),
};
scsi_cmd_t cmd = {
.cdb = &cdb,
.cdb_size = sizeof(cdb),
};
const errno_t rc = usbmast_run_cmd(mfun, &cmd);
if (rc != EOK) {
usb_log_error("Synchronize Cache (10) transport failed, device %s: %s.",
usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
return rc;
}
if (cmd.status != CMDS_GOOD) {
usb_log_error("Synchronize Cache (10) command failed, device %s.",
usb_device_get_name(mfun->mdev->usb_dev));
return EIO;
}
return EOK;
}
HelenOS homepage, sources at GitHub